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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Can you check whether it returns sys id for the gr.insert() method?

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi,

When using the gr.initialise() & gr.insert(); I do not get any values in the logs.

When I use gr.newRecord(); & gr.update(); it does give the incident number & sys_id but you cannot see it on the incident table

Hi Andy,

Can you check whether that user can create or not

you won't get the sys id I think unless you insert

gs.log("User can create: " + gr.canCreate());

var sysId = gr.insert();

gs.log("Inserted record sysId is: " + sysId);

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

andymar
Kilo Expert

Hi,

I have added your suggestions to the script, currently script is set with;

gr.newRecord(); & gr.insert();

gs.log("User can create: " + gr.canCreate());  Log shows -> User can create: true

gs.log("Inserted record sysId is: " + sysId); Log shows -> Inserted record sysId is: undefined

 

 

(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 sysId = gr.insert();
var gr = new GlideRecord('incident');
gr.newRecord();
//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.log("User can create: " + gr.canCreate());
gs.info(gr.sys_id + "sys id created" + gr.number);
//gr.update();
gs.log("Inserted record sysId is: " + sysId);
gr.insert();

})(request, response);