Make 'state' field readonly if logged-in user & caller are same on incident form using client script

PTR
Tera Expert
 
4 REPLIES 4

AnubhavRitolia
Mega Sage
Mega Sage

Hi @PTR 

 

Have you tried any client script yet and facing issue?

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Mallidi Suma
Tera Guru

Hi @PTR ,

 

Please find the below onChange of Caller Client script to achieve your above use case.

 

Code:-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

if (newValue == g_user.userID) {
g_form.setReadOnly('state', true);

} else {
g_form.setReadOnly('state', false);
}//Type appropriate comment here, and begin script below

}

 

Please find the below screenshot.

Screenshot 2023-05-19 at 5.31.41 PM.png

 

Please mark my answer as helpful and accept it as a solution, if it helps!!

Thanks & Regards,

Suma.

AnubhavRitolia
Mega Sage
Mega Sage

Hi @PTR 

 

Please find the onChange Client Script on Caller field below:

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (newValue === '') {
      return;
   }
    //Type appropriate comment here, and begin script below
    var callerID = g_form.getValue('caller_id');
	
    if (g_user.userID == callerID) {
		
        g_form.setReadOnly('state', true);
    } else {
		
        g_form.setReadOnly('state', false);
    }
}

 

 

There is an existing OOTB UI Policy "Make fields read-only on close" Which will conflict with it. Inactivating it, script works fine. You have to modify this UI Policy accordingly to script is also not conflicting and purpose of UI Policy is also fulfilled.

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

tanvis
Tera Contributor

hi , 

please try below On change client script on "caller" field 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (newValue === '') {
      return;
   }
    //Type appropriate comment here, and begin script below
    var callerID = g_form.getValue('caller_id');
    var user=g_user.userID;
	
    if (user == callerID) {
		
        g_form.setReadOnly('state', true);
    } 
else {
		 g_form.setReadOnly('state', false);
    }
}