How to populate values in User profile BU and Business Unti column based on User Group membership

e__rajesh_badam
Mega Guru

Hi Every one,

 

Need help in below scenario to achieve,

 

In my Account User getting created and adding into Group 'X'.

The requirement is Based on user group membership need to populated value in Business Unit and BU column.

 

Note: Both BU and Group details we are pulling it from sys_user_group table only.

 

 

 

 

e__rajesh_badam_0-1742995494879.png

 

2 ACCEPTED SOLUTIONS

@e__rajesh_badam 

then do this

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

    // Add your code here
    var gr = new GlideRecord("sys_user_group");
    gr.addQuery("sys_id", current.group);
    gr.query();
    if (gr.next()) {
        var userRec = current.user.getRefRecord();
        userRec.u_business_unit = current.group.name; //COGS is the group name here
        userRec.u_bu = current.group;
        userRec.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

View solution in original post

@e__rajesh_badam 

Keep the same script, update the BR condition as this

current.group.name == 'GLO-COGS ServiceNow SSO Users'

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

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

@e__rajesh_badam 

you can use after insert business rule on sys_user_grmember table which holds User<->Group membership

1) get the BU details from field present on sys_user_group and then query User and set it

something like this

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

    // Add your code here
    var gr = new GlideRecord("sys_user_group");
    gr.addQuery("sys_id", current.group);
    gr.query();
    if (gr.next()) {
        var userRec = current.user.getRefRecord();
        userRec.buField = gr.buFieldFromGroup;
        userRec.businessUnitField = gr.businessUnitFieldFromGroup;
        userRec.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 is not populating value in respective fields.

 

Here is the script which i created as per your suggestion

 

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

    // Add your code here
    var gr = new GlideRecord("sys_user_group");
    gr.addQuery("e145770dfb28ae10d9a4fc287befdc41", current.group);// sys_id is group sys id
    gr.query();
    if (gr.next()) {
        var userRec = current.user.getRefRecord();
        userRec.u_business_unit = COGS; //COGS is the group name here
        userRec.u_bu = COGS;
        userRec.update();
    }

})(current, previous);

 

e__rajesh_badam_0-1742997940933.png

 

e__rajesh_badam_1-1742998051419.png

 

 

@e__rajesh_badam 

your script is wrong, use this

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

    // Add your code here
    var gr = new GlideRecord("sys_user_group");
    gr.addQuery("sys_id", current.group);
    gr.query();
    if (gr.next()) {
        var userRec = current.user.getRefRecord();
        userRec.u_business_unit = 'COGS'; //COGS is the group name here
        userRec.u_bu = 'COGS';
        userRec.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

Sorry to trouble @Ankur Bawiskar , Did same but no luck, it is not populating Value in Business Unit and BU.
If group is selected/added manually as GLO-COGS ServiceNow SSO USERS or added automatically by system in user profile then we need to set value of BU is COGS.

e__rajesh_badam_0-1743003236605.pnge__rajesh_badam_1-1743003257110.png