How to add a flow variable to JSON payload in Flow designer?

Lidia
Tera Contributor

Hi,

I have tried everything I can think of and nothing works so I really need help ("You are my only hope" lol).

So I have this workflow that is triggered from an Integration Queue Record and so it will have the payload itself (JSON) and the Integration Queue Number that is assigned to that record.

I am trying to modify my existing payload to add the IQ number to that JSON. The issue is that I can't seem to pass that variable into the JSON object - it either appears blank or literal variable name instead of variable value.

var myPayload = fd_data.trigger.current.u_payload; //this is the current payload
var myIQnumber = fd_data.trigger.current.u_number; //this is the integration queue number

var obj = JSON.parse(myPayload); //change back to object
obj.IQname = myIQnumber; //adding the IQ number to the JSON payload

var newPayload = JSON.stringify(obj); //convert object back to string

return newPayload;

here is the result:

find_real_file.pngIf I try to escape the variable like I do in the workflow, for example: 

obj.IQname = "+myIQnumber+"; //adding the IQ number to the JSON payload

That will literally result in JSON output looking like:

{...,"IQname":"+myIQnumber+"}

 

Help!

1 ACCEPTED SOLUTION

Lidia
Tera Contributor

Finally got it to work today, switched my method a bit

var myPayload = fd_data.trigger.current.u_payload; //this is the current payload
var myIQnumber = fd_data.trigger.current.u_number; //this is the integration queue number

var obj = JSON.parse(myPayload); //change to object

var IQ = 'IQnumber'; //this is the key of the JSON's key value pair, it assumes that it already exists

obj[IQ] = ""+myIQnumber+""; //proper escape //sets the value (assuming that key exists, if not then do: obj.IQnumber)

var newPayload = JSON.stringify(obj); //convert object back to string

return newPayload;

View solution in original post

2 REPLIES 2

Lidia
Tera Contributor

Finally got it to work today, switched my method a bit

var myPayload = fd_data.trigger.current.u_payload; //this is the current payload
var myIQnumber = fd_data.trigger.current.u_number; //this is the integration queue number

var obj = JSON.parse(myPayload); //change to object

var IQ = 'IQnumber'; //this is the key of the JSON's key value pair, it assumes that it already exists

obj[IQ] = ""+myIQnumber+""; //proper escape //sets the value (assuming that key exists, if not then do: obj.IQnumber)

var newPayload = JSON.stringify(obj); //convert object back to string

return newPayload;

Anil Lande
Kilo Patron

Hi,

You can put logs in your script to p rint value of your variable myIQnumber before adding to payload.

Also make small change and use

obj.IQname= myIQnumber.toString();

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande