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.

Client Script to make fields read only based on Field state and role

paul_roberts
Kilo Expert

Hi All,

We have an issue where people raising changes are able to alter the Planned start / end times aftert the change has been Scheduled.   the change team have asked if there is a way to stop this occuring.

I'm trying to build a Client script that will lock the fields when the state changes to 'Scheduled' for everyone except the Change team.

I've created a new role called change_unlock for this purpose which i will give to the members of the change team.

Worth mentioning that I'm a total scripting novice with very basic Javascript skills  

My script currently looks like this, but doesn't seem to do anything at all....

function onLoad()

{

     

      var rqdrole = g_user.hasRole('change_unlock');

      var state =   g_form.getValue('State');

     

}

if (state == 'Scheduled' + (rqdrole)){

      g_form.setReadonly('start_date',false);

      g_form.setReadonly('end_date',false);

     

}

else {

     

      if (state == 'Scheduled')

              g_form.setReadonly('start_date',true);

              g_form.setReadonly('end_date',true);

     

}

1 ACCEPTED SOLUTION

srinivasthelu
Tera Guru

Hi Paul,



I have edited little bit. Below is the code.



function onLoad()


{


   


var = g_user.hasRole('change_unlock');


var state =   g_form.getValue('state');


if(!rqdrole){


if (state != 'DontForegetTOReplaceThisWithChoiceValue' ){     // You can find the choice value of schedule Label by right clicking on the state field in the form and show choice list


      g_form.setReadonly('start_date',false);


      g_form.setReadonly('end_date',false);


   


}


else {


              g_form.setReadonly('start_date',true);


              g_form.setReadonly('end_date',true);


   


}


}


}




Hope this helps


Srini


View solution in original post

5 REPLIES 5

That's got it thanks Srinivas.   Just had to re-add my Variable name



I wasn't too far off!



Thanks Both