Flow Designer issue

Hafsa1
Mega Sage

I have below script to run on hr task table everytime task is updated, can anyone tell me how to add this in flow designer? Earlier this code was in workflow editor.

 

var grp = current.groups.toString(); //Group to execute action against
    var grps = grp.split(',');
    var subject = current.opened_by; //Get User to execute action against
    var requestor = current.opened_for; //Get Requestor
    var grpMgr;
    var group = '';
    for (var i = 0; i < grps.length; i++) {
        grpMgr = grps[i].manager; //Get the Groups Manager
        var grMbr = new GlideRecord('sys_user_grmember'); //Initiate Glide against Group membership table
        //Add Group membership  
        grMbr.initialize();
        grMbr.user = subject;
        grMbr.group = grps[i];
        grMbr.insert();
        group += "," + grMbr.group.getDisplayValue();
    }
    //Send the notification to the Requestor and Group Manager that the User has been removed from the Group
    gs.eventQueue('sn_hr_core.group_member_added', fd_data.trigger.current, grpMgr, requestor);
    current.work_notes = subject.getDisplayValue() + " successfully added to " + group; //Update Work Note with confirmation
    current.state = 3;
    current.active = false;
    current.update();
6 REPLIES 6

Sandeep Rajput
Tera Patron
Tera Patron

@Hafsa1 You need to create a custom action and use a script step to execute this script. 

 

Fore more information please refer to

https://docs.servicenow.com/bundle/vancouver-build-workflows/page/administer/flow-designer/concept/a...

 https://docs.servicenow.com/bundle/washingtondc-build-workflows/page/administer/flow-designer/refere...

 

Hope this helps.

Tai Vu
Kilo Patron
Kilo Patron

Hi @Hafsa1 

Let's give a try to the below approach. We would need around 5 action steps in the flow to achieve the business needs from your script above.

1. Create new Flow > set Trigger condition to the HR Task table

2. The 1st flow action is For Each, we loop through the Groups field

3. Create Group Member record with the Group and the Opened by

4. Set Flow Variable and collect the Group Managers from each group

5. Fire the event with Group Managers and Requestor

Screenshot 2024-08-22 at 14.33.07.png

6. Update the current HR Task record work notes.

 

Cheers,

Tai Vu

Kai Tingey
Tera Guru

what kind of data is current.groups? is it a list collector? 

 

you can either create a new action, add a script step and then massage what you've already got into that. Then add that to your flow.

 

or you can do it in the flow itself which is probably what i'd be trying to do.

 

If it's a reference field like a list collector, you can use flow designer to get the current record, then use a 'look up records' action

Specify sys_user_group table and conditions = sys_id 'is one of' (trigger record -> variable).

That should return each of the groups selected.

Then you can use 'for each' (group record in look up records), and then:

- create record (sys_user_grmember) -> group = (for each -> group record) and user is (trigger record ->  opened_by)

- fire event (sn_hr_core.group_member_added, p1 = (group record -> group manager -> sys_id), p2 = (trigger record -> opened for -> sys_id). 

 

you could update the work notes with the 'user added to group (group->name)' in each loop, or better yet - add to a string flow variable on each loop to build a list

 

After the for each loop is finished - update record and set the state and add the work notes then.

 

hope that helps

Getting error as below:

Hafsa1_1-1724818775570.pngHafsa1_2-1724818819567.png