Access Subflow inputs from Activity Script

I have cloned the “Create Incident” Sub-Flow.  Note that this is a SubFlow not a flow.   I am now modifying the Activity called:  “Create Task”.  I have added “Location” down the bottom.  This is a field that the SolarWindows team would like to populate on the incident.  I know I should use the CMDB.  But they have already included this info into the SNMP TRAP.  The Network Team are quite insistent.  The Location information from the TRAP appears in the alert in the Additional Data (additional_info) field.  I therefore want to extract the SWD_Location field from the JSON of the Additional Data and then set the Location Field on the incident. 

The SN documentation does not say anywhere how to access these input variables for a SubFlow.  When you first create the script, the ServiceNow default script contains comments that explain how to do it for a Flow – but not a Subflow.  (The comments mention the trigger.current but there is no trigger.current record for my Subflow.  Maybe I am confused – but I am have tried Trial and Error.  I have exhausted all attempts at Googling.  Any thoughts anyone?   Thank you in advance.  

Here is my Code

/* 2020-05-14 Doug Connell
This code is designed to fill in the Location field on the incident.
using the addititional data field from the alert.  
The Additional Data Field is in JSON format.
*/
var logMessage = "";
var additionalDataString = inputs.alertGR.additional_info.toString();
logMessage += "\n inputs.keys()=" + inputs.keys();
logMessage += "\n inputs.alertGR.sys_id=" + inputs.alertGR.sys_id;
logMessage += "\n inputs.alertGR=" + inputs.alertGR;
logMessage += "\n inputs.ah_alertgr.sys_id=" + inputs.ah_alertgr.sys_id;
logMessage += "\n inputs.ah_alertgr=" + inputs.ah_alertgr;
logMessage += "\n ===== Additional Data =====\n" + additionalDataString + "\n ==========\n";
var additionalDataObject = {};
var location = "";
try {
    additionalDataObject = JSON.parse(additionalDataString);
    location = additionalDataObject.SWD_Location;
    logMessage += "\n Setting location to: " + location;
}
catch(err) {
    location = "";
    logMessage += "\n Parsing the JSON failed!";
}
// Setting the source of the System Log message to "SW" (SolarWinds) so that it is easier to find.
// NOTE: Don't forget to comment this gs.log line below after you have fnished debugging.
gs.log(logMessage,"SW");
return location;

This is the output I get in the System Log:

nputs.keys()=undefined
inputs.alertGR.sys_id=undefined
inputs.alertGR=undefined
inputs.ah_alertgr.sys_id=undefined
inputs.ah_alertgr=undefined
===== Additional Data =====
undefined
==========

Parsing the JSON failed!
find_real_file.png

Testing

I am testing using the Test Button at the top of the Subflow.  I am entering a valid alertGR record.  So I can see that it is set in the Executions record.  Maybe inputs are not part of the inputs object?  Maybe it is called something else  – maybe input or my_inputs?    Maybe I have to create a custom Activity?      I am in the dark and guessing.

Solution 1

However I did get it working. 

  1. I copied the Activity called:  Calculate Values (Based On The Alert).  I created my own custom Activity called:  Calculate Values (Based On The Alert). 
  2. I then modified this new custom activity so that it had only one input variable (alertGr) and one output variable (location).  I changed the code so that it did the JSON.parse of the Additional Data field and returned the location.
  3. I then linked this new Activity into my SubFlow.   I made sure I put it as a Step before Create Task.
  4. Lastly editing the Create Task Activity in my Subflow to set the location field as a new Field Value step right at the bottom.  This step now required no code.  It just used the referenced variables that you access using that Black Bullet/Spanner Icon. 

So my fist method should have worked. I don’t know why it didn’t.  However, I managed to get it working – even through this method is longer and more involved.  so I am moving on ….  

Solution 2

The right way to access the input variables is with:

fd_data.subflow_inputs.variable_name

Tagged in :

dconnell@hotmail.co.nz Avatar

Leave a Reply

Your email address will not be published. Required fields are marked *