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

jlaue
Kilo Sage

Hello -

Just wanted to see if anyone had any ideas on how to approach this. 

Thanks!!

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

Hello - thank you for your help!  I did try this code, I received an error on line 37 on the second 'else'. 

 

find_real_file.png

 

Thank you!!

Hello -

I just commented this first part out and updated the If statement accordingly and it is working perfectly!   Thank you!!

 

//         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{