Subflow returns the wrong result when called in Workflow

Community Alums
Not applicable

I created a subflow so that I could call it in a legacy workflow. All it does it query whether a user is a member of an AD group. When I call it in a workflow using a script activity it returns true. Yet when I call it from a background script or directly from flow designer with the same inputs I get false which is what I am expecting because the user is not in the AD group. 

 

********Background Script*************

var inputs = {};

inputs['user'] = 'secyorkb'; // String
inputs['group'] = 'SN.03 Test Related App - SOC1'; // String
// Execute Synchronously: Run in foreground. Code snippet has access to outputs.
var result = sn_fd.FlowAPI.getRunner().subflow('global.is_user_in_group').inForeground().withInputs(inputs).run();
var outputs = result.getOutputs();

// Get Outputs:
// Note: outputs can only be retrieved when executing synchronously.
var is_member = outputs['is_member']; // True/False
gs.addInfoMessage('XX: outputs ' + is_member); ----> This returns false

 

************Subflow***************

ChrisDoernbrac_0-1716432931296.pngChrisDoernbrac_1-1716432957211.png

 

***********Workflow Script Activity*************

I hard coded the values to make sure it would definitely use the same data

 

try {
    var reqType = workflow.scratchpad.requestType; //Normal/Elevated/Service
    var adUser = 'secyorkb';
    var groupName = 'SN.03 Test Related App - SOC1';

    var inputs = {};
   
    inputs['user'] = adUser; // String
    inputs['group'] = groupName; // String
    gs.log('XX:3 inputs: ' + inputs.user + inputs.group);

    // Execute Synchronously: Run in foreground. Code snippet has access to outputs.
    var result = sn_fd.FlowAPI.getRunner().subflow('global.is_user_in_group').inForeground().withInputs(inputs).run();
    var outputs = result.getOutputs();

    // Get Outputs:
    // Note: outputs can only be retrieved when executing synchronously.
    var is_member = outputs['is_member']; // True/False
    gs.log('XX:4 outputs ' + is_member);
    workflow.scratchpad.isMember = is_member; ------->returns true

} catch (ex) {
    var message = ex.getMessage();
    gs.error(message);
}
________Logging__________
ChrisDoernbrac_2-1716433102603.png

 

What could be causing the workflow activity to return true when the other two methods return false
1 ACCEPTED SOLUTION

Community Alums
Not applicable

It turns out that though I was passing in a string to function it was treating it as an object for some reason. Once I added .toString() it works fine. 

View solution in original post

6 REPLIES 6

Ravi Chandra_K
Kilo Patron
Kilo Patron

Hello @Community Alums 

Try using log statements to check what values the script is returning at each step.

 

Also, I beleive the Workflow script runs Asynchronously. And the flow here running synchronously.

There might be issue because of that 

 

Please Mark this answer as helpful and correct if helped.

Kind Regards,

Ravi Chandra

Community Alums
Not applicable

Even if I put a 2min wait on that activity it still returns incorrectly. Running it from backgroud script its totally fine.

Community Alums
Not applicable

What's odd is that I am calling this sublow synchronously from the run script activity so it should wait for it to complete but it doesn't appear to do so. I if I run this from a background script it take maybe 5-10 seconds to run but in the workflow it gets past the script activity almost instantly. It's like it's not waiting for the subflow to complete. 

Community Alums
Not applicable
Now it seems to put returning null but I can confirm I sending in the correct inputs and when running it from flow designer it works fine.
 
    
(function() {

    try {
        var reqType = workflow.scratchpad.requestType; //Normal/Elevated/Service
        var adUser;
        var groupName;

        var grEa = new GlideRecord('x_baacb_wf_config_enterprise_access');
        grEa.addQuery('related_id', current.variables.u_access_id);
        grEa.query();

        if (grEa.next()) {

            groupName = grEa.entitlements;
        }

        gs.log("XX: group - " + groupName);

        if (reqType == 'normal') {
            var user = current.variables.requestType;

            var gr = new GlideRecord('sys_user');
            gr.addQuery('sys_id', user);
            gr.query();

            if (gr.next()) {
                adUser = gr.user_name;
            }
        } else {
            adUser = current.variables.user_id_for_elevated_account;
        }
        gs.log('XX: user ' + adUser);
        gs.log('XX: here1');
        var inputs = {};
        inputs['user'] = adUser; // String
        gs.log('XX: here2');
        inputs['group'] = groupName; // String
        gs.log('XX: here3');
        gs.log('XX: inputs ' + inputs.user + inputs.group);

        // Execute Synchronously: Run in foreground. Code snippet has access to outputs.
        var result = sn_fd.FlowAPI.getRunner().subflow('global.is_user_in_group').inForeground().withInputs(inputs).run();
        var outputs = result.getOutputs();

        gs.log('XX: here4');
        // Get Outputs:
        // Note: outputs can only be retrieved when executing synchronously.
        var is_member = outputs['is_member']; // True/False
        gs.log('XX: outputs ' + is_member + outputs.is_member);
        workflow.scratchpad.isMember = is_member;

    } catch (ex) {
        var message = ex.getMessage();
        gs.error(message);
    }

})();
 
OUTPUT
2024-05-22 22:45:48
InformationXX: here4*** Script
Select record for action: Created 2024-05-22 22:45:48 
2024-05-22 22:45:48
InformationXX: here3*** Script
Select record for action: Created 2024-05-22 22:45:48 
2024-05-22 22:45:48
InformationXX: action Add isMember null*** Script
Select record for action: Created 2024-05-22 22:45:48 
2024-05-22 22:45:48
InformationXX: outputs nullnull*** Script
Select record for action: Created 2024-05-22 22:45:48 
2024-05-22 22:45:48
InformationXX: inputs secyorkbTEST*** Script
Select record for action: Created 2024-05-22 22:45:48 
2024-05-22 22:45:48
InformationXX: group - TEST*** Script
Select record for action: Created 2024-05-22 22:45:48 
2024-05-22 22:45:48
InformationXX: here2*** Script
Select record for action: Created 2024-05-22 22:45:48 
2024-05-22 22:45:48
InformationXX: here1*** Script
Select record for action: Created 2024-05-22 22:45:48 
2024-05-22 22:45:48
InformationXX: user secyorkb*** Script