Show error message in scripted rest api
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2017 11:19 AM
Hi,
I want to show error message when my caller_id and short_description is empty but here my api is running and creating incident in all the cases
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var body = request.body.data;
// implement resource here
var caller_id = body.caller_id;
var short_description = body.short_description;
var number = body.number;
var state = body.state;
var gr = new GlideRecord('incident');
gr.initialize();
gr.caller_id = caller_id;
gr.short_description = short_description;
if(caller_id && short_description == "")
{
var errorResponse= "<response><result><Message>Record creation failed in ServiceNow </Message></result></response>"; //////////////////// this message is not working
}
else{
gr.insert();
return {
"custom_message": 'Thanks for raising incident',
"number": gr.number,
"short_description": gr.short_description,
"caller_id": gr.caller_id,
"state": gr.state,
"Priortiy": gr.impact,
"Location": gr.locaion,
"Assignment Group": gr.assignment_group,
"Assignee": gr.assigned_to,
"Created" : gr.opened_at,
"Updated": gr.updated,
"Comments": gr.comment
};
}
Thanks in advance

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2017 11:27 AM
try to use gs.addErrorMessage('ERROR!!!!!');
5.1 addErrorMessage(Object)
Adds an error message for the current session. Use getErrorMessages() to retrieve a list of error messages currently being shown.
5.1.1 Input Fields
Parameters: an error object.
5.1.2 Example
gs.include("PrototypeServer"); var ValidatePasswordStronger = Class.create(); ValidatePasswordStronger.prototype = { process : function() { var user_password = request.getParameter("user_password"); var min_len = 8; var rules = "Password must be at least " + min_len + " characters long and contain a digit, an uppercase letter, and a lowercase letter."; if (user_password.length() < min_len) { gs.addErrorMessage("TOO SHORT: " + rules); return false; } var digit_pattern = new RegExp("[0-9]", "g"); if (!digit_pattern.test(user_password)) { gs.addErrorMessage("DIGIT MISSING: " + rules); return false; } var upper_pattern = new RegExp("[A-Z]", "g"); if (!upper_pattern.test(user_password)) { gs.addErrorMessage("UPPERCASE MISSING: " + rules); return false; } var lower_pattern = new RegExp("[a-z]", "g"); if (!lower_pattern.test(user_password)) { gs.addErrorMessage("LOWERCASE MISSING: " + rules); return false; } return true; // password is OK } }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2017 11:34 AM