Business Rule not updating the required fields

matthew_hughes
Kilo Sage

I'm trying to write a business rule that is running from the Position (sn_hr_core_position) table and I'm wanting it to run on the following triggers:

If 'Job Profile' -> 'Job Family' CHANGES 

OR 

If 'Job Profile' -> 'Job Family Group' CHANGES

matthew_hughes_0-1730815086200.png

 

The script for my business rule is:

(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here
    //Find the User record mapped to the HR profile]]
    var userGR = new GlideRecord('sys_user');
    userGR.get('sys_id', current.u_hr_profile.user);
    userGR.u_job_family = current.u_job_profile.u_job_family.sys_id;
    userGR.u_job_family_group = current.u_job_profile.u_job_family_group.sys_id;
    gs.log('MH - The job family is ' + userGR.u_job_family);
    gs.log('MH - The job family group is ' + userGR.u_job_family_group);
    userGR.update();
    gs.log('MH - The job family is ' + userGR.u_job_family);
    gs.log('MH - The job family group is ' + userGR.u_job_family_group);
})(current, previous);
 
However what I'm finding is that when I change either the 'u_job_family' or 'u_job_family_group' fields on the Job Profile table, the changes are not pushing into the corresponding fields on the sys_user record:
matthew_hughes_1-1730815254735.png

 

If somebody could let me know what I'm doing wrong of if it's even possible to dot walk through fields, that would be great.

 
11 REPLIES 11

@matthew_hughes 

are you sure there is an HR Position record matching your query?

Because if the logic worked for some then it means it's working fine. Definitely some issue with the data setup

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

GlideFather
Tera Patron

Your BR is executed after the change is performed, try to switch it to onBefore > before the database operation is executed... if not helpful, try to log the values to see what is happening

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */