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

K10
Kilo Guru

Hi Paul,



A slight modification in your script



function onLoad()


{


    if(isLoading) return false;



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


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


   


}


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



      g_form.setMandatory('start_date',false); // This makes the field non mandatory


      g_form.setReadonly('start_date',false);



      g_form.setMandatory('end_date',false);   // This makes the field non mandatory  


      g_form.setReadonly('end_date',false);



}



else {


   


      if (state == 'Scheduled')


              g_form.setReadonly('start_date',true);


              g_form.setReadonly('end_date',true);


   


}



Hope this works


Hi Ketan,



Thanks for this, however I've just tried it by impersonating a user without the role and the fields are still editable...



Cheers


Paul


K10
Kilo Guru

Hi,



Gottcha   : setReadonly function is setReadOnly


Hope that's the culprit  



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