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.

Show error message in scripted rest api

Sneha39
Mega Guru

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

2 REPLIES 2

Harsh Vardhan
Giga Patron

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 } }

I have added




if(caller_id && short_description == "")


{



gs.addErrorMessage('ERROR!!!!!');


}


else{




        gr.insert();




}




but still its running on passing empty fields



find_real_file.png




Thanks