Pass request from Scripted REST API POST method to a script Include.

Ramkumar Thanga
Mega Sage

I am trying to pass the JSON Request from the scripted REST API using the POST method to a script Include for further validations.

I have tried with the logs but the script include is not being triggered with the request.

Scripted REST API:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

var body = request.body.data;
var strBody = JSON.stringify(body);


var ship = shipmentInsertionvalidations.ws_createShipments(strBody);
gs.info(ship);
return ship;

})(request, response);

 

 Script Include

var shipmentInsertionvalidations = Class.create();
shipmentInsertionvalidations.prototype = {
    initialize: function() {
        this.error_message = String();
    },
	ws_createShipments: function(wp_request){
		
	gs.info("LOG_SHIP"+wp_request);	
	},
type: 'shipmentInsertionvalidations'
};
1 ACCEPTED SOLUTION

Vishal Birajdar
Giga Sage

Hi @Ramkumar Thanga 

 

Can you try updating below line in your code.

 

Scripted Rest api :

var ship = new shipmentInsertionvalidations().ws_createShipments(strBody);

 

and in script include:

 

var shipmentInsertionvalidations = Class.create(); shipmentInsertionvalidations.prototype = { initialize: function() { this.error_message = String(); },
ws_createShipments: function(wp_request){
gs.info("LOG_SHIP"+wp_request);
return "something";
},
type: 'shipmentInsertionvalidations'

};

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

3 REPLIES 3

Vishal Birajdar
Giga Sage

Hi @Ramkumar Thanga 

 

Can you try updating below line in your code.

 

Scripted Rest api :

var ship = new shipmentInsertionvalidations().ws_createShipments(strBody);

 

and in script include:

 

var shipmentInsertionvalidations = Class.create(); shipmentInsertionvalidations.prototype = { initialize: function() { this.error_message = String(); },
ws_createShipments: function(wp_request){
gs.info("LOG_SHIP"+wp_request);
return "something";
},
type: 'shipmentInsertionvalidations'

};

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Ramkumar Thanga
Mega Sage

Hi @Vishal Birajdar 

 

Thanks for the response!!

 

I am trying to the response back from the script include as below. But it doesn't works. Could you pls help me out.

 

var ship = new shipmentInsertionvalidations().ws_createShipments(strBody);
var resp = JSON.stringify(ship);
var parser = JSONParser();
var parsed = parser.parse(resp);
gs.info(resp+ "RAMCPL");
response.setBody(parsed);
 
The response at post man side is empty.

Hi @Ramkumar Thanga 

 

Refer the below script , you can adjust your code accordingly

 

try{

var ship = new shipmentInsertionvalidations().ws_createShipments(strBody);

var resp = {};

resp ["value"]= ship;
response.setBody(JSON.stringify(resp));
}

catch(ex){
                       // in case of error
var resp = {};

resp ["status"] = "Error Message";

resp ["message"] = ex.message;

response.setBody(JSON.stringify(resp ));

}

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates