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

Hi @Ankur Bawiskar So you mean like the below:

matthew_hughes_0-1730822138331.png


What would the script need to look like?

@matthew_hughes 

in the BR script search the sn_hr_core_position table with query as this and then update

like this

(function executeRule(current, previous /*null when async*/ ) {
        // Add your code here

        var gr = new GlideRecord("sn_hr_core_position");
        gr.addQuery("u_job_profile", current.sys_id);
        gr.query();
        if (gr.next()) {
            var userRec = new GlideRecord('sys_user');
            userGR.addQuery('sys_id', gr.u_hr_profile.user);
            userGR.query();
            if (userGR.next()) {
                if (current.u_job_family.changes())
                    userGR.u_job_family = current.u_job_family;
                else if (current.u_job_family_group.changes())
                    userGR.u_job_family_group = current.u_job_family_group;
                userGR.update();
            }
        }
    }

})(current, previous);

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

Hi @Ankur Bawiskar 

 

It works when the fields are changed separately. However, what I've noticed is that if I change both of the fields at the same time, it doesn't work. What code would I need to add to cover if both fields are changed at the same time?

@matthew_hughes 

Glad to know that my script worked.

For that you can have another BR with condition as Both changes and make this script

(function executeRule(current, previous /*null when async*/ ) {
        // Add your code here

        var gr = new GlideRecord("sn_hr_core_position");
        gr.addQuery("u_job_profile", current.sys_id);
        gr.query();
        if (gr.next()) {
            var userRec = new GlideRecord('sys_user');
            userGR.addQuery('sys_id', gr.u_hr_profile.user);
            userGR.query();
            if (userGR.next()) {
                    userGR.u_job_family = current.u_job_family;
                    userGR.u_job_family_group = current.u_job_family_group;
                userGR.update();
            }
        }
    }

})(current, previous);

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

Hi @Ankur Bawiskar Thanks for your help. It'll update on some records, but not on others and I don't know why.