Pull in Sys_ID of sys_user record for a csm_consumer update

WLMurray
Tera Contributor

I am newer to ServiceNow and I am having difficulty on a business rule. The goal is to add a record to sys_user_has_role for the consumer if a certain field on their csm_consumer record is updated.

Using a Business Rule on csm_consumer table if the field is changed is leaving the user blank on the record in sys_user_has_role because I’m not pulling back the correct sys_id.

If I create the same Business Rule based on an update to csm_consumer_user table or sys_user table it works properly. How do I pull back the sys_id of the sys_user or csm_consumer_user record when it’s the csm_consumer record that is being updated?

 

(function executeRule(current, previous /*null when async*/) {

var userRole = new GlideRecord('sys_user_has_role'); userRole.initialize();

userRole.setValue('role' , '48637227536ae250657679a0a0490e54'); // role sys_id

userRole.setValue('user', current.getUniqueValue());

userRole.insert();

})(current, previous);

 

WLMurray_0-1752522636127.png

 

 

1 REPLY 1

Sean Webster
ServiceNow Employee
ServiceNow Employee

You need to call the csm_consumer.user reference field to pull the value you are looking for, then use that same sys_id when setting the user value for the sys_user_has_role reference for the user field.

var csmRecords = new GlideRecord('csm_consumer');
csmRecords.query();

while (csmRecords.next()){
var userInCSM = csmRecords.user;
gs.info(userInCSM.sys_id);
}

If this answers your question, please mark this post as solved with this post as the solution.

Cheers,
Sean