UI Policy Script - ReadOnly fields

Kamran1
Kilo Guru

Hello Community,

I am trying to allow ITIL user to be able to edit incident due date. We have a UI policy in place which makes the due date mandatory or not mandatory based on contact type and status fields selected. However, when ITIL user selects a due date, and later on tries to change to a different due date, it gets greyed out. We have a UI policy script in place that restricts to change the due date as noted below. I want to edit the script so ITIL user can change due date once set to a different date. For example if contact type is phone and status is New or Active and similar other variations:

function onCondition() {

var isAdmin = g_user.hasRole('admin');

if (!isAdmin) {

g_form.setReadOnly('u_reported_by',true);

g_form.setReadOnly('caller_id',true);

g_form.setReadOnly('u_non_ad_user',true);

g_form.setReadOnly('location',true);

g_form.setReadOnly('u_due_date',true);

}

var isAllowedToUpdateLocation = g_user.hasRole('CW_UpdateIncidentLocation');

if(isAllowedToUpdateLocation){

g_form.setReadOnly('location',false);

g_form.setReadOnly('u_due_date',false);

}

}

Not experience in scripting, can anyone please provide few suggestions that I can play around with.

Thanks in advance!

1 ACCEPTED SOLUTION

She Sull
Giga Guru

Not sure if CW_UpdateIncidentLocation is an actual role on your instance, but if you just need itil and admin:



function onCondition() {



var isAdmin = g_user.hasRole('admin');


var isItil = g_user.hasRole('itil');



if (!isAdmin) {


g_form.setReadOnly('u_reported_by',true);


g_form.setReadOnly('caller_id',true);


g_form.setReadOnly('u_non_ad_user',true);


}



if(isItil){


g_form.setReadOnly('location',false);


g_form.setReadOnly('u_due_date',false);


}


}



Though you have to be careful with ui policy scripts because you need to ensure the fields are actually on the form and that there are no acls conflicting with your script. If Location and Due date are on the form and do not have any acl restricting them you don't need the isItil if statement.


View solution in original post

6 REPLIES 6

She Sull
Giga Guru

Not sure if CW_UpdateIncidentLocation is an actual role on your instance, but if you just need itil and admin:



function onCondition() {



var isAdmin = g_user.hasRole('admin');


var isItil = g_user.hasRole('itil');



if (!isAdmin) {


g_form.setReadOnly('u_reported_by',true);


g_form.setReadOnly('caller_id',true);


g_form.setReadOnly('u_non_ad_user',true);


}



if(isItil){


g_form.setReadOnly('location',false);


g_form.setReadOnly('u_due_date',false);


}


}



Though you have to be careful with ui policy scripts because you need to ensure the fields are actually on the form and that there are no acls conflicting with your script. If Location and Due date are on the form and do not have any acl restricting them you don't need the isItil if statement.


Thanks Sherry. CW_UpdateIncidentLocation is a role. will give it a try. Although it is only required for particular contact type such as ;phone and walk-in. Just wondering how to include those two contact types so when the role is itil u_due_date can be modified by itil?


You'll need to get the value of those fields and then do an if. It depends on the field type of those fields, you can check the GlideForm API for the method to use to check the field values:


https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=c_GlideFormAPI



Then, once you get the value you can check the value and perform the appropriate actions in an if statement.


Thanks so much I will review the the docs.