Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Need to retrieve Related Entity Info using Plugin

(0) ShareShare
ReportReport
Posted on by 161

Hello,

I'm kind of new to Dynamics. 

I would like to create a CRM plugin.  

There are Primary 'Customer' entity and 'Request' entity. Relationship is 1:N

The lookup field is : customerinfoid

How do I retrieve 'Request' entity fields' information in my plugin.  

Would you please provide me with some examples how to achieve this?

Thanks

  • Verified answer
    cloflyMao Profile Picture
    25,208 on at
    RE: Need to retrieve Related Entity Info using Plugin

    Hi Kahspot,

    My thought is that when the plugin executes in Customer context, we could retrieve Request entity records which has same parent customer record by QueryExpression class,

    then get Request entity fields' information from retrieved result.

    e.g: My demo is based on a default 1:N relationship: Account <-> Contact(parentcustomerid).

    When I update an account record, retrive all associated contact records, populate description field of context entity with retrieved fullname and emailaddress.

    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using System;
    using System.Text;
    
    namespace DynamicsPlugins
    {
        public class plugin2 : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                IOrganizationServiceFactory servicefactory =
                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service =
                    servicefactory.CreateOrganizationService(context.UserId);
    
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
    
                    Entity entity = (Entity)context.InputParameters["Target"];
                    // Make plugin only excutes for Account entity
                    if (entity.LogicalName != "account")
                    {
                        return;
                    }
                    // Prevent infinite loop
                    if (context.Depth > 1)
                    {
                        return;
                    }
    
                    try
                    {
                        QueryExpression query = new QueryExpression("contact");
                        query.ColumnSet = new ColumnSet("fullname", "emailaddress1");
                        query.Criteria.AddCondition("parentcustomerid", ConditionOperator.Equal, entity.Id);
                        EntityCollection collection = service.RetrieveMultiple(query);
                        if (collection.Entities.Count > 0)
                        {
    
                            StringBuilder sb = new StringBuilder();
                            sb.Append("Associated contacts: \n");
                            foreach (var item in collection.Entities)
                            {                           
                                sb.Append("Name: "   item.Attributes["fullname"]   "\n");
                                
                                if (item.Contains("emailaddress1") && !string.IsNullOrEmpty(item.GetAttributeValue("emailaddress1")))
                                {
                                    sb.Append("Email: "   item.Attributes["emailaddress1"]   "\n");
                                }
                            }
    
                            Entity newEntity = new Entity("account");
                            newEntity.Id = entity.Id;
                            newEntity.Attributes["description"] = sb.ToString();
                            service.Update(newEntity);
    
                        }
                    }
    
                    catch (Exception ex)
                    {
                        tracingService.Trace("Error of Plugin: {0}", ex.ToString());
                        throw;
                    }
    
                }
    
            }
        }
    }
    

    Register new step to run plugin:

    pastedimage1585880921485v1.png

    Result:

    pastedimage1585880994869v2.png

    You can replace entity name and fields parameter with your owns for test.

    Regards,

    Clofly

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 82 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans