Rest API Explorer not creating new Record despite response saying success
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-22-2022 08:55 AM
Hey all,
I'm working on a scripted API that will create a new Change Request record using the information from the incoming payload.
Below is the Scripted REST API I have and its script.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
this.status = '200';
var respObj = {}; //declare the response object
var payload = request.body.data; //retrieving the JSON body
var requested_by = payload.requested_by; //getting the number from JSON
var type = payload.type;
var assignment_group = payload.assignment_group;
var short_description = payload.short_description;
var start_date = payload.start_date;
var end_date = payload.end_date;
var plutora_id = payload.u_plutora_release_id;
var user = new GlideRecord("sys_user");
user.addQuery("email", requested_by);
user.query();
if (user.next()){
var req_by = user.sys_id;
}
var newchg = new GlideRecord("change_request");
newchg.initialize();
newchg.requested_by = req_by;
newchg.type = type;
newchg.assignment_group = assignment_group;
newchg.short_description = short_description;
newchg.start_date = start_date;
newchg.end_date = end_date;
newchg.u_plutora_release_id = plutora_id;
newchg.correlation_id = "Plutora";
var sysID = newchg.insert();
this.status = '200';
respObj.body = {
"message": "Creation Success!",
"detail": "Change " + newchg.number + " created successfully"
};
if (this.status == '200') {
response.setBody(respObj);
response.setStatus(this.status);
return response;
} else {
var setError= new sn_ws_err.ServiceError(); //this API used to set the custom error messages
setError.setStatus(this.status);
setError.setMessage(respObj.body.message);
setError.setDetail(respObj.body.detail);
return setError;
}
})(request, response);
When I run through the script using the Rest API Explorer it comes back looking like it has been successful, but the Change Request that it says it has created does not exist. Have I done something wrong in my initialize stage of the script or is it just a feature of the Rest API Explorer that it doesn't actually generate any records?
Thanks for any responses!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-22-2022 01:32 PM
Hi,
REST API Explorer does create records. So there should be something. Since you are using initialize(), it gets a number value before it's inserted.
Does it work if you take your code and run it in scripts - background with the same parameters?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-22-2022 08:27 PM
You are setting "this.status" to '200' (twice) and then checking to see if "this.status" = '200'. Of course it is. I think you have some logic issues there. You should use sysID to determine whether or not the insert was successful.
Instead of
if (this.status == '200') {
use
if (sysID) {
Instead of using "this.status", just stick the status codes you want to return directly in the setStatus() functions.
See where I'm going?