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 :
Microsoft Dynamics CRM (Archived)

How to add related entity records to subgrid?

(0) ShareShare
ReportReport
Posted on by

Hi there,


I have 2 entities. First is "Agreement", and second "Recipient". There is existing 1:N relationship, respectively (1 Agreement can have many Recipients).

On my Agreement form, I have subgrid of related Recipients.  I have a need to set (associate) Recipients on this subgrid with this Agreement entity when onLoad() handler is called on form. But, important, this needs to be before user clicks SAVE (in upper left corner).


recipient_5F00_subgrid.png

I was thinking I could call some JS to associate Recipient records with this Agreement, but I don't have GUID for Agreement yet.

I was also reading about setting fetchXML to this subgrid using JS, but don't know how do I come up with that fetchXML? 

What is recommended way to do this with JavaScript?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    If I understand the scenario correctly - a user will click New to create a record and you want to have a subgrid populated BEFORE the save of the record. As a principal of databases in general, you can't have records which look up to records that don't exist. You can't have line items on an order without an order - same thing here.

    With that said, you might be able to trick the user so they think that's what's happening. I would try to talk the customer out of it by explaining the problems, but if that doesn't work you can implement this convoluted (and unsupported) solution. You'd create a new agreement that holds the recipients until the primary agreement is saved, then you'd reassign the recipients and delete the temporary agreement.

    Create two subgrids on your form - all recipients (All) and related recipients (Related). On load, if it's an existing Agreement, hide the All grid and that's it. If it's a new Agreement:

    1) Create a new text field (or lookup on the Agreement)

    2) Create the temporary agreement record, and create all the Recipients you want but related to that record.

    3) Record the ID of the temporary agreement on the primary agreement (the field from step 1)

    4) Change the fetch xml on the All grid to only show records for the temporary agreement. This is unsupported (joshimandarblog.wordpress.com/.../crm-2015-applying-custom-fetchxml-to-a-subgrid-using-javascript)

    5) On save of the agreement, retrieve the recipients (using the ID from step 3) and set their agreement to the real agreement and delete the temporary agreement.

    6) Toggle the visible grids so the user sees the true grid.

    Like I said, it's a messy solution but I think a hackey way like that is the only way you'll be able to accomplish it.

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

     Aiden

  • Community Member Profile Picture
    on at

    Let's say I drop the requirement that this needs to be done while Agreement is not saved.

    How would I do this if my Agreement is saved (existing) record in CRM.

    I am trying to do it with JS but unable to get subgrid as JS object. This is my fucntion that gets called form onLoad()

    function loadDefaultRecipients(defaultRecipients){

       //get the subgrid

       //CRM 2015

       //var objSubGrid = document.getElementById("EnvelopeRecipientsTable");

       //CRM 2015 Update 1

       //var objSubGrid = window.parent.document.getElementById("EnvelopeRecipientsTable");

       //Legacy forms

       var objSubGrid = Xrm.Page.getControl("EnvelopeRecipientsTable");

       //CRM loads subgrid after form is loaded so when we are adding script on form load, need to wait until sub grid is loaded

       // that's why we are adding a delay

       if (objSubGrid == null) {

           setTimeout(function(){

                           loadDefaultRecipients(defaultRecipients);

                      }, 2000);

           return;

       } else {

           alert("Subgrid is loaded...");

           //when subgrid is loaded, get GUID

           var GUIDvalue = Xrm.Page.data.entity.getId();

           //Create FetchXML for sub grid to filter records based on GUID

           var FetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +

                             "<entity name='cs_esignaturerecipient'>" +

                               "<attribute name='cs_routingorder' />" +

                               "<attribute name='cs_name' />" +

                               "<attribute name='cs_email' />" +

                               "<attribute name='cs_signertype' />" +

                               "<order attribute='cs_routingorder' descending='false' />" +

                               "<filter type='or'>";

                                   for(var rec=0; rec<defaultRecipients.length; rec++){

               FetchXml+=              "<condition attribute='cs_email' operator='eq' value='" + defaultRecipients[rec] + "' />";

                                   }

               FetchXml+=      "</filter>" +

                             "</entity>" +

                           "</fetch>";

           //apply layout and filtered fetchXML

           objSubGrid.control.SetParameter("fetchXml", FetchXml);

           //Refresh grid to show filtered records only.

           objSubGrid.control.Refresh();

       }

    }

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    If the agreement is already saved, couldnt you just use a normal sub grid with the Related Records flag ticked? No need for JS then.

  • Community Member Profile Picture
    on at

    I get some Recipients loaded from configuration entity, through JS. And I need to add them to be "related" to this Agreement entity. That is why I would need JS.

  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    If you ditch the requirement about it being there before you save then you can do it more traditionally. You would just have a standard subgrids of related Recipients and create/associate the records when they save.

    Option 1) Write a plugin the runs on post create of the agreement. Query for the recipients from the config entity via the SDK and go from there (either create a copy for your agreement or associate it N:N - depends on your setup).

    Option 2) Run that same logic as javascript on the form save event. Need to make sure it's only coming from a save.

    I always go the plugin route because they'll run no matter how the record is created (through code), where the javascript only runs if you're a user in CRM.

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

     Aiden

  • Community Member Profile Picture
    on at

    Thank you Aiden. This answers my question! I will take the route with plugin as I have already written plugins before and I have short deadline.

    I suppose FetchXML is used only if I want to filter existing set of related records on subgrid.

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 March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans