logo

NJP

ServiceNow use case Make all fields Read-only using Client Script | ServiceNow tips and tricks

Import · Mar 04, 2022 · video

[Music] hi all and welcome back so in today's tutorial we'll be looking on one more use case okay this is pretty easy use case but i think a lot of people will be facing these things in their organizations so for example let's consider a scenario where there is a form on certain state and you want to mark all the pages on the form as the read only through a ui policy or client script you don't want to go to this data policy we will look into the ui policy site so what is the best way to do that so if we go with the primitive wave okay so for example we will take example of change why change request for today's demonstration because it has a state transition right so for example you have a requirement that uh you want to make all the fields on the form read only okay when the state is scheduled okay so now what would the best practice to do this what we generally do is like we simply go configure we create a ui policy we'll give it a certain name what name we will give for example basically a short description may read only on schedule state schedule state then your condition as what is what was the condition at date is scheduled okay now let's save it out and now what we'll do we will add the ui policy action on each field for example if i want to make uh you which field is redone editable on this let me open this record so now if i want to get started with a lot of feeds are editable create a requested by okay so what i will do i will simply go over here i will find a field requested by when is requested by request to be requested by i will make it read only equal to true and submit it out okay so that if i go to the form again if i reload it out i can see the requested bias read read only now but is it feasible that if you want to mark this much of page on the form as read only you go into each and every creator your policy actions and then define it over there and there's a possibility if you create a field in the future on the form okay possibly listed you have to again go to the you have to be proactive and you have to go to the ui policy again and add one more action over there so this is not that convenient method right so how we can do it with ease let's try to figure it out so what i will do i will simply go to the ui policy first to make it inactive i hope the use case got clear to everyone right so we will first make it inactive and instead of writing a ui policy on the form now what we'll be doing we will be writing a client script white client script because it will ease out our work okay how i will show you in a second so you can configure we will write a client script and i will click new i give it a name as make fill read only [Music] on kid instead on schedule we will give ui type as all for now we'll give it as on load and we will not give description message if you want to give as a best practice you can give that then what we can do we can give a condition as if if g under conform the dot get value of what we want the value of date because we want to make it read only only on schedule state equal to equal to what is the value for schedule let me just check it out so i will just go straight right click show choices schedule is minus 2 okay so if it's equal to minus 2 then we want to make all the bits that read only how we can do that if it's minus 2 that means if it's a schedule state then what will do valuable fields first of all we'll extract all the fields which you want to make it as marks mark it as the read only so for that we will try to get all the editable fields so we can simply use a function we underscore for dot get i'm not sure it's not present over here but there is a function okay edit table fails i hope it's i have written it get editable fields okay and first of all we'll try to get see whether it's providing all the fields or not finally it should provide a list of fields okay which are editable on the form so i'll simply do alert and i will give twist let's see what comes through this and save this out well again go to the change form we will reload it out to just to see the values what values are coming so now you can see all the fields which are editable on the form are coming as a part of this function pretty interesting right now you can see now we got all the face so it's pretty easy now so you can simply it will return an array okay so just to mention it will be array and then you can simply do 4 variable i equal to 0 and less than what fields dot send i plus plus and now we can simply write in one liner j underscore form dot that read only what to set rate on the its field of x i comma true that's it but with this short code what you are doing you are making all the fills on the format read only you don't believe me let's save it and try it out so we'll save it out it will again go to the form you can see all the bits that read only i've marked the ui policy also inactive so this is also editable reload and now you can see the entire form turns out as read only you don't have to define any field it will take it on its own okay so this was the use case i thought state is not read only okay because there is a separate property for that okay so it will not get rid of you because if if it goes read only then how we will do the state transitions right okay so this is all for today's tutorial hope you like this tutorial out okay please try to implement this kind of simple tricks in your project to make your code pretty easy okay and avoid a lot of creating a lot of components on the service noise steps like ui policy action hope you like this video please follow my channel follow such videos in the future okay till then stay safe stay happy and have a nice day thank you you

View original source

https://www.youtube.com/watch?v=gkkqiDm2ciQ