onCellEdit Help!!

SandeepKSingh
Kilo Sage

How can you allow a user to update the incident state but restrict them from closing it in the list view? 

2 ACCEPTED SOLUTIONS

Ravi Gaurav
Giga Sage
Giga Sage

Hi @SandeepKSingh 

Solution

You can create an OnCellEdit Client Script on state field that checks if the new state value is "Closed." If the user tries to close the incident by changing the state to "Closed," the script will block the update. If the new state is anything other than "Closed," the record update will proceed as normal.


function onCellEdit(tableName, record, columnName, oldValue, newValue, callback) {
    if (newValue === 'Closed') {
        // Prevent update if the state is being set to "Closed"
        callback(false);  // Pass 'false' to prevent update
    } else {
        // Allow update if the state is not being set to "Closed"
        callback(true);  // Pass 'true' to allow update
    }
}
--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

SriharshaYe
Kilo Sage

Hi @SandeepKSingh , 

write an on cell edit client script with the below code :

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var saveAndClose = true;
if(newValue == 7){
alert('Not allowed');
saveAndClose = false;
} else {
saveAndClose = true;
}
callback(saveAndClose);
}
 
the above code helps you in restricting to update the incident state  from closing it in the list view .Screenshot 2025-01-27 at 11.23.33 AM.pngScreenshot 2025-01-27 at 11.27.00 AM.png

 

If my solution is helpful , mark it as helpful .
 
0 Helpfuls
 

View solution in original post

8 REPLIES 8

PritamG
Mega Guru

you can achieve this by using a UI Policy or Business Rule

UI Policy/Client Script :

in the list view, create a UI Policy/Client Script to disable the "State" field when the value is set to "Closed."

Business Rule:

write a Before Update Business Rule on the incident table to restrict users from setting the "State" to "Closed" unless they have specific roles.

SriharshaYe
Kilo Sage

Hi @SandeepKSingh , 

write an on cell edit client script with the below code :

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var saveAndClose = true;
if(newValue == 7){
alert('Not allowed');
saveAndClose = false;
} else {
saveAndClose = true;
}
callback(saveAndClose);
}
 
the above code helps you in restricting to update the incident state  from closing it in the list view .Screenshot 2025-01-27 at 11.23.33 AM.pngScreenshot 2025-01-27 at 11.27.00 AM.png

 

If my solution is helpful , mark it as helpful .
 
0 Helpfuls
 

Thanks @Ravi Gaurav  and @SriharshaYe  it worked 🙂

vishwajeet5550
Mega Guru

onCellEdit client script use on the List view when we edit or change the field value using (g_list)

 if (control == 'state' && newValue == '7') { 
       
        g_form.addErrorMessage("You are not allowed to close incidents from the list view.");
        
        g_form.setValue('state', oldValue);
}

This approach only prevents users from closing incidents in the list view. If you also want to restrict closure in the form view, consider combining this with a UI Policy or Business Rule.