Parse additional info to get key value pair from alert record and store it in a variable

Ash34
Giga Contributor

Hello Folks,
I'm new to service now. I'm trying to create a flow where the trigger is from an alert. 

In the additional information of the alert, there is a key/value pair that I want to store as a variable and then use in an Action.

Additional info JSON: 

{

name: "ashutosh",

id: "1234"

}

I want to extract the id and store it in a variable. Then, use that variable in an API call (action). If someone can provide me detailed steps as I'm very new to this platform would be nice. 

1 ACCEPTED SOLUTION

Ash34
Giga Contributor

Here is how I achieved it:

  1. In the sub-flow added an input variable and label as id of type string.
  2. In the action of rest API, I also created input variable and named it the same as the above.
  3. From the flow designer, there was an option to add a script, the key was very hard for me to find. I have highlighted the green box.
    find_real_file.png
  4. After press that button, we need to add the below javascript code provided by @Ankur Bawiskar. But we had to get to our records which were hard to find in the first line. Note: if you are trying to parse some other variable than additional_info, change it accordingly. Try to use auto-completion.
    var str = fd_data.trigger.current.additional_info;
    var obj = JSON.parse(str);
    var id = obj.id;
    return id;​
  5. Finally test the flow. It works like a charm.

View solution in original post

21 REPLIES 21

Hi,

so you need to send this to REST API action

so how are you invoking that REST API

Did you try to send the complete JSON and then parse it in the REST action

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I have created an action for the rest API. It gets invoked once the alert is triggered, meaning the flow starts.


How can I send the complete JSON to action rest API?

Hi,

you can send the Alert.Field value from the data pill

can you share the action you created for REST API.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I have written this logic in the script to get data out of the JSON:

var str = fd_data.trigger.current.additional_information;
var obj = JSON.parse(str);
var id = obj.id;

 
But additional_information might be of string type I guess.

 

The error that I'm getting:

failed with error: java.lang.IllegalArgumentException: additional_information is not a valid dot-walk for record 

 

I'm using this as an input variable for a sub-flow and want to create output of field type, so that I can use it in API.

Ash34
Giga Contributor

Here is how this is working:

var str = fd_data.trigger.current.additional_info;
var obj = JSON.parse(str);
var id = obj.id;
return id;

 

The sub-flow is taking the above as the input variable and also passing it as output.

How can I use the output variable in the REST API call?