onCellEdit client script in servicenow

0
2351

This type of client script runs when the list editor changes a cell value. Script runs when a particular field value on a list changes. Applies to all records selected.

In onCellEdit() client script we will get OOB parameters/Objects. Let me explain you each object in details.

Request you to watch it for better understanding.

 

A. sysIDs: It consist an array of the sys_ids for all items being edited.

B. table: The table of the items being edited.

C. oldValues: The old values of the cells being edited.

D. newValue: The new value for the cells being edited.

E. callback: A callback that continues the execution of any other related cell edit scripts.

If true is passed as a parameter, the other scripts are executed or the change is committed if there are no more scripts. If false is passed as a parameter, any further scripts are not executed and the change is not committed.

 Note: onCellEdit() scripts do not apply to List Widgets on homepages or dashboards. 

Now lets take a look on real word example

Real World Scenario

Requirement: Prevent state being edited from list view of incident if state marked as Resolved or Closed.

Solution: create onCellEdit client script and write below code.

function onCellEdit(sysIDs, table, oldValues, newValue, callback){
var saveAndClose = true;

if(newValue == 6) { //Resolved
alert("You cannot change the State to 'Resolved' from a list");
saveAndClose = false;
}
if(newValue == 7) { //Closed
alert("You cannot change the State to 'Closed' from a list");
saveAndClose = false;
}
callback(saveAndClose);
}

 

You can not call event from client side, Event will be call on server side only but you can call event in UI action.
Q. Which is executed first UI or client script?
UI Policies execute after Client Scripts. If there is conflicting logic between a Client Script and a UI Policy, the UI Policy logic applies.

Get Latest Update From Runjay Patel

We don’t spam! Read our privacy policy for more info.

LEAVE A REPLY

Please enter your comment!
Please enter your name here