Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Plugin Development - Firing during Plugin Profiled .

(0) ShareShare
ReportReport
Posted on by 5

I'm new to plugin development, so bare my naivety. This is an exercise so I can become more familiar with plugins. 

When a user updates a Contact record's Account Name field, the plugin will obtain the id of the Account used in the update and return the total number of child Contacts with Account. It will then update the Contact's 'Total Contacts in Account' field with the number of contacts it found.

pastedimage1674158929397v5.png

List of Contacts with Adventure Works (sample)

pastedimage1674158208196v3.png

The problem I'm facing is that the logic will work when I activate Plugin Profiler, but not when plugin profiler is off. I'm assuming there's something wrong with the registration step, but I can't seem to find a way to fix it. 

Here is my code:

public class QueryContactsFromAccounts : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // 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){
                // 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);
                // Retrieves Target entity. Contact is the target. We will register this plugin for Contact Update on Primary Customer and Update Primary Customer
                if (entity.LogicalName == "contact")
                    {
                    if (entity.Attributes.Contains("parentcustomerid"))
                        {
                            // Instantiate QueryExpression looking for Contact. 
                            QueryExpression qeContacts = new QueryExpression("contact");
                            qeContacts.ColumnSet.AddColumns("contactid");
                            //
                            if (entity.GetAttributeValue("parentcustomerid") != null)
                            {
                                // Instantiate AccountGUID that you will use to lookup Contacts. 
                                string AccountGUID = entity.GetAttributeValue("parentcustomerid").Id.ToString();
                                // Build query with PageInfo to include Total Record Count. 
                                qeContacts.PageInfo = new PagingInfo { ReturnTotalRecordCount = true };
                                qeContacts.Criteria.AddCondition("parentcustomerid", ConditionOperator.Equal, AccountGUID);
                                // retireve number of Contacts. 
                                EntityCollection ec = service.RetrieveMultiple(qeContacts);
                                int totalRecordCount = ec.TotalRecordCount;
                                // update Contac record.
                                Entity updateContact = new Entity();
                                updateContact.LogicalName = "contact";
                                updateContact.Id = entity.Id;
                                updateContact["cr876_totalcontactsinaccount"] = totalRecordCount;
                                service.Update(updateContact);
                            }
                        }
                    }
            }
        }

    }

Here is my plugin registration step.

pastedimage1674158288256v4.png

  • Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User 2024 Season 2 on at
    RE: Plugin Development - Firing during Plugin Profiled .

    Thank you for trying out the suggestions.

  • michaelgappa22 Profile Picture
    michaelgappa22 5 on at
    RE: Plugin Development - Firing during Plugin Profiled .

    Thank you very much, I changed plugin step to parentcustomerid and it worked. Also, the tracing service is very helpful. I also didn't know that about plugin registration. I'll keep all that in mind in the future.

  • Verified answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User 2024 Season 2 on at
    RE: Plugin Development - Firing during Plugin Profiled .

    Hi,

    Please below condition:

    pastedimage1674191149848v1.png

    Based on Plugin Registration it seems that you have selected accountId but in condition you checked the "parentcustomerid".

    Also, to debug plugin use Tracing Service Object as described in below link:
    https://mscrmwithramandeep.home.blog/debugging-using-tracing-service-in-dynamics-365-ce/

    This object will help you check your code line by line.

    One more point, when we run plugin on Update then you will not get all the fields in target. It will only pass the fields that are changed during Action.


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans