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 159

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
    cloflyMao 25,202 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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans