Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

Subgrid OnChange event for JavaScript

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

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: Subgrid OnChange event for JavaScript

    There are four events of grid:

    OnChange

    OnLoad

    OnRecordSelect

    OnSave

    Hope it clears your doubt.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Subgrid OnChange event for JavaScript

    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
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Subgrid OnChange event for JavaScript

    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

  • CRMDev18 Profile Picture
    CRMDev18 55 on at
    RE: Subgrid OnChange event for JavaScript

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

  • CRMDev18 Profile Picture
    CRMDev18 55 on at
    RE: Subgrid OnChange event for JavaScript

    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
  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Subgrid OnChange event for JavaScript

    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.

  • gdas Profile Picture
    gdas 50,083 on at
    RE: Subgrid OnChange event for JavaScript

    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);
    
    }


  • CRMDev18 Profile Picture
    CRMDev18 55 on at
    RE: Subgrid OnChange event for JavaScript

    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

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Subgrid OnChange event for JavaScript

    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

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,532 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,501 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans