How to set HTTP Status Code and Response Body to the Outputs of Script Action step in Flow Designer?

Aki18
Tera Contributor

I created a Script Action step to execute REST message like below.

Could someone please let me know how to set "HTTP Status Code" and "Response Body" to the Outputs of Script Action step? I need to use the values of the outputs in Flow that calls the Action.

(function execute(inputs, outputs) {
 try { 
 var r = new sn_ws.RESTMessageV2('Yahoo Finance', 'get');

//override authentication profile 
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);

//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');

//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.message;
}
})(inputs, outputs);

Best Regards,

Aki

1 ACCEPTED SOLUTION

You just need to set some output variables in your script like:

...
 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();

// define outputs
outputs.status_code = httpStatus;
outputs.response_body = responseBody;

 

Then complete the 'Output Variables' section below to capture these values (you can setup you type as required based on script outputs):

Outputs1.JPG

 

Lastly define the Action Outputs and pull in the data pills (generated from above steps) from the data pane on the right hand side:

Outputs2.JPG

 

Note: There's a lot of good documentation already on this. Check out the developer docs for instance:

https://developer.servicenow.com/dev.do#!/learn/courses/sandiego/app_store_learnv2_flowdesigner_sand...

 

View solution in original post

3 REPLIES 3

Geoff_T
Mega Sage

Hi,

Any reason you aren't using the out of box REST step?

REST IH.JPG

 

This way you get all the REST step data outputs you might want readily available to you:

REST IH3.JPG

Geoff

Aki18
Tera Contributor

Hi @Geoff_T ,

Unfortunately, I have no access to the REST step due to license issue, so I'm trying to use the Script step instead.

You just need to set some output variables in your script like:

...
 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();

// define outputs
outputs.status_code = httpStatus;
outputs.response_body = responseBody;

 

Then complete the 'Output Variables' section below to capture these values (you can setup you type as required based on script outputs):

Outputs1.JPG

 

Lastly define the Action Outputs and pull in the data pills (generated from above steps) from the data pane on the right hand side:

Outputs2.JPG

 

Note: There's a lot of good documentation already on this. Check out the developer docs for instance:

https://developer.servicenow.com/dev.do#!/learn/courses/sandiego/app_store_learnv2_flowdesigner_sand...