#8 Make fields mandatory and read only in ServiceNow | Learn Application Development | LMS
[Music] please subscribe to my channel and click on the bell icon to get the regular updates of my channel and do not forget to like comment and share the next feature we have to add to our application is mandatory fields that means we have to make some fields mandatory now if i talk about leave bucket if i talk about leave calculator both are not basically basically will not be used for end user perspective but leave request is for end users so in that case we will make some of the fields mandatory we will also make some fields read only as well because you don't want people to change some of the fields like for example maybe number number should always be read only so in that case i will go to my instance over here i am admin right now that's the reason i'm able to basically edit this number field as well so if you want to make it read only you can do that if i talk about requested by now this should also be read only i should not because if i am the user my name should automatically be populated over here basically i should not be able to change this so i can make it make this read only then i have to make the start date as mandatory started has to be mandated even even leave type should be mandatory and for example other other fields like approval maybe status these fields should also be i would say read-only basically users should not be able to edit this now in order to do that maybe you can do one thing that maybe you can put it for every user with the help of ui policy because ui policy will work for admins as well if until you will not put any kind of condition uh or or maybe you can also create acls but as of now i'm going to create a ui policy uh to make these fields mandatory uh and and some read only as well so in that case i will go to the studio and i will close this one i will click on create application file go to client development and double click on ui policy now when exactly these fields should be mandatory or they should be read only they have to be on load off on load of the the forms in that case i will quickly select the table that is leave request and i can put like this make fields men day 3 that's something i can mention i'm not putting any kind of condition because i want it for every every uh load of the form and i can just click on save so this ui policy will be saved but now i have to create ui policy actions so i will click on new and i will start with leave type so if i select leave type it has to be mandatory so i will make it true that's it so i'm done with this particular leave type so i will click on submit and if i come over here again i click on new i have to make requested by as read-only so i can just select over here i will go to requested by now i can't make it mandatory it has to be just read only that's it so i can make it read only true and i can submit this it's done i will go over here again i will click on this new i think i really like this studio because the the good part is that you can just create multiple tabs and i will select start date now which has to be mandatory so i select start date and it will be mandatory and i can just click on submit that's it so we have created the ui policy and if i come over here and i reload it you will see that requested by is read-only and you can see leave type is mandatory start date is mandatory and we also have cash end date is also mandatory now even even for example uh we that means we have to change this one i'm not showing anything none over here but we can just keep it mandatory because people have to select it um start date is mandatory end it was already mandatory with the help of with the help of client script i think ui policy that that we made before this half day that's what it is mandatory uh then we have these other fields as well like you can see we have this accrued taken balance you should definitely make them read only even to duration as well so in that case i will maybe go to my studio and i will come over here and i will just make it like this i will select it i will do accrued and i will make it true it would be read only and i will come over here another ui policy action i will close this one which we created i will come over here and i will go for balance balance should also be read-only submit close and i will come over here new and i will have duration read only submit and if i come over here what other field we have it's a balance taken so i will come over here and i will go to taken i will make it true and submit so all these fields basically they become they became read only so if i come over here reload it you will see now the duration is also read only all these fields are read only and this is also read-only now you can see as of now this is blank this is empty i don't i don't have any data over here so what i will do basically i will uh automatically populate the user in this particular field automatically and i really i don't have to change it how can i do that i will just go to the table leave request table and i will populate the default value that's it so if i come over here we have this requested by let me go to requested by over here now what i can do i can just provide the default value the default value i can put like basically it has to be it has to be with the basically it should show automatically the current logged in user so for that i can do javascript and i can do gs dot get user id and i can just put parentheses that's it and if i save this and i come over here and i reload it absolutely you can see that my name is populating over here automatically so if you want to do this you can just do you can also populate it with the help of client script but if you want to make it just a default value you can you don't have to write client script you can just make it as a default value with the help of gs dot get user id now how exactly i'm able to do that for user because that's what service now provides the api for glide system but it might happen that you want to populate some other field on reference field in that case i cannot put the default value but i might but i have to use the maybe script include i have to get the server data then or maybe you can do it with the help of client script as well the populated from the beginning so that's how you can put the default value in this particular field and we are done with the mandatory field so this is how you create a ui policy which which can make fields read-only visible or mandatory as well and at the same time we also learned that how we can set the default value for any kind of user field as well and similarly you can do for other fields as well as per the requirement for you get from your customers and clients
https://www.youtube.com/watch?v=fKam0Q5LdqY