The CreatorCon Call for Content is officially open! Get started here.

To update group names list on List field when user is added or removed from the group on form

lakshmi_laksh
Tera Contributor

Hello,

I am having requirement To update group names list on List field on employee profile form when a user is added or removed from the group, group names should automatically updated on list field value.
example,
->if user A is added to Group1 - then Group1 name should be updated on list field form.

-> when user A is removed from Group1 - then Group1 name should be removed and updated on list field on form

If anyone can help me on this would be helpful,
Thanks in advance

 

lakshmi_laksh_0-1745913923593.png

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@lakshmi_laksh 

you can use before delete business rule on sys_user_grmember table

Query your table and search if the user+group combination is present in your table

If yes then remove that group from list field

Something like this

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

    // Add your code here
    var gr = new GlideRecord("your table"); // give your table name here
    gr.addQuery("userField", current.user); // give your user field name here
    gr.addQuery("groupField", "IN", current.group); // give your group field name here
    gr.query();
    if (gr.next()) {
        var existingGroup = gr.groupField.toString(); // give your group field name here

        var index = existingGroup.indexOf(current.group.toString());
        if (index > -1) {
            existingGroup.splice(index, 1);
        }
        gr.groupField = existingGroup.toString(); // set the groups again after removing the group
        gr.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

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@lakshmi_laksh 

you can use before delete business rule on sys_user_grmember table

Query your table and search if the user+group combination is present in your table

If yes then remove that group from list field

Something like this

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

    // Add your code here
    var gr = new GlideRecord("your table"); // give your table name here
    gr.addQuery("userField", current.user); // give your user field name here
    gr.addQuery("groupField", "IN", current.group); // give your group field name here
    gr.query();
    if (gr.next()) {
        var existingGroup = gr.groupField.toString(); // give your group field name here

        var index = existingGroup.indexOf(current.group.toString());
        if (index > -1) {
            existingGroup.splice(index, 1);
        }
        gr.groupField = existingGroup.toString(); // set the groups again after removing the group
        gr.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

Thanks for the response