onChange

onChange Client Scripts execute whenever a particular selected field's value is modified. Additionally, when the form first loads, this technically constitutes a change event, insofar as it triggers onChange scripts. However, by default, onChange Client Scripts include a condition block which returns to halt execution of the rest of the script if the variable isLoading is true (meaning that the change is the result of the form loading, not of a manual change.

By simply removing the isLoading condition, we can cause an onChange script to execute as though it were an onLoad script as well as onChange.

There's another condition under which the script is halted that you might notice in the screenshot above: If the new value is blank. This is another scenario that the default Client Script behavior precludes, but which you may want to un-preclude, as it were. That is, you may want to perform some action when a field is changed to a blank value, in which case you might want to override that functionality as well.

Consider an example where the business would like to employ some new functionality like so:

  • Whenever an Incident is assigned to a group which has a manager, and the assigned_to field is empty, the manager of that group should be set as the assignee for the Incident.
  • If the assignment group field is cleared out, the assigned_to field should also be cleared out.

For this, we might use an onChange Client Script like so:

  • Name: Set Assignee When Assignment Group Changed
  • Table: Incident [incident]
  • Type: onChange
  • Field name: Assignment group
Note: We set the field that triggers this Client Script to Assignment group, because that's the field which we'll be watching for a change (not the field that we'll be modifying).
function onChange(control, oldValue, newValue, isLoading,    isTemplate) { 
if (isLoading) {
return;
}
else if (newValue == '') {
g_form.setValue('assigned_to', '');
}
else {
g_form.getReference('assignment_group', assigmentGroupCallBack);
}
}
function assigmentGroupCallBack(gr) {
g_form.setValue('assigned_to', gr.getValue('manager'));
}

On line 2, you might notice that we've removed the condition that would return if the new value is blank. This is because we want to perform an action (clearing out the Assigned to [assigned_to] field when the value is changed to blank. Instead, we have the same condition on line 5, whereafter instead of returning, we blank out the assigned_to field value.

Next, on line 9, we call the getReference() method, and pass in both the field name (assigned_to), and a reference to our callback function (declared on line 12). This causes the lookup from the server to run asynchronously, limiting the impact on client-side performance.

On line 13, inside our callback function, we then call the setValue() method of the g_form client-side API, and set the assigned_to field to the value of the manager field on the assignment group record that was passed into our callback function.

There is one way in which this could be improved from a client-side performance perspective. When we get the value of the Manager field in our callback function on line 13, we get a sys_id. We then use this to set the value of the Assigned to field. When we set this field with a sys_id, the system has to perform a lookup on the reference table to retrieve the display value. If we used something like a GlideAjax script or another query to pull the display value of the manager field, we could specify that as well when setting the value using g_form, and that would prevent the lookup from having to retrieve the display value for the Assigned to field (the manager's name).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.154.18