How to parse response in workflow runscript

vinitaraico
Tera Contributor

Hi Team,

 

I am working on one integration where first api generate a Machine Number and I need to parse it from the response and call send that machine number in the second api.

Response Body{"data":{"modifiedBy":null,"customData":{""machineAccessNumber":"2794"}

 

Code in 1st API to parse machineAccessNumber

var parseData = JSON.parse(response.getBody());
var ipin=parseData.machinAccessNumber; //getting undefined in log if i print "ipin" value
 
Calling this "ipin " in 2nd  api this is mandatory value for the payload 
var payload = {};
payload.machineAccessNumber = workflow.scratchpad.ipin;
 
Not sure what is wrong here
 
3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Does the Response Body really have two quotes before machineAccessNumber?  If it does, that's the first problem.  I assume the Response Body also continues from what is shown to close all of the curly brackets }}

Since some of the object values are themselves objects, you need to drill-down when accessing a nested value, and use the same name as in the Response Body (yours is missing an 'e' at the end of machine)

var ipin=parseData.data.customData.machineAccessNumber;

Once you get the correct value for 'ipin' I assume you are also setting this to the value of workflow.scratchpad.ipin, to use in the later workflow activity for the 2nd API.

Hi Brad,

 

Yes I am getting value in first API AFTER adding data in the script

 

var pin1=parseData.data.machineAccessNumber; it is working
 
However in the second api when i am setting again this value in the 2nd runscript ,api failed
var payload = {};
payload.machineAccessNumber = workflow.scratchpad.pin1;
How to call a variable value from one runscript to other in workflow?
 
 

Scratchpad is the right idea when using a variable in more than one Run Script activity.  Now that you have a good value for pin1, either add another line to assign the value to a new scratchpad variable, or if you are not using 'pin1' anywhere in the same script, change the assignment to a scratchpad variable.

var pin1=parseData.data.machineAccessNumber; 
workflow.scratchpad.pin1 = pin1;
-or-
workflow.scratchpad.pin1 = parseData.data.machineAccessNumber;

Then in the other Run Script you will be able to access workflow.scratchpad.pin1 as you attempted.