Subflow Variables - can you pass an object?

Mary Beth Hutch
Tera Guru

I am wondering if it is possible to pass an object that is not a GlideRecord, to a subflow as an input. I am calling my subflow from a business rule, and in the business rule, performing several lookups. The subflow is submitting a catalog item, and I need to pass in the values for various questions. 

 

I know I can define separate flow variables for each, but there are 19 things I need to lookup and share with the subflow.

4 REPLIES 4

Roshan Tiwari
Tera Guru

I think yes you can send objects parameters as string, try data type string when you are creating the subflow inputs.

 

To convert Object to String you can use JSON.stringify()

 

Please response if this finds helpful.

Viraj Hudlikar
Giga Sage

Hello @Mary Beth Hutch 

In ServiceNow, while it is not directly possible to pass complex objects (like JavaScript objects) to a subflow, you can overcome that by serializing the object into a JSON string and then passing that string as a single input variable. Then, you can deserialize the JSON string back into the object in the subflow for usage of its values.

 

In this business rule, serialize the object into a JSON string and pass it to the subflow as such.

 

 

(function executeRule(current, previous /*null when async*/) {
    // Example object with 19 properties
    var data = {
        field1: 'value1',
        field2: 'value2',
        // ... add all 19 fields
        field19: 'value19'
    };

    // Serialize the object to a JSON string
    var jsonString = JSON.stringify(data);

    // Call the subflow and pass the JSON string
    var subflow = new sn_fd.FlowAPI();
    subflow.startSubflow('your_subflow_sys_id', {
        json_data: jsonString
    });
})(current, previous);

 

 

In your subflow, create an input variable named as json_data  with type as string to receive the JSON string and then deserialize it back into an object.

Add a script step at the beginning of your subflow to deserialize the JSON string.

 

 

// Deserialize the JSON string into an object
var data = JSON.parse(inputs.json_data);

// Access the fields as needed in variable or use directly when you are creating in RITM
    var field1 = data.field1;
    var field2 = data.field2;
    // ... access all 19 fields
    var field19 = data.field19;

 

 

Note: I have not tested it just a prototype which might be helpful to you.

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Hello @Mary Beth Hutch 

 

I hope your concern has been fully addressed. If it resolves your issue, please consider marking it as the accepted solution. This will ensure others in the community can benefit from the solution too.

Thanks & Regards
Viraj Hudlikar.

Mary Beth Hutch
Tera Guru

Thank you very much for taking the time to write this up; I appreciate it very much and will give it a try