How to make a list column read-only based on another field using onCellEdit?Body:

AbW
Tera Contributor
Hi everyone,
I am new to ServiceNow and trying to write an onCellEdit client script.
My requirement is: If the Category of a record is "Inquiry / Help", I want to prevent users from changing the State column directly from the list view (make it read-only/uneditable).
I have already successfully implemented this using ACLs and Data Policies, but for this specific use case, I specifically need to achieve it using onCellEdit.
Could someone please show me a simple, beginner-friendly example of how to write this onCellEdit script?
Thank you!
4 REPLIES 4

Sanjay191
Kilo Patron

Hi @AbW 

Please refer the below thread i hope it will work for you!


https://www.servicenow.com/community/developer-blog/learn-servicenow-oncelledit-client-script-servic... 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You

Ankur Bawiskar
Tera Patron

@AbW 

you can use before update BR and validate this and abort the update

💡 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 @AbW 

 

Can you try this once - onCellEdit on state field. For me first choice is always ACL here.

 

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var saveAndClose = true;
var gr = new GlideRecord('incident');
gr.addQuery('sys_id', 'IN', sysIDs);
gr.addQuery('category', 'inquiry');
gr.query();
if (gr.hasNext()) {
alert("State cannot be changed from the list view for Inquiry / Help records.");
saveAndClose = false;
}
callback(saveAndClose);
}

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

Deepak89
Tera Expert

@AbW 

try this 

 

On cell edit on state 
client script :

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
 
 //Type appropriate comment here, and begin script below

var ga= new GlideAjax("DB_GroupType");
ga.addParam('sysparm_name','getCategory');
ga.addParam("sysparm_id",sysIDs);
ga.getXMLAnswer(output);

function output(response){
    alert(response);


    if(response== "true"){
        alert("you can not modify the sate for this category");
        callback(false);
       
    } else{
       
        callback(true);
    }
}

}
 
SI :
     getCategory: function() {
                var id = this.getParameter("sysparm_id");
                var gr = new GlideRecord("incident");
                if (gr.get(id)) {

                        if (gr.category == "inquiry") {
                            return true;
                        } else {
                            return false;
                        }
                    }

                },
 
let me know if work and accept it