web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
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

I have the same question (0)
  • Verified answer
    Pradeep Rai Profile Picture
    5,489 Moderator on at

    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.


  • michaelgappa22 Profile Picture
    5 on at

    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.

  • Pradeep Rai Profile Picture
    5,489 Moderator on at

    Thank you for trying out the suggestions.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 180 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 123

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans