Advertisements

{Dynamics 365/ CRM} Access your cell values easily during on-change event of field in Dynamics CRM editable sub-grid.

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)
{
debugger;
// 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)

Advertisements

About Debajit
I am a Dynamics CRM Most Valuable Professional (MVP) with 10 years of experience in Microsoft .NET Technologies and 7 years of dedicated experience in Microsoft Dynamics CRM. I have worked with companies like Microsoft, SanDisk, PwC, TMF Group and have extensive experience of implementing complex CRM solutions from both offshore and client side. Currently the face of XrmForYou.com with significant experience in delivering corporate training on Dynamics CRM and have already delivered multiple projects to client through XrmForYou.com Author of multiple tools on codeplex including the 'Role Based Views' and 'CRM-Sharepoint Metadata manager & Attachment Extractor' which are available for commercial use under XrmForYou.com For consulting/ training, drop me a note at info@xrmforyou.com or visit our website www.xrmforyou.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: