Incident should be editable only for the members of the assignment group and watchlist. Caller/ Requested for users should have write access on "Additional comments" only. "ITIL" users should have access to "additional comments" and "work notes". Any
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2016 01:53 AM
Hi All,
I have above mentioned requirement in my project . I am not able to make all the fields read only if the conditions are met.
Below is the code that I wrote for the above requirement.
Any help would be appreciated .
function onLoad() {
//Type appropriate comment here, and begin script below
var assigned_group=g_form.getValue("assignment_group");
var person=g_user.userID;
var caller=g_form.getValue("caller_id");
var editable_fields = g_form.getEditableFields();//gets all the editable fields on the form
var watchlist=g_form.getValue('watch_list').toString().indexOf(person);//will get the index value of the person
var gr = new GlideRecord('sys_user_grmember');//check for user to be part of the assignment group
gr.addQuery('group',assigned_group);
gr.addQuery('user', person);
gr.query();
if(gr.next() || watchlist>-1){ }
else{
for (var y = 0; y < editable_fields.length; y++) {
g_form.setReadOnly(editable_fields[y], true);
g_form.setReadOnly("comments",true);
}//makes the fields readonly for the users not part of current assignment group
}
if(g_user.hasRole("itil")){
g_form.setReadOnly('comments',false);
g_form.setReadOnly('work_notes',false);//making the comment and work_note fields editable for the itil user
alert("person has itil role");
}
else if(person==caller){
g_form.setReadOnly('comments',false);//check if person is a caller and making comment field editable.
alert("person is a caller");
}
}
Thanks,
Pawan
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2016 10:25 PM
Hey Pawan,
If the data is not dynamically changing on the form, that is, the read-only or edit attribute does not change after the form is loaded, ACLs are recommended. ACLs are evaluated and enforced entirely on the server, making them much more secure than UI policies and client scripts.
You have to adjust/create "Write" ACL's on incident table and with the script validate your logic to return true or false.
I am pointing you to the links to get started. Give it a try on dev instance and let me know if you are blocked.
Using Access Control Rules - ServiceNow Wiki
Getting a User Object - ServiceNow Wiki
I hope this helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2016 10:52 PM
Thanks for your reply Pradeep. Actually one of the UI policy was overrideing my client script . Now its working fine.
Thanks & Regards,
Pawan Kumar