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

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/

Let me have a Try and I will get Back to you

yuvarajkate
Giga Guru

Use this Script:

(function executeRule(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === oldValue) return; // Skip if loading or no change

    if (control === 'state') { // Check if editing "state"
        if (newValue == '7') { // Restrict "Closed" state
            g_form.addErrorMessage('Cannot set state to Closed.');
            g_form.setValue('state', oldValue); // Revert value
            return false;
        }
    }
})(control, oldValue, newValue, isLoading, isTemplate);

I guess you  have just copy pasted from chatGPT.. I asked for onCellEdit and you are giving onChange.. I am sorry buddy your code is of no Help.