Update group for user created from catalog item

Souvick6917
Tera Contributor

Hello team,

I am creating customer contact from a catalog item. After submission of the catalog it is creating record on the customer table.

The challenge is to add the created users to specific groups. I want to achieve this by flow designer,

Scenario: One variable A with option X and Y.

On select of X , the created users should be added to P group 

On select of Y , the created users should be added to Q group 

The groups are visible under the related tab of the customer form.

 

Please help with this requirement, and suggest any alternate method to achieve the solution other than using Flow designer.

 

Rergards

Souvick

 

6 REPLIES 6

The variables are of 'Select Box' type with choices. 

Maddysunil
Kilo Sage

@Souvick6917 

You can try using after insert business rule on customer table, below is sample code you can use it in script:

 

(function executeRule(current, previous) {

        // Fetch the associated Catalog Item record
        var catalogItem = new GlideRecord('sc_cat_item');
        if (catalogItem.get(current.cat_item)) {

            var variableAValue = catalogItem.getValue('variable_A'); // Change 'variable_A' to the actual variable name

            // Check the value of variable A and add user to appropriate group
            if (variableAValue == 'X') {
                addUserToGroup(current, 'P'); // Add user to group P
            } else if (variableAValue == 'Y') {
                addUserToGroup(current, 'Q'); // Add user to group Q
            }
        }
    }

    // Function to add user to group
    function addUserToGroup(userRecord, groupName) {
        var group = new GlideRecord('sys_user_group');
        if (group.get('name', groupName)) {
            var membership = new GlideRecord('sys_user_grmember');
            membership.initialize();
            membership.setValue('group', group.sys_id);
            membership.setValue('user', userRecord.sys_id);
            membership.insert();
        } else {
            gs.error('Group ' + groupName + ' not found.');
        }
    }

})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks