Help with code for a scripted inbound rest API / query incident table first

jlaue
Kilo Sage

Hello -

We had originally coded a scripted rest API to check if the inbound payload had a value for a field:  "Customer_Ticket__c" (which is our ServiceNow incident number) and if it did not, to return a 400 error, and if it did, to go ahead and insert it into a staging table, where I am then transforming into the incident table to update the record. This is working just fine....

However, that does not necessarily mean that it is a valid incident number, only that there is something provided.  So I would like to look at that "Customer_Ticket__c"  value and query against our 'incident' table to ensure that it is a valid incident and if it is not, to return an error and if it is valid, to go ahead and insert it into a staging table. 

I am not sure how/where to go about making that query into the incident table within the script.  Hoping someone could point me in the right direction.  I will include the code that we are currently using below.

Thank you!!

 

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    
    var d = request.body.data;
    var answer = {};
        
        if(d['Customer_Ticket__c'] == undefined){
            answer.http_status = "400";
            answer.status_message = 'Request Rejected: Customer_Ticket__c required but was not found. Please confirm before resending';
            
            
        }else{
            
            var inc=d.Customer_Ticket__c;  //ServiceNow Incident #
            var num=d.CaseNumber;  //Vendor Case #
            var ans=d.Answer__c;  //Proposed resolution
            var com=d.Customer_Comment;  //Vendor comments/notes
            
            var gr = new GlideRecord('u_xyz_vendor_incident_integration');
            gr.initialize();
            gr.u_customer_ticket_c=inc;
            gr.u_casenumber=num;
            gr.u_answer_c=ans;
            gr.u_customer_comment=com;
            gr.insert();
            
            
            answer.status = "201";
            answer.status_message = "Incident has been updated";
        }
        
        response.setContentType('application/json');
        response.setStatus(answer.http_status);
        var writer = response.getStreamWriter();
        writer.writeString(JSON.stringify(answer));
        
    })(request, response);

 

Thank you!!

1 ACCEPTED SOLUTION

Hi,

Update code as below and try once; I assume Customer_Ticket_c has incident number hence I am querying with number field

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    
    var d = request.body.data;
    var answer = {};
        
        if(d['Customer_Ticket__c'] == undefined){
            answer.http_status = "400";
            answer.status_message = 'Request Rejected: Customer_Ticket__c required but was not found. Please confirm before resending';
            
            
        }else{
            
            var inc=d.Customer_Ticket__c;  //ServiceNow Incident #
            var num=d.CaseNumber;  //Vendor Case #
            var ans=d.Answer__c;  //Proposed resolution
            var com=d.Customer_Comment;  //Vendor comments/notes
            
var incRec = new GlideRecord('incident');
incRec.addQuery('number', inc);
incRec.query();
if(incRec.next()){

            var gr = new GlideRecord('u_xyz_vendor_incident_integration');
            gr.initialize();
            gr.u_customer_ticket_c=inc;
            gr.u_casenumber=num;
            gr.u_answer_c=ans;
            gr.u_customer_comment=com;
            gr.insert();
            
            answer.status = "201";
            answer.status_message = "Incident has been updated";
        }
		else{
answer.http_status = "400";
answer.status_message = "Incident number not found";
}
        
}
        response.setContentType('application/json');
        response.setStatus(answer.http_status);
        var writer = response.getStreamWriter();
        writer.writeString(JSON.stringify(answer));
        
    })(request, response);

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

View solution in original post

6 REPLIES 6

Try this code. 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    
    var d = request.body.data;
    var answer = {};
        
        if(d['Customer_Ticket__c'] == "undefined"){
            answer.http_status = "400";
            answer.status_message = 'Request Rejected: Customer_Ticket__c required but was not found. Please confirm before resending';
            
            
        } else {
            
            var inc=d.Customer_Ticket__c;  //ServiceNow Incident #
            var num=d.CaseNumber;  //Vendor Case #
            var ans=d.Answer__c;  //Proposed resolution
            var com=d.Customer_Comment;  //Vendor comments/notes
            
var incRec = new GlideRecord('incident');
incRec.addQuery('number', inc);
incRec.query();
if(incRec.next()){

            var gr = new GlideRecord('u_xyz_vendor_incident_integration');
            gr.initialize();
            gr.u_customer_ticket_c=inc;
            gr.u_casenumber=num;
            gr.u_answer_c=ans;
            gr.u_customer_comment=com;
            gr.insert();
            
            answer.http_status = "201";
            answer.status_message = "Incident has been updated";
             }
	     else {
answer.http_status = "400";
answer.status_message = "Incident number not found";
           }       
        }
        response.setContentType('application/json');
        response.setStatus(answer.http_status);
        var writer = response.getStreamWriter();
        writer.writeString(JSON.stringify(answer));
    })(request, response);

Mark the comment as a correct answer and helpful once worked.

Hello - just to note that I did try this script, I didn't receive any errors on it.  However, when I test it out, it looks like it is just skipping past this part:

 

 if(d['Customer_Ticket__c'] == "undefined"){
            answer.http_status = "400";
            answer.status_message = 'Request Rejected: Customer_Ticket__c required but was not found. Please confirm before resending';

 

When I run test payloads through without the Customer_Ticket__c field/value pair it continues on to do the incident query, etc.

Thanks.