web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

Dynamically get the source of an event triggered on a form

Charles Abi Khirs Profile Picture Charles Abi Khirs 3,569

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 in order 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.

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:

1. You can get the field value using the same function as follows var changedFieldValue = formContext.getEventSource().getValue();

2. 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!


This was originally posted here.

Comments

*This post is locked for comments