DYNAMICS 365 HOW TO DYNAMICALLY GET THE FIELD NAME THAT TRIGGERED EVENT ON FORM

Let's take the scenario, where a user has to choose Department 1, Department 2 and Department 3 on a form for escalation purpose. Each department has its own enabled and required fields where a user in the department has to fill before saving.

In this situation, when you have multiple attributes on the form that operate similarly, and if needed, as a best practice, they should fire the same JavaScript function.

In order to achieve this, you need to dynamically get the attribute name of the field being changed (source of the event) in the function to know which dependent fields should be enabled and set as required.

Here comes the role of the getEventSource() function by calling it as follows formContext.getEventSource().getName(); that will retrieve the schema name of the field being changed, and this way, you can achieve some work based on it as per the below function.

function manageDepartmentsBasedOnChangedField(executionContext) {
var formContext = executionContext.getFormContext();
var changedFieldName = formContext.getEventSource().getName();
if(changedFieldName == "<schemanamefield1>") {
// Enable and set required fields for department 1 and do something
}
else if(changedFieldName == "<schemanamefield2>") {
// Enable and set required other fields for department 2 and do something else
}
else if(changedFieldName == "<schemanamefield3>") {
// Enable and set required other fields for department 3 and do something else
}
}

Bonus Tips:
  • You can get the field value using the same function as follows var changedFieldValue = formContext.getEventSource().getValue();
  • Since it is a HTML format, you can add different element to display the details (table, ordered list...) and with different style
  • You can get the object for other events as well like onstagechange of a BPF, ongriddataload, tabstatechange... for a full list of those events, check this docs link for more details


Hope This Helps!

Comments

Popular posts from this blog

DYNAMICS 365 FILTER LOOKUP FIELD BASED ON N:N RELATIONSHIP

DYNAMICS 365 LEVEL UP BROWSER EXTENSION - PART 1 - FORMS

DYNAMICS 365 REFERENCE PANEL