can we restrict updates using onCellEdit client scripts or before business rules

jagapyG
Mega Explorer

 can we restrict updates using onCellEdit client scripts or before business rules

5 REPLIES 5

GlideFather
Tera Patron

ahoy @jagapyG,

 

restrict how - by role or for specific field values, .....  Could you possibly share more details? 

_____
A community that tolerates cheaters and ignores collusion isn't a community . RIP

Ankur Bawiskar
Tera Patron

@jagapyG 

If your requirement is not allow user to update from list then

1) use list_edit ACL for that field and block list edit -> this works for only native and not workspace

OR

2) use before update business rule and stop the update using current.setAbortAction(true) -> should work in native + workspace

OR

3) use onCell edit which will work only in native, something like this if you don't want user to set 6 or 7 or 9 state from list

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {

    var saveAndClose = true;
    if (newValue == '6' || newValue == '7' || newValue == '9') {
        alert('Not allowed');
        saveAndClose = false;
    } else {
        saveAndClose = true;
    }
    callback(saveAndClose);

}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Tanushree Maiti
Tera Patron

Hi @jagapyG 

 

Both the method will work .

 

1. Before Business Rule (Recommended) : It is the most secure and comprehensive method. It runs on the server, blocks unauthorized updates regardless of where they come from (form/list view, API, or background script)

Sample script:  (for Before Update BR)

if (condition_is_met) {
    gs.addErrorMessage('You are not allowed to update this record.');
    current.setAbortAction(true);
}

 

2. onCellEdit Client Script : runs  in the browser when a user attempts to double-click and edit a record directly from a List view

 

Sample code:

function onCellEdit(sysIDs, tables, oldValues, newValue, callback) {
    var allowUpdate = false; 
if (condition_is_met) { allowUpdate = true; } else { alert('Updates from this list are restricted.'); allowUpdate = false; }
callback(allowUpdate); }
 

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

pr8172510
Tera Guru

Hi @jagapyG ,

Before Business Rule 
Use when You need to enforce rules regardless of how the record is updated

(function executeRule(current, previous) {
    
    // Block if certain condition is met
    if (current.state == 7 && previous.state != 7) {
        gs.addErrorMessage('Cannot close this record - conditions not met');
        current.setAbortAction(true);
    }
    
})(current, previous);

  

onCellEdit Client Script
Use whenYou want to block specific values from being edited directly in the list

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
    
    var allowed = true;
    
    // Block if trying to set status to 'Closed'
    if (table == 'incident' && newValue == '7') {
        alert('Cannot set status to Closed from list view');
        allowed = false;
    }
    
    callback(allowed);
}