Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Try catch in script include

Geeky
Kilo Guru

Hi,

I am publishing a scripted web service and have code in the script include. For better error handling of an unexpected error, thinking of using try-catch block.

Can anybody share a code snippet of how to use a try-catch block for displaying the error message in response to the API call?

6 REPLIES 6

lpsilviag
Tera Contributor

Could you help me check why this script is not working?

 

var vendorName = current.variables.vendor_name.getDisplayValue();
var vendorURL = current.variables.vendor_website_url.getDisplayValue();

try {
 var r = new sn_ws.RESTMessageV2('Push Data to Panorays on Vendor Onboard', 'POST');
r.setStringParameterNoEscape('asset',vendorURL);
r.setStringParameterNoEscape('name',vendorName);
 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();

if (httpStatus == 200){
gs.info("Panorays successfully updated for: " + vendorName);
current.work_notes = 'Success Panorays API Call';
gs.addInfoMessage("Panorays was successfully updated");
}

if(httpStatus == 400){
gs.addErrorMessage("Panorays update failed");
current.work_notes = 'Failed Panorays API Call';
gs.error("Panorays update failed for: " + vendorName);
}
}
catch(ex) {
 var message = ex.message;
 gs.error("Error in system logs: " + message);
}

 

what is the error message you're receiving?