on change event in dynamics 365/ CDS editable sub-grid.

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com
It’s been some time that I wrote a blog. Been busy with the deliverable and tied up in loads of personal stuffs. However finally took out the time to pen down this small post which you might find useful.
So here I was in business as usual, writing some scripts on change of cell values in editable sub-grid. Just to mock up the requirement, the client had the following schema.

  • Parent entity
  • Child Entity. (bearing N:1 relationship with the Parent Entity)
  • There is an editable sub-grid of Child entity on the form of the Parent Entity
  • The Child Entity has three fields being shown on the editable grid
    • Name
    • Field 1
    • Field 2
  • On Change of Name in any of the rows of the sub-grid, the Field 1 should be set required as well as some default values should be shown in the field

You might be thinking, won’t a business rule do this. Yes certainly. And if your requirement is just as simple, then always go ahead with business rules. However for my client, the field values were based on some business logic and required to be set using complex operators which is unfortunately not possible using business rules.
And if you are stuck in the same situation, please read on.
Pretty simple isn’t it. So let’s first register on-change of the ‘Name’ field of the sub-grid. You might already be knowing this on how to do it, but there may be a small twist in the tale. So keep reading Smile

  • Open the form of the Parent Entity.
  • Highlight the sub-grid of the Child Entity on the Parent Entity and Click on Change Properties
  • In the pop-up dialog, go the Events Tab.
image
  • In the form properties, upload the webresource containing the script for the on-change of the name field.
  • In the Event Handlers section, select Field = “Name”
image

Click on Add, and then select the appropriate library. Put the function name which should be called on the on-change.
image
Before you close it out just like another mundane operation, please make sure “Pass Execution context as first parameter” is checked. We are going to need it a lot.
Save & Publish the form changes.
Now coming to the code. I have been surprised that there is very less of examples to show how to access field values on-change of cell values in editable sub-grid. So decided to pen this down.
Now coming to the function. Lets first see what is written. Off-course, your function is going to look lot more complex. However the basic framework to access the attributes and get/ set field values and other stuffs still remains the same.

function NameChange(eContext)
{
// get the attribute which fired the onchange.
var nameAttr = eContext.getEventSource();
    // get the container for the attribute.
var attrParent = nameAttr.getParent();
    // get the value of the name.
var name = nameAttr.getValue();
    // var field1 Attribute
var field1 = attrParent.attributes.get("new_field1");
    field1.setValue("<default value for field 1>");
    // set field as mandatory.
field1.setRequiredLevel("required");
}

Ok. so let’s analyze bit by bit.
The first is to get the attribute which changed. the below line does exactly the same. And this is the reason why you need to check
var nameAttr = eContext.getEventSource();
The next is to get the gridRow object which contains the Name Attribute.
 var attrParent = nameAttr.getParent();

Although not used much, the getParent() is documented by Microsoft the Xrm.Page.ui.client control reference. Although I need to make sure whether calling it on attribute is supported or not. I will update on this blog only after my findings.
The remaining of the code is self explanatory. And you can see in this way, how easily you can access the attributes of the row which has been affected. And below is how it looks after I change the Name field in my sub-grid control.
image
Simple isn’t it.
Surprisingly there are very less examples on this. The other way if not this would be iterate through the selectedRows of the grid each time and then do a switch-case on each attribute of the row to achieve the desired result.
Hope it helps
Debajit Dutta
(Dynamics MVP)
(Visit our products page – http://www.xrmforyou.com/products-1.html to know more about our offerings)


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

6 thoughts on “on change event in dynamics 365/ CDS editable sub-grid.”

  1. Will this Javascript function also call if we click on ‘Activate’ button of subgrid and change the status of selected records ?

    1. Hi Milan
      Thanks for reading my blog. As far I understand when you select a row in subgrid and click on Activate button, CRM fires a server side event to change the status. I really doubt whether registering in onchange of the status field would trigger this. Still I will give a check and let you know of this.
      -Debajit

      1. Yes You are right. Its not calling as its server side.
        Do you know how we can get selected records ids and can pass to a plugin or an action on click of Activate button ?

        1. In that case you need to put your own custom ribbon button for activate, hide the OOB one and then handle the click event of the button where you can get the selected record id’s and call an action.

          1. Measn its not possible with OOB Activate button. We can’t use custom button because OOB button gives all status reason window to select any status for all selected records, something not possible with custom button.

Comments are closed.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading