Plugin C# Code – Retrieve Example

Created Customer Email and Customer Phone filed in PowerBills(Child Entity)..when ever user select customer filed in power bills it Retrieves fetchs email and phone from the CustomerInformation(Parent Entity).

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace CustomerInfo
{
    public class CustomerInfo : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            if (context.Depth > 1)
            {
                return;
            }

            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                Entity sourceRecordInfo = (Entity)context.InputParameters["Target"];

                IOrganizationServiceFactory serviceFactory =
                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                try
                {
                    string strEmail = " ";
                    string strPhone = " ";

                    // GET lookup field
                    if (sourceRecordInfo.Contains("sdc_customer") && sourceRecordInfo["sdc_customer"] != null)
                    {
                        // GET customer guid from lookup
                        EntityReference customerRef = (EntityReference)sourceRecordInfo["sdc_customer"];
                        Guid customerGuid = customerRef.Id;

                        // RETRIEVE parent entity record
                        ColumnSet customerFields = new ColumnSet("sdc_email", "sdc_phonenumber");
                        Entity customerInfo = service.Retrieve("sdc_customerinformation", customerGuid, customerFields);

                        // GET email from parent
                        if (customerInfo.Contains("sdc_email") && customerInfo["sdc_email"] != null)
                        {
                            strEmail = customerInfo["sdc_email"].ToString();
                        }
                        // GET phone from parent
                        if (customerInfo.Contains("sdc_phonenumber") && customerInfo["sdc_phonenumber"] != null)
                        {
                            strPhone = customerInfo["sdc_phonenumber"].ToString();
                        }

                        // SET email in child entity ✅
                        sourceRecordInfo["sdc_customeremailplugin"] = strEmail;
                        // SET phone in child entity ✅
                        sourceRecordInfo["sdc_customerphoneplugin"] = strPhone;
                    }
                }
                catch (Exception ex)
                {
                    tracingService.Trace("CustomerInfo: {0}", ex.ToString());
                    throw;
                }
            }
        }
    }
}

2. Created Customer Phone number and Product ID in CustomerPurchasedProducts(Child)…. and Retrive this filed from ParentEntity(BajajProducts) and CustomerInformation(ParentEntity)

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace CustomerInfo
{
    public class CustomerPurchaseproducts : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            if (context.Depth > 1)
            {
                return;
            }

            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                Entity sourceRecordInfo = (Entity)context.InputParameters["Target"];

                IOrganizationServiceFactory serviceFactory =
                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                try
                {
                    string strProductID = " ";
                    string strCustomerphone = " ";

                    // GET lookup field
                    if (sourceRecordInfo.Contains("sdc_customer") && sourceRecordInfo["sdc_customer"] != null)
                    {
                        // GET customer guid from lookup
                        EntityReference customerRef = (EntityReference)sourceRecordInfo["sdc_customer"];
                        Guid customerGuid = customerRef.Id;

                        // RETRIEVE parent entity record
                        ColumnSet customerFields = new ColumnSet("sdc_phonenumber");
                        Entity customerInfo = service.Retrieve("sdc_bajajcustomer", customerGuid, customerFields);


                        // GET phone from parent
                        if (customerInfo.Contains("sdc_phonenumber") && customerInfo["sdc_phonenumber"] != null)
                        {
                            strCustomerphone = customerInfo["sdc_phonenumber"].ToString();
                        }

                        // SET email in child entity ✅
                        sourceRecordInfo["sdc_customerphoneplugin"] = strCustomerphone;

                    }
                    if(sourceRecordInfo.Contains("sdc_product") && sourceRecordInfo["sdc_product"] != null)
                        {
                        // GET customer guid from lookup
                        EntityReference priductRef = (EntityReference)sourceRecordInfo["sdc_product"];
                        Guid productGuid = priductRef.Id;

                        // RETRIEVE parent entity record
                        ColumnSet productFields = new ColumnSet("sdc_productid");
                        Entity producutInfo = service.Retrieve("sdc_bajajproducts", productGuid, productFields);


                        // GET phone from parent
                        if (producutInfo.Contains("sdc_productid") && producutInfo["sdc_productid"] != null)
                        {
                            strProductID = producutInfo["sdc_productid"].ToString();
                        }

                        // SET email in child entity ✅
                        sourceRecordInfo["sdc_productidplugin"] = strProductID;
                    }
                }
                catch (Exception ex)
                {
                    tracingService.Trace("CustomerInfo: {0}", ex.ToString());
                    throw;
                }
            }
        }
    }
}

Leave a Comment