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

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

I have the same question (0)
  • Verified answer
    cloflyMao Profile Picture
    25,210 on at

    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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 130 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 117

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans