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

@e__rajesh_badam 

both the fields on user table are of type string?

both should hold Group name?

if yes then the BR should work fine, did you debug by adding info statements?

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

@Ankur Bawiskar 

Business Unit is String field

BU is reference field where it is pointing to sys_user_group table.

@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

Hi @Ankur Bawiskar ,

 

Thanks for guidance and your code helped me to achieve my half of the requirement.

 

and my code as per requirement is here in below

 

My requirement is if user part of 

GLO-COGS ServiceNow SSO Users assignment group then only i need to populate the BU and Business unit with COGS Assignment Group. If User selects any of the other groups then no need to perform any activity.
 
Here is the code which i Written 
 
 
(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())
    if (gr.name = 'e145770dfb28ae10d9a4fc287befdc41') // sys id of GLO-COGS ServiceNow SSO Users
    {
        var userRec = current.user.getRefRecord();
        userRec.u_business_unit = '0cb9b3901b04ac905c4cb91bcd4bcb36'; //sys_id of COGS group
        userRec.u_bu = '0cb9b3901b04ac905c4cb91bcd4bcb36';//sys_id of COGS group
        userRec.update();
    }

})(current, previous);
 
I used AFTER INSTER BUSINESS Rule

 

 

 

@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