Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Populating a reference field dynamically in UI Builder

NathanHaywood
Tera Contributor

Hi all,

 

I'm attempting what should be a simple thing in theory, but UI Builder seemingly makes simple things difficult. 

 

Anyway, in the Hardware Asset Workspace, there's a button 'Renew' on ast_contract records. It opens a modal with various fields. One of which is an 'Approver' field. 

 

What's required:

- Check the 'Cost Centre' of the contract

- If 'Cost Centre' is 'IT' (sys_id of this record), then make the 'Approver' field in the modal read only. I have done this using the below script on the field in UIB.

- If 'Cost Centre' is 'IT', then populate the 'Approver' field with a specific user. - For this I have used a Data Resource look up record and for now gone straight to the sys_user table and found that user, I get access to the data no problem. 

 

The trouble with the last point is no matter what value I pass into the 'Approver' field from this data resource, it doesn't populate. The field is definitely a reference to sys_user. 

 

Here is the script that works fine to make the field read-only:

 

function evaluateProperty({api, helpers}) {

  if(api.data.look_up_record_contract.result.cost_center.value == '6c7bf3a61b51a61003c2db92b24bcb91') {

    return true;

  } else return false;
  
}

 

Here is the script that doesn't set the value of the field:

 

function evaluateProperty({api, helpers}) {

  if(api.data.look_up_record_contract.result.cost_center.value == '6c7bf3a61b51a61003c2db92b24bcb91') {

    return api.data.look_up_it_cost_center_approver.result._row_data.uniqueValue;

  } else return '';
  
}

 

I have also played around with the client scripts such as 'Set values from contract' but this is no use either.

 

Thanks in advance.

0 REPLIES 0