Triggering REST API Call from Workflow Run Script doesn't work?

MichaelLorincz
Tera Contributor

As the title might suggest, I'm trying to trigger a REST API call from a Workflow's "Run Script" activity (Several calls in different run scripts, actually). The problem I'm having is that the actual request doesn't seem to construct properly. I know the call/endpoint itself works, because I've tested it in Postman & ServiceNow, but when I try to do it specifically from the workflow the request is empty. Below is an example:

 

if (workflow.scratchpad.url == '') {
    var request, response;
    request = new sn_ws.RESTMessageV2('Modify WAS Application (Prod)', 'GET WAS App Information');
    request.setStringParameterNoEscape('system_tag', workflow.scratchpad.systemtag);
	gs.log(JSON.stringify(request), 'MJL');
    response = request.execute();
    if (response.haveError()) {
        current.work_notes = 'Error ' + response.getStatusCode() + ': ' + response.getErrorMessage();
    }
	var body = response.getBody();
	workflow.scratchpad.url = body.url;
}

And in a later run script:

if (current.variables.dev == "true") {
	var requestDev, responseDev;
    requestDev = new sn_ws.RESTMessageV2('Modify WAS Application (dev)', 'POST Modify WAS App');
	requestDev.setStringParameterNoEscape('system_tag', workflow.scratchpad.systemtag);

    if (workflow.scratchpad.desc != null) {
        requestDev.setStringParameterNoEscape('description', workflow.scratchpad.desc);
    }
    if (workflow.scratchpad.name != null) {
        requestDev.setStringParameterNoEscape('name', workflow.scratchpad.name);
    }
    if (workflow.scratchpad.owner != null) {
        requestDev.setStringParameterNoEscape('owner_ad_id', workflow.scratchpad.owner);
    }
    if (workflow.scratchpad.url != null) {
        requestDev.setStringParameterNoEscape('url', workflow.scratchpad.url);
    }
    // request.setRequestHeader("Accept", "application/json");
    // request.setRequestHeader('Content-Type', 'application/json');
	gs.log(JSON.stringify(requestDev), 'MJL');
    responseDev = requestDev.execute();
    current.additional_comments = "Response message: " + responseDev.message;
    if (responseDev.haveError()) {
        current.work_notes = 'Error ' + responseDev.getStatusCode() + ': ' + responseDev.getErrorMessage();
    }
    workflow.scratchpad.responseMsgDev = responseDev.message;
    workflow.scratchpad.responseCodeDev = responseDev.getStatusCode();
	gs.log("Dev Request: " + JSON.stringify(requestDev), 'MJL');
}
current.update();

I've tried doing this with setEndpoint() as well, to no different effect. As a side note, I also can't seem to make the work notes update whether or not there's an error (I have tried to update the notes in different spots within the run script activities).

The gs.log() of the request in both of those examples shows an empty JSON. Just open and close brackets, like so: {}.

 

Any tips? Thank you!

 

2 REPLIES 2

dev_hakimoufkir
Tera Contributor

did you get the solution ?

rauldacosta
Tera Contributor

I believe the problem could be in 

var body = response.getBody();
	workflow.scratchpad.url = body.url;

 
I would try to do JSON.parse(response.getBody()) before trying to access the .url property.