Advertisements
Home > CRM, Dynamics 365 > Dynamics 365 – Capture events from Editable subgrid and set main form fields

Dynamics 365 – Capture events from Editable subgrid and set main form fields

To answer one of the queries posted on my blog, in this article I am going to detail, capturing the events from Editable sub grid and Get/Set the form fields.

To simplify the explanation, I am taking below use case

  • On Account from configure ‘Contacts’ editable sub grid
  • On change of ‘Business Phone’ from ‘Contact’ sub grid
    • Read the new ‘Business Phone’ value
  • SubGridEvent_3
    • Set the ‘Telephone’ field on Account form with the Contacts ‘Business Phone’

SubGridEvent_4

below are the execution steps

Add ‘Editable Grid’ on Account Form:

  • Add a ‘Contact’ sub grid on the Account form (You can leverage OOB Contacts grid as well)
  • To convert read only grid to Editable grid, Double click on ‘Sub grid’ and go to ‘Controls’ -> Add Control
  • Choose ‘Editable Grid’ option from the list

Add Sub Grid

  • Select ‘Web’ checkbox

Add Sub Grid_1

  • Click ‘OK’

Register ‘onchange’ event on sub grid field:

  • Double click on ‘Sub grid’ and go to ‘Events’
  • Select the Field as ‘Business Phone’ and the Event as ‘OnChange’

SubGridEvent_2

  • Add click ‘+Add’ to add your Jscript function name
    • Function : businessPhoneOnChange
  • Make sure you select the ‘Pass execution context as first parameter’ checkbox

SubGridEvent_1

  • Save and Publish the form

JScript function:

function businessPhoneOnChange(execContext) {
try {
var formContext = execContext.getFormContext();

// Read the changed Business Phone’ value from sub grid
var entityObject = formContext.getData().getEntity();
var contactBusinessPhone = entityObject.attributes.getByName(“telephone1”).getValue();
// showAlert(“Business Phone – ” + contactBusinessPhone);

// Set the ‘Phone’ field on ‘Account’ form
parent.Xrm.Page.getAttribute(“telephone1”).setValue(contactBusinessPhone);
} catch (e) {
showAlert(“Error in businessPhoneOnChange – ” + e.description);
}
}

function showAlert(message) {
var alertStrings = { confirmButtonLabel: “Yes”, text: message };
var alertOptions = { height: 120, width: 400 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
function success(result) {
console.log(“Alert dialog closed”);
},
function (error) {
concole.log(error.message);
}
);
}

🙂

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: