onload client script

sparkles
Tera Contributor

Hi,

 

I need help with client script. I have a form with one mandatory field, I need to make this field non mandatory for 3 users only. So if the login user / requester is one of A, B, C then this field should be non mandatory.

 

Thanks! 

1 ACCEPTED SOLUTION

Manmohan K
Tera Sage

@sparkles 

 

You might be having a UI policy which is setting the field mandatory .

UI policy precedence is higher than client script so its not working

 

if you are able to find the ui policy then remove ui action which makes the field mandatory and try with below script 

function onLoad() {
  
  var currentUser = g_user.user_id; 
  
  var nonMandatoryUsers = ['A', 'B', 'C']; // Replace A,B and C with the sys_ids of the users who should have the field as non-mandatory
  
  if (nonMandatoryUsers.indexOf(currentUser)!=-1) {
    g_form.setMandatory('field name', false);     //replace field name with backend name of field to be made non-mandatory
  }
else
{
  g_form.setMandatory('field name', true);  
}
}

 

View solution in original post

6 REPLIES 6

Manmohan K
Tera Sage

Hi @sparkles 

 

You can write below on load client script to do it (make changes as instructed in comments)

function onLoad() {
  
  var currentUser = g_user.user_id; 
  
  var nonMandatoryUsers = ['A', 'B', 'C']; // Replace A,B and C with the sys_ids of the users who should have the field as non-mandatory
  
  if (nonMandatoryUsers.indexOf(currentUser)!=-1) {
    g_form.setMandatory('field name', false);     //replace field name with backend name of field to be made non-mandatory
  }
}

 

sparkles
Tera Contributor

Hi Manmohan,

Thanks for your reply, unfortunately it didn't work

Manmohan K
Tera Sage

@sparkles 

 

can you share your client script code

 

function onLoad() {

    //Type appropriate comment here, and begin script below

    var currentUser = g_user.UserID;

    var nonMandatoryUsers = ['3aaf6648db2a63c7855d90d8db9619ce', '8aaf6648db2a63c0365d90d8db9619ce', '9aaf6648db2a63c0444d90d8db9619ce'];

         if (nonMandatoryUsers.indexOf(currentUser) != -1) {

g_form.setMandatory('approval_attach', false);