Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Get parent fields when using Retrieve or RetrieveMultiple

(0) ShareShare
ReportReport
Posted on by

Hi

Using Late Bounding to Retrieve a record in C#, I want to see if it is possible to get parent fields of that record in single request. For example, consider a quote record which has parents like 'Customer' or 'Opportunity', How can I Use service.Retrieve  to get Customer`s (Say it is Contact) custom field value ?

var quote = service.Retrieve("quote", quiteId, new ColumnSet(true));


// Get a custom field of Quote Customer Record, Say her website address. 

*This post is locked for comments

  • Suggested answer
    Vahid Samimi Profile Picture
    Vahid Samimi 210 on at
    RE: Get parent fields when using Retrieve or RetrieveMultiple

    If you want to retrive column from another entity in link entity you must use entity alias name and use aliasvalue variable for get it:

    decimal aggregate1 = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;

  • Aric Levin Profile Picture
    Aric Levin 30,188 Moderator on at
    RE: Get parent fields when using Retrieve or RetrieveMultiple

    Hi Babak,

    That is correct.

    You can also add multiple linked entities if you need.

  • RE: Get parent fields when using Retrieve or RetrieveMultiple

    Thanks.  So int QueryExpression with LinkEntities  specified, I can set the columns of opportunity I want to retrieve ?

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 Moderator on at
    RE: Get parent fields when using Retrieve or RetrieveMultiple

    You can use a FetchXml or linked entities. This will return using RetrieveMultiple, but you can still get a single result.

    // Put this in a string variable (Field Names are only examples):

    string fetchXml - "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>

     <entity name='quote'>

       <attribute name='name' />

       <attribute name='customerid' />

       <attribute name='opportunityid' />

       <filter type='and'>

         <condition attribute='quoteid' operator='eq' value='{A73F6DF6-7F85-E711-80D8-0050569B3FA1}' />

       </filter>

       <link-entity name='opportunity' from='opportunityid' to='opportunityid' visible='false' link-type='outer' alias='a_c6cc66c9ea83e71180d80050569b3fa1'>

         <attribute name='title' />

       </link-entity>

     </entity>

    </fetch>"

    EntityCollection totalCount = service.RetrieveMultiple(new FetchExpression(fetchXml));

    if (totalCount.Entities.Count > 0)

    {

     // Get attribute value from linked entity

    }

    // Option 2 use linked entities

               QueryExpression query = new QueryExpression

               {

                   ColumnSet = new ColumnSet(true),

                   EntityName = "quote",

                   LinkEntities =

                   {

                       new LinkEntity()

                       {

                          LinkToEntityName = "opportunity",

                          LinkToAttributeName = "opportunityid",

                          LinkFromEntityName = "quote",

                          LinkFromAttributeName = "opportunityid",

                          LinkCriteria =

                          {

                               Conditions =

                               {

                                   new ConditionExpression("quoteid", ConditionOperator.Equal, quoteid),

                               }

                          },

                       }

                   }

               };

    EntityCollection entities = service.RetrieveMultiple(query);

  • RE: Get parent fields when using Retrieve or RetrieveMultiple

    I want it either in plugin or other app that uses Dynamics SDK.

  • RE: Get parent fields when using Retrieve or RetrieveMultiple

    Thanks For your response Aric. But I dont want Customer Name, for example, If it is contact, I want her Web Site field.

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 Moderator on at
    RE: Get parent fields when using Retrieve or RetrieveMultiple

    Entity quote = service.Retrieve("quote", quoteId, new ColumnSet(true));

    Guid customerId = ((EntityReference)(quote["customerid"])).Id;

    Guid opportunityId = ((EntityReference)(quote["opportunityid"])).Id;

    You can also use Name and LogicalName to receive the value of the customer field and Name

    string customerName = ((EntityReference)(quote["customerid"])).Name;

    string customerType =  ((EntityReference)(quote["customerid"])).LogicalName;

    Hope this helps.

  • Friyank Profile Picture
    Friyank 944 on at
    RE: Get parent fields when using Retrieve or RetrieveMultiple

    you want it in plugin or some other application ?

  • RE: Get parent fields when using Retrieve or RetrieveMultiple

    Thanks for your response but it is not what I want, I want to get a field of  Quote customer by using single request.

  • Suggested answer
    Friyank Profile Picture
    Friyank 944 on at
    RE: Get parent fields when using Retrieve or RetrieveMultiple
     public void Execute(IServiceProvider serviceProvider)
            {
                ITracingService traceObj = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                IPluginExecutionContext pluginContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                try
                {
                    traceObj.Trace("Plugin Starts");
                    IOrganizationService _service = serviceFactory.CreateOrganizationService(pluginContext.UserId);
                    if (pluginContext.Depth > 1)
                        return;
                    Entity ExecutingEntity = (Entity)pluginContext.InputParameters["Target"];
                    if (ExecutingEntity.LogicalName != "contact") // Entity Name needed 
                        return;
    
                    Entity postImage = (Entity)pluginContext.PostEntityImages["Image"];
                    string AccountId = ((EntityReference)postImage.Attributes["parentcustomerid"]).Id.ToString();
    
                    ExecutingEntity["description"] = AccountId;
    
                    _service.Update(ExecutingEntity);
                    traceObj.Trace("Plugin Ends With No Excepption");
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                }
                catch (Exception ex)
                {
                    traceObj.Trace("Plugin Exception : {0}", ex.ToString());
                    traceObj.Trace("Plugin Ends With No Excepption");
                }
            }

    Above Code fetch Account or company of contact 

    hope this help you

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 231,436 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans