Help with locking down fields

c_hayes
Giga Contributor

I am currently developing an application and want to lock down fields. When a user fills out a request, they submit it and the status automatically changes upon pressing submit. When the status is Complete or Cancelled we lock down all fields by setting them to read only using the client script below.

find_real_file.png

However, there are status' between those two points and we don't want approvers altering the information that users submit. So we want to lock down only certain fields(the fields that the user fills out) when they submit a request. I reached out to someone for help and they suggested I use the ACL's to do this. I'm not very confident in using the ACL's so I decided to make a client script based on the one above. Both of them lock down fields based on the status(One locks down all fields when the status is Complete or Cancelled and one Locks down a list of fields when the status isn't new request). I wrote this script based on the script above and it doesn't appear to be working.

find_real_file.png

Additional information:

I could be wrong but it appears in the first script it sets all fields to be included under a variable x and sets that variable x(which includes all of the fields) to read only. Because I just want certain fields I abandoned that method and decided that I would just list the fields I wanted to lock down within the if statement. Although I'm sure there neater ways to do this I figured that listing the fields would still work. I didn't want to type out the name of every field so I just decided to test the script with one field that I wanted locked down and if it worked then I'd go type out the rest.

Can anybody see why this wouldn't lock down the field u_prod_title when the status is something other than New Request?

I made sure that the script was active and I was looking at requests that were not in the new request status.

1 ACCEPTED SOLUTION

Hello Colin!!



Using client script to make fields read-only is not the best solution. Use ACLs/UI Policies is the best solution but since you want to know whats wrong with your script, here it is:



In line 7, use g_form.setReadOnly('u_prod_title', true);



fields[] is only used in the first script because the field names are all stored into an array called fields and each field name was walked through using the for loop. Since you are not doing that here, you don't need line 5 and change the line 7 to what I gave above.



the 'x' in fields[x] represents the index of the field name present in the array.


View solution in original post

8 REPLIES 8

I understand that the best course of action is an ACL but do you know why the code isn't working? Because I didn't get any error message and there is very few differences between the one that is working and the one that isn't working. At this point I just want to know if there is something wrong with the script for future reference and to have a better understanding of what went wrong.


Hello Colin!!



Using client script to make fields read-only is not the best solution. Use ACLs/UI Policies is the best solution but since you want to know whats wrong with your script, here it is:



In line 7, use g_form.setReadOnly('u_prod_title', true);



fields[] is only used in the first script because the field names are all stored into an array called fields and each field name was walked through using the for loop. Since you are not doing that here, you don't need line 5 and change the line 7 to what I gave above.



the 'x' in fields[x] represents the index of the field name present in the array.


Thank You, this is exactly what I was looking for!


Dave Smith1
ServiceNow Employee
ServiceNow Employee

Colin Hayes wrote:



I understand that the best course of action is an ACL but do you know why the code isn't working?


As mentioned in another response: you're trying to compare a choice field against display values, rather than the enumerated choices, so you're not performing the right comparison.   Try viewing the choice list to see what the list of potential values are.



If you want a deeper insight under the hood of your script, I'd recommend using alert() or g_form.addInfoMessage() to write out a few values as part of debugging (or use the field watcher to inspect values as they change).



However, for what you want, a UI policy is easier than scripting, and ACLs are better uppermost.   I understand you want to treat this as a scripting exercise, but it's probably not the best situation for experimentation.  



(Even the most skillful of scripters still fall into the "if the only tool you have is a hammer, every problem looks like a nail" trap.)