The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Make Approval state field read only unless I am the approver or an admin

Baggies
Kilo Guru

I am trying to make the state field on an approval form read_only, except for admin role, and also if the current user is the approver. I have set up a UI Policy to do this,but it doesn't appear to like the current user. This is the script I have in the Execute if true script:

function onCondition() {

      if ((g_user.userID == g_form.getValue( 'approver' )) || (g_user.hasRole("admin"))) {

              g_form.setReadOnly( "state", false );

                      } else {

              g_form.setReadOnly( "state", true );

            }

}

 

approvalUIPolicy.JPG

1 ACCEPTED SOLUTION

poyntzj
Kilo Sage

are using Calgary too and we use an ACL



type: record


operation: write


active: yes


admin overwrite: yes


name: sysapproval_approver         state


Add a condition if you want   (we have state is not "Not valid for this change"


script: answer = gs.hasRole('approval_admin') || isApprovalMine(current);


View solution in original post

9 REPLIES 9

Thanks niedziel,


unfortunately, we are still on Calgary (I check Dublin too) and the 'is (dynamic) ' option must be a Eureka enhancement as I don't have it available.


Cheers, Mark S.


niedziel1
ServiceNow Employee
ServiceNow Employee

Curious you don't see it on dublin...that is when the feature was added.   It is the last choice on the operator list when building a condition for a reference field that points at the sys_users table.


Stephen W_
Giga Guru

You don't need "reverse if false".


But I believe your original should work if you clear out the condition.


I created and tested the following script in Calgary and it worked exactly as you want.



function onCondition() {


      if (g_user.userID == g_form.getValue('approver') || g_user.hasRole('admin')){


              g_form.setReadOnly('state',false);


      } else {


              g_form.setReadOnly('state',true);


      }  


}




However, if this should have an effect across the system, a UI policy isn't the place to do this.


Take a look at write ACLs, you can use current.approver there and it does a much more thorough job of preventing the state from being manually changed by someone without authority.



Thanks,


-Stephen


poyntzj
Kilo Sage

are using Calgary too and we use an ACL



type: record


operation: write


active: yes


admin overwrite: yes


name: sysapproval_approver         state


Add a condition if you want   (we have state is not "Not valid for this change"


script: answer = gs.hasRole('approval_admin') || isApprovalMine(current);


Baggies
Kilo Guru

Thanks again for all the responses. As pointed out, I did go the ACL route, and it was just what I needed. I will mark this as answered.