Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to make one variable readonly after task is closed

Gopal14
Tera Contributor

Hi Team,

 

for making sc_task variables readonly, I have written a UI Policy applies on catalog task and below is my script.

 

Gopal14_0-1731566288437.png

 

 

Now I have another variable on task form which is mandatory, after saving the record variable is automatically changing to readonly. however I want that variable readonly when state is closed complete,  I have used below code, but it is not working. means when catalog task state is in open or work in progresss, at that time also variable is readonly, I want that to be editable.

 

Gopal14_1-1731566532122.png

 

 

function onLoad() {
   //Type appropriate comment here, and begin script below

    var shortDescription = g_form.getValue('short_description');
    var state = g_form.getValue('state');
      if (shortDescription == 'Confirm Receipt of Old Machine' || state == 3) {
    g_form.setReadOnly('confirm_which_stockroom_this_has_been_returned_to', true);
   }

}
 
 
Note: Order of UI Policy is 100
           Order of Client script is 200
 
 

 

 

4 REPLIES 4

Runjay Patel
Giga Sage

Hi @Gopal14 ,

 

try below code.

function onLoad() {
   //Type appropriate comment here, and begin script below

    var shortDescription = g_form.getValue('short_description');
    var state = g_form.getValue('state');
      if (shortDescription == 'Confirm Receipt of Old Machine' || state == 3) {
g_form.setMandatory('confirm_which_stockroom_this_has_been_returned_to', false);
    g_form.setReadOnly('confirm_which_stockroom_this_has_been_returned_to', true);
   }

}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

In this video i have explained about Web service integration in ServiceNow like how it works, how we can configure it, what are the prerequisite and many more. I have covered below topics in this video. 1. understand Web Service. Like when and how we will use it. 2. Talked about Inbound and ...

HI @Runjay Patel 

 

why we are using setMandatory here?  I am having issue with ReadOnly

Hi @Gopal14 ,

 

Is this worked? if yes then reason is you can not make field read only if field have empty value with mandatory.

 

 

Ajay61
Kilo Sage

Hi @Gopal14 ,

 

It seems the field is becoming read-only due to the if condition:
 if (shortDescription == 'Confirm Receipt of Old Machine' || state == 3)  instead use  if (shortDescription == 'Confirm Receipt of Old Machine' && state == 3)
OR , simply 
if (state == 3) 

If you are using the if condition with ll operator the onload script execute even if short description condtion matches.