Update the location from Portal then add that user to group

Suresh_32
Tera Expert

Requirements : If End User change the location from the portal (under user profile) then add that user to Specific group.

 

The below code is working fine, when we change the location field on user (Sys_user) table.

But we need to change the location from portal (or) from preferences the below code is not working. 

 

Please help me on this requirement.

 

// Before BR condition is location changes to

(function executeRule(current, previous /*null when async*/ ) {
 
    // Add your code here
    var myGroup = 'bca6ab3f87a0b910d02fa6c83cbb354d';  // Group SysID
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('group', myGroup);
    gr.addQuery('user', current.sys_id);
    gr.query();
 
    if (!gr.next()) {
        gr.initialize();
        gr.group = myGroup;
        gr.user = current.sys_id;
        gr.insert();
    }
})(current, previous);
 
Suresh_32_0-1693496544889.png

 

3 REPLIES 3

Peter Bodelier
Giga Sage

Hi @Suresh_32,

 

If you are changing it from somewhere else as the user record, your current object is not going to be the user record.

Assuming that the user is changing it themselves, you could use below script.
Otherwise, you will need to get the information of the user somewhere first.

 

// Before BR condition is location changes to

(function executeRule(current, previous /*null when async*/ ) {
 
    // Add your code here
    var myGroup = 'bca6ab3f87a0b910d02fa6c83cbb354d';  // Group SysID
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('group', myGroup);
    gr.addQuery('user', gs.getUserID());
    gr.query();
 
    if (!gr.next()) {
        gr.initialize();
        gr.group = myGroup;
        gr.user = gs.getUserID();
        gr.insert();
    }
})(current, previous);

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Hi Peter,

 

 Thank you for your reply.

 BR is not trigged for the above code.

   

Vanderlei
Mega Sage

Hi, do you want to trigger an business rule if the language changes?

 

You need to make an Business Rule on the sys_user_preference table selecting the preference user.language

 

Vanderlei_0-1699025061170.png