REST API not creating incident

andymar
Kilo Expert

Hi All,

I have created a scripted REST API in Global application and when using the REST API Explorer within ServiceNow I can POST to the incident table and it correctly populates the fields from the JSON.

However when I use postman or the webhook I can see in the logs that it creates the incident assigns an incident number but does not save the record.

(17e32c42dbfce3405c00b5ca6896194b sys id created INC0011055)

I have changed the table in the script to one of my custom tables and it posts to the table from postman.

The incident table is allowed to allow access to this table via web services.

I have setup a user with the ITIL role + import roles and also tried Admin role to send from postman but no success.

My scripted REST API is;

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

// implement resource here
var body = request.body.data;
gs.info("[Sample] Processing message: " + JSON.stringify(body.alert));

var alertID = body.alert.alertId;
var message = body.alert.message;
var tags = body.alert.tags;
var alias = body.alert.alias;
var desc = body.alert.description;
var priority = body.alert.priority;
var details = body.alert.details;
var stype = body.source.type;
var sname = body.source.name;

var gr = new GlideRecord('incident');
gr.initialize();

gr.x_86994_opsgenie_opsgenie_alert_id = alertID;
gr.short_description = message;
gr.u_tags = tags;
gr.x_86994_opsgenie_alert_alias = alias;
gr.description = desc;
gr.u_ops_priority = priority;
gr.worknotes = details;
gr.u_source_type = stype;
gr.u_source_name = sname;

gs.info(gr.sys_id + "sys id created" + gr.number);

gr.insert();

})(request, response);

Any ideas on why it will not POST from external sources but will POST from the REST API explorer?

1 ACCEPTED SOLUTION

andymar
Kilo Expert

Issue now resolved, A business rule was restricting my view, I turned off all business rules against the incident table and turned them back on one at a time and posting with postman until I found the rule that was stopping me.

Thanks

 

Andy

View solution in original post

6 REPLIES 6

andymar
Kilo Expert

Spotted I put the Var sysid in wrong place;

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

// implement resource here
var body = request.body.data;
gs.info("[Sample] Processing message: " + JSON.stringify(body.alert));

var alertID = body.alert.alertId;
var message = body.alert.message;
var tags = body.alert.tags;
var alias = body.alert.alias;
var desc = body.alert.description;
var priority = body.alert.priority;
var details = body.alert.details;
var stype = body.source.type;
var sname = body.source.name;


var gr = new GlideRecord('incident');
gr.newRecord();
//gr.initialize();
var sysId = gr.insert();
gr.x_86994_opsgenie_opsgenie_alert_id = alertID;
gr.short_description = message;
gr.u_tags = tags;
gr.x_86994_opsgenie_alert_alias = alias;
gr.description = desc;
gr.u_ops_priority = priority;
gr.worknotes = details;
gr.u_source_type = stype;
gr.u_source_name = sname;

gs.log("User can create: " + gr.canCreate());
gs.info(gr.sys_id + "sys id created" + gr.number);
//gr.update();
gr.insert();
gs.log("Inserted record sysId is: " + sysId);


})(request, response);

Inserted record sysId is: 115e910adb7027405c00b5ca689619f7 from your log

and from my log;

115e910adb7027405c00b5ca689619f7sys id createdINC0011078

 

Just really odd that it will not display the record

 

andymar
Kilo Expert

Issue now resolved, A business rule was restricting my view, I turned off all business rules against the incident table and turned them back on one at a time and posting with postman until I found the rule that was stopping me.

Thanks

 

Andy