NBeed help with FLOW - Scripted ASK for APPROVAL

Zod
Giga Guru

Hi there. I am struggling, and neither ChatGPT nor Grok will help out ;-(.

 

I have two flow variables that contain group/user sys_ids.

I now want to trigger an "Ask for Approval" via script to get approval from at least one member of each group and each of the users.

 

If I add it normally without the script, it works.

The context shows the rule as ApprovesAnyG[1b563e4d83636290198ac2dfeeaad3a7]&AnyG[10d00c3e8323e210198ac2dfeeaad339]&AllU[ba82abf03710200044e0bfc8bcbe5d1a].

 

When doing the same thing dynamically with the script, the rule is the same, but no approval records are added, and it is set to skipped.

Why?

 

This is not logical... 

 

I knopw that flow has some issues here .. and there are workarounds with tasks - but this is not acceptable. No ideal why they do not keep the common workflow as before ... at least for real complex things. MUTCH mutch more work now ... and 0% benefit ... (lets not start discussing)

 

(function() {
    // Access flow variables
    var groupIds = fd_data.flow_var.approval_group_list || '';
    var userIds = fd_data.flow_var.approver_list || '';

    // Process groups
    var rawGroups = groupIds ? groupIds.split(',').map(id => id.trim()) : [];
    var validGroups = [];
    rawGroups.forEach(function(gid) {
        if (gid) {
            var gr = new GlideRecord('sys_user_group');
            if (gr.get(gid)) {
                validGroups.push(gid);
            }
        }
    });

    // Process users
    var rawUsers = userIds ? userIds.split(',').map(id => id.trim()) : [];
    var validUsers = [];
    rawUsers.forEach(function(uid) {
        if (uid) {
            var ur = new GlideRecord('sys_user');
            if (ur.get(uid)) {
                validUsers.push(uid);
            }
        }
    });

    // Build rule string dynamically
    var rule = '';
    if (validGroups.length > 0) {
        rule = 'ApprovesAnyG[' + validGroups[0] + ']'; // First group
        for (var i = 1; i < validGroups.length; i++) {
            rule += '&AnyG[' + validGroups[i] + ']'; // Additional groups
        }
    }
    if (validUsers.length > 0) {
        validUsers.forEach(function(uid, index) {
            var userPart = (index === 0 && !rule) ? 'AllU[' : '&AllU['; // Start with AllU if no groups, else &AllU
            rule += userPart + uid + ']';
        });
    }

    // Debug logging for final rule 
    if (rule) {
        try { gs.info('Final rule: ' + rule); } catch (e) {}
    }
    return rule;
})();

 

1 ACCEPTED SOLUTION

Cause I am working on a very complext end-to-end thing .. not BR possible or usefull here.

But anyhow .. I found it - the 1t and last line need to be removed as not function is to be set here ... I am god now ... even still hating FLOW for complext issues ... 

View solution in original post

4 REPLIES 4

GlideFather
Tera Patron

Hi @Zod,

 

do you have any specific need to trigger approval by script? Why not to use BR or more preferably by a flow?

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


Cause I am working on a very complext end-to-end thing .. not BR possible or usefull here.

But anyhow .. I found it - the 1t and last line need to be removed as not function is to be set here ... I am god now ... even still hating FLOW for complext issues ... 

Ankur Bawiskar
Tera Patron
Tera Patron

@Zod 

did you get a chance to check this?

It has some extra options which you can check and try
Scripted Approvals in Flow Designer with Flow Variables 

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

Sure. But my rule was correct ... in line with that ..