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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Subgrid OnChange event for JavaScript

(1) ShareShare
ReportReport
Posted on by 55

I cant find OnDelete/Change-Events in Dynamics CRM subgrids.

In the related subgrid, the user will delete record. After this, there is a plugin for updating the parent lead entity. I want to refresh the parent form after successful update. Is it possible?

CRM-Version: Microsoft Dynamics 365 (8.2.2)

Thanks

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    after deleted the record, the gridview will refresh automatically , so use below method to register your function via addonload

    msdn.microsoft.com/.../dn932126.aspx

  • CRMDev18 Profile Picture
    55 on at

    Thanks,

    I try this

    onLoad() {

               var contactsSubgrid = Xrm.Page.getControl("Contact");

               var myContactsGridOnloadFunction = function ()

               {

                 Xrm.Page.data.refresh(false);

               };

               contactsSubgrid.addOnLoad(myContactsGridOnloadFunction);

    }

    ...

    but this will cause an endless loop

  • gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Please try with this . Hope this helps.

    function Onload() {
        var intervalId = window.setInterval(
          function () {
              if (Xrm.Page.ui.controls.get("subgridname") != null)
              {
                  window.clearInterval(intervalId);
                  var subgrid = Xrm.Page.ui.controls.get("subgridname");
    
                  subgrid.refresh();
              }
          }, 500);
    
    }


  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    during onload of the form , go get the total records of that subgrid and store into a global variable.

    after record get deleted, get the total records of that subgrid again and compare to the global variable , if the result is not same only refresh.

  • CRMDev18 Profile Picture
    55 on at

    Thanks, good idea but the

    subgrid.OnLoad runs before the user open the dialog to add or delete subgrid items (total count = -1)
    After successful adding/delete of subgrid items the subgrid.OnLoad will not run
  • CRMDev18 Profile Picture
    55 on at

    ok, but this is a refresh of the subgrid, I want to refresh the parent form after create/delete subgrid elements

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    the below is my code

    //copy from here

    var totalrecordonload;

    function onLoad()
    {

    try {

    //setting timeout beacuse subgid take some time to load after the form is loaded

    setTimeout(function () {

    //validating to check if the sub grid is present on the form

    if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("Contacts") != null && Xrm.Page.getControl("Contacts") != undefined) {

    //stores the row count of subgrid on load event of CRM Form

    totalrecordonload = Xrm.Page.getControl("Contacts").getGrid().getTotalRecordCount();

    //registering refreshform function onload event of subgrid

    Xrm.Page.getControl("Contacts").addOnLoad(SetSubgridOnLoad);


    }

    }, 5000);

    } catch (e)
    {

    Xrm.Utility.alertDialog(functionName + "Error: " + (e.message || e.description));

    }




    }

    function SetSubgridOnLoad()
    {
    try {

    //setting timeout beacuse subgid take some time to load after the form is loaded

    setTimeout(function () {

    //validating to check if the sub grid is present on the form

    if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("Contacts") != null && Xrm.Page.getControl("Contacts") != undefined) {

    //stores the row count of subgrid on load event of CRM Form

    var aftertotal = Xrm.Page.getControl("Contacts").getGrid().getTotalRecordCount();


    if (totalrecordonload != aftertotal )
    {

    alert("Total Record Onload " + totalrecordonload + " after deleted " + aftertotal);
    totalrecordonload = aftertotal ;
    Xrm.Page.data.refresh(false);

    }
    else

    {

    }


    }

    }, 5000);

    } catch (e)
    {

    Xrm.Utility.alertDialog(functionName + "Error: " + (e.message || e.description));

    }

    }

    //copy until here

    you can take a look on below video, how it works in my environment.

    https://youtu.be/8pqIprEmQ7c

  • Community Member Profile Picture
    on at

    Was looking for this exact functionality for our Dynamics 365 v9.x on premise environment.  Thank you "Wei Jie Fun", this worked perfectly!

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    7,755 on at

    There are four events of grid:

    OnChange

    OnLoad

    OnRecordSelect

    OnSave

    Hope it clears your doubt.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans