Thursday 31 March 2022

Java Script to add XRM web API on Form load

 function RetieveContact(executionContext)

{

 var formContext = executionContext.getFormContext();

        var recordId = formContext.data.entity.getId();

    Xrm.WebApi.retrieveMultipleRecords("contact", "?$select=firstname,lastname&$filter=_parentcustomerid_value eq '+recordId +'").then(

        function success(result){

            for (var i = 0; i < result.entities.length; i++)

            {

formContext.getAttribute("websiteurl").setValue("http://newvalue.com");

                alert(result.entities[i].firstname);

formContext.data.entity.save("saveandclose");

                break; // just for code demo

// Set value


            }

        },

        function (error)

        {

            alert("Error: " + error.message);

        }

    );

}


 //JavaScript source code


Thursday 17 March 2022

MS Plugin Linq Query Using late Bound

 using System;JoyTaylor@zevn.onmicrosoft.com

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.ServiceModel;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Client;


namespace AccountPluginOnPostCreate

{

    public class AccountPlugin : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

            // Obtain the tracing service

            ITracingService tracingService =

            (ITracingService)serviceProvider.GetService(typeof(ITracingService));


            // Obtain the execution context from the service provider.  

            IPluginExecutionContext context = (IPluginExecutionContext)

                serviceProvider.GetService(typeof(IPluginExecutionContext));

            // The InputParameters collection contains all the data passed in the message request.  

            if (context.InputParameters.Contains("Target") &&

                context.InputParameters["Target"] is Entity && context.MessageName.ToLower() == "update")

            {

                // Obtain the target entity from the input parameters.  

                Entity entity = (Entity)context.InputParameters["Target"];


                // Obtain the organization service reference which you will need for  

                // web service calls.  

                IOrganizationServiceFactory serviceFactory =

                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                OrganizationServiceContext orgContext = new OrganizationServiceContext(service);

                try

                {

                    var Contact = from a in orgContext.CreateQuery("contact")

                                  where (Guid)a["parentcustomerid"] == (Guid)entity.Id

                                  select a;


                    foreach (var con in Contact)

                    {

                        entity["telephone1"] = (string)con.Attributes["mobilephone"];

                    }


                    // Plug-in business logic goes here.  

                }


                catch (FaultException<OrganizationServiceFault> ex)

                {

                    throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);

                }


                catch (Exception ex)

                {

                    tracingService.Trace("Account Craete Plugin : {0}", ex.ToString());

                    throw;

                }

            }

        }

    }

}