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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Updating associated records

(0) ShareShare
ReportReport
Posted on by

I have a customization that contains some custom entities that are added in a subgrid to an Order.  When the Order is saved I want to update the status (custom field) in each of these associated entities.

I've created a C# plug-in for the Order, but now I want to get the associated records and run a "for each" to update each of the subgrid records.  I can't find an example to show me how to get a collection of the subgrid records.  Anyone have one?

*This post is locked for comments

I have the same question (0)
  • Royal King Profile Picture
    27,686 on at
    RE: Updating associated records

    There is codeplex custom workflow that will do this job for you without writing your own plugin.

    You can download this workflow and documentation from below url

    crm2011distributewf.codeplex.com

    Build fetchxml using advanced find that gets all the sub records for the selected order . Once you see right result in advanced find export that fetchxml and use that fetchxml with replaing the guid with right value in your plugin to retrieve all related records.

    service.RetrieveMultiple(new FetchExpression(fetchxmlquery ));

  • Suggested answer
    Deepesh161 Profile Picture
    6,317 on at
    RE: Updating associated records

    This should help you:

    community.dynamics.com/.../plugin-to-update-children-records-when-parent-record-is-upadated.aspx

  • Suggested answer
    Hosk Profile Picture
    on at
    RE: Updating associated records

    Can't you use the same search criteria as the subgrid is using to filter which records are displayed in the subgrid and retrieve those records and update them.

    you could do a query expression as show in the link

    msdn.microsoft.com/.../gg509034.aspx

    or you could do a linq query to retrieve those records.

    You can retrieve the id's of items in the subgrid using Javascript and then with the GUID retrieve and update the individual records

    lakshmanindian.wordpress.com/.../retrieve-subgrid-rows-in-crm-2011-using-jscript

  • Community Member Profile Picture
    on at
    RE: Updating associated records

    Thanks all:

    Chitra - I am reluctant to add other solutions that I have to install at my client, but I'm keeping this in reserve.

    Ben - I already have the javascript for getting the records within a subgrid, but got stalled when I had to update the custom entity records.  Couldn't find the code for that.  Also, there was an issue (since I'm updating an Option Set field) in finding the correct option set field since it only returned the value, not the text.

    Deepesh - working through this, but unlike the example, I don't have a field within my child entity that contains the id of the parent entity.  (In the example, they use the parentcustomerid for the relation with the query.  My custom entity (new_prokey) does not have a "parentsalesorderid" field, nor would it.)  I'm trying to determine how to get beyond that.

    Thanks all - but I'm still working through the various choices to find one that works for my solution.

  • Community Member Profile Picture
    on at
    RE: Updating associated records

    What is the query behind the subgrid?

    Surely it is displaying a view of associated records that have a relationship to the parent record? Or is this an N:N relationship (in which case it does not seem to make sense to update it based on only one related Order)

    Is there really no relationship at all between new_prokey and salesorder?

  • Community Member Profile Picture
    on at
    RE: Updating associated records

    There IS a relationship, which includes a schema value in the relationship view.  However, there is no specific field within the field list that contains that orderid.  

    I am looking to update many fields within the custom entity.  For example, there is a Customer field within the Order.  I want to save that field in the custom entity (currentcustid) and also create a history record for that entity (in another custom entity) that shows what has happened to that custom entity (i.e. - it was attached to an order which had a specific customer).

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Updating associated records

    Did you try query expression or query by attributes to retrieve the related records of Order. Query by attribute would be simple to use in such scenarios and should go like -

    QueryByAttribute retrieveRelatedRecords = new QueryByAttribute(<related entity name>);

    retrieveRelatedRecords.ColumnSet = new ColumnSet("col1", "col2");

    retrieveRelatedRecords.AddAttributeValue(<order lookup name>, currentOrderId);

    EntityCollection relatedRecords = service.RetrieveMultiple(retrieveRelatedRecords);

    Now you can have 'foreach' on this entity collection to execute update request for modifying the custom status of each entity.

    Same applies to other entities.

    Hope this helps.

  • Verified answer
    Nuno Profile Picture
    2,005 on at
    RE: Updating associated records

    Hi,

    You can use the LoadProperty method of OrganizationServiceContext, that loads the related entity collection for the specified relationshp.

    msdn.microsoft.com/.../gg307150.aspx

    Then you can use the RelatedEntities property of Entity class to get a collection of all related entities.

    msdn.microsoft.com/.../microsoft.xrm.sdk.entity.relatedentities.aspx

    For example

    Entity account = crm.OrgService.CreateQuery("account").Where(x => (String)x["name"] == "NAME").FirstOrDefault();

    crm.OrgService.LoadProperty(account, new Relationship("incident_customer_accounts"));

    RelatedEntityCollection relatedEntities = account.RelatedEntities; foreach (KeyValuePair<Relationship, EntityCollection> entry in relatedEntities) { // go through all related entities if (entry.Key.SchemaName == "incident_customer_accounts") // related incident { // print the name of each related incident entry.Value.Entities.ToList().ForEach(x => Console.WriteLine(x.Attributes["title"])); } }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans