Unable to send the updated worknotes through rest message integration

VIKAS MISHRA
Tera Contributor

We have integration with 3rd party servicenow and using Rest message for this integration.

Now we have 2 API created 1 is for create incident and 2nd for update incident. create one is working fine but update one is not working means if i update the work notes in my side incident then it should send that work note to 3rd party tool. as in logs i can see the error "{"error":{"message":"com.glide.rest.util.RESTRuntimeException: The payload is not valid JSON.","detail":""},"status":"failure"}".

 

Below is the BR that i am using to send the worknote while updating the incident.

(function executeRule(current, previous /*null when async*/ ) {
    if (current.u_external_ticket_no == '') {
        try {
            var rInsert = new sn_ws.RESTMessageV2('Ultima', 'Create Incident');

            //override authentication profile 
            //authentication type ='basic'/ 'oauth2'
            //r.setAuthenticationProfile(authentication type, profile name);

            //set a MID server name if one wants to run the message on MID
            //r.setMIDServer('MY_MID_SERVER');

            //if the message is configured to communicate through ECC queue, either
            //by setting a MID server or calling executeAsync, one needs to set skip_sensor
            //to true. Otherwise, one may get an intermittent error that the response body is null
            //r.setEccParameter('skip_sensor', true);
            var descbody = current.description;
            descbody = descbody.replace(/(\r\n|\n|\r|\\n)/gm, "");
            descbody = JSON.stringify(descbody);
            descbody = descbody.replace(/"/g, "'");
            descbody = descbody.replace(/\\/g, "\\\\");

            var shortDesc = current.short_description;
            shortDesc = shortDesc.replace(/(\r\n|\n|\r|\\n)/gm, "");
            shortDesc = JSON.stringify(shortDesc);
            shortDesc = shortDesc.replace(/"/g, "'");
            shortDesc = shortDesc.replace(/\\/g, "\\\\");

            rInsert.setStringParameterNoEscape('short_description', shortDesc);
            rInsert.setStringParameterNoEscape('description', descbody);

            rInsert.setStringParameterNoEscape('caller_id', 'Costa_API_user');
            rInsert.setStringParameterNoEscape('impact', current.impact);
            rInsert.setStringParameterNoEscape('urgency', current.urgency);
            rInsert.setStringParameterNoEscape('state', 1);
            rInsert.setStringParameterNoEscape('contact_type', 'Integration');
            rInsert.setStringParameterNoEscape('category', '910'); // cloud
            rInsert.setStringParameterNoEscape('subcategory', '1015'); // virtual
            rInsert.setStringParameterNoEscape('u_customer_reference', current.number);



            var insertResponse = rInsert.execute();
            var insertResponseBody = insertResponse.getBody();
            var insertHttpStatus = insertResponse.getStatusCode();
            var parsedData = JSON.parse(insertResponseBody);
            gs.log(parsedData.result.number + " Ultima Incident creation response : " + insertResponseBody, 'UltimaINCInsert');
            current.u_external_ticket_no = parsedData.result.number;
			current.state =11;
            current.update();

        } catch (ex) {
            var message = ex.message;
        }
    } else {
        try {
            var rUpdate = new sn_ws.RESTMessageV2('Ultima', 'Update Incident');

            //override authentication profile 
            //authentication type ='basic'/ 'oauth2'
            //r.setAuthenticationProfile(authentication type, profile name);

            //set a MID server name if one wants to run the message on MID
            //r.setMIDServer('MY_MID_SERVER');

            //if the message is configured to communicate through ECC queue, either
            //by setting a MID server or calling executeAsync, one needs to set skip_sensor
            //to true. Otherwise, one may get an intermittent error that the response body is null
            //r.setEccParameter('skip_sensor', true);
            var req = '{"u_customer_reference":"' + current.number + '"';
            if (current.impact.changes())
                req = req + ',"impact":"' + current.impact + '"';
            if (current.urgency.changes())
                req = req + ',"urgency":"' + current.urgency + '"';

            if (current.work_notes.changes()) {
                var workNotes = current.work_notes.getJournalEntry(1);
                workNotes = workNotes.replace(/(\r\n|\n|\r|\\n)/gm, "");
                workNotes = JSON.stringify(workNotes);
                workNotes = workNotes.replace(/"/g, "'");
                workNotes = workNotes.replace(/\\/g, "\\\\");
                req = req + ',"work_notes":"' + workNotes + '"';
            }
            if (current.comments.changes()) {
                var comment = current.comments.getJournalEntry(1);
                comment = comment.replace(/(\r\n|\n|\r|\\n)/gm, "");
                comment = JSON.stringify(comment);
                comment = comment.replace(/"/g, "");
                comment = comment.replace(/\\/g, "\\\\");
                req = req + ',"comments":"' + comment + '"';
            }
            if (current.assignment_group.changes()) {
                if (current.assignment_group == 'e8c3483c1b118510f48c65f7b04bcb0d') { // Ultima support group
                    req = req + ',"assignment_group":"' + 'd934315d1b4a2300ab0887fe6e4bcbf5' + '"'; // ultima servicedesk 24*7  
                }
                // 				else {
                //                 req = req + ',"state":"' + '8' + '","close_code":"' + 'Cancelled' + '","close_notes":"' + 'Cancelled as reassigned' + '"'; // resolved with cancel code in IBM
            }
            if (current.state.changes()) {
                if (current.state.changesTo('2')) { // In Progress
                    req = req + ',"state":"' + '2' + '"';
                } 
// 				else if (current.state.changesTo('10')) { //Awaiting User Information
//                     req = req + ',"state":"' + '3' + '","on_hold_reason":"' + '1' + '"';
//                 } else if (current.state.changesTo('11')) { //Awaiting Vendor Information
//                     req = req + ',"state":"' + '3' + '","on_hold_reason":"' + '4' + '"';
//                 } else if (current.state.changesTo('12')) { //Change implementation in-progress
//                     req = req + ',"state":"' + '3' + '","on_hold_reason":"' + '5' + '"';
//                 } 
				else if (current.state.changesTo('6')) { // resolved
                    var closeNotes = current.close_notes;
                    closeNotes = closeNotes.replace(/(\r\n|\n|\r|\\n)/gm, "");
                    closeNotes = JSON.stringify(closeNotes);
                    closeNotes = closeNotes.replace(/"/g, "'");
                    closeNotes = closeNotes.replace(/\\/g, "\\\\");
                    var close_code = current.close_code;
                    if (close_code == 'Solved (Work Around)' || close_code == 'Solved (Permanently)' || close_code == 'Solved Remotely (Work Around)' || close_code == 'Solved Remotely (Permanently)') {
                        close_code = 'Fix Implemented';
                    } else if (close_code == 'Closed/Resolved by Caller') {
                        close_code = 'Resolved by user';
                    } else {
                        close_code = 'Unable to Identify/Resolve';
                    }

                    req = req + ',"state":"' + '6' + '","close_code":"' + close_code + '","close_notes":"' + closeNotes + '"';
                } else if (current.state.changesTo('7')) { // closed
                    req = req + ',"state":"' + '7' + '"';
                } else if (current.state.changesTo('8')) { // cancelled
                    req = req + ',"state":"' + '8' + '"';
                } else if (current.state.changesTo('13')) { //reopened
                    req = req + ',"state":"' + '2' + '","work_notes":"' + 'Reopened' + '"';
                } else {
                    gs.log(current.state + " state update is not in scope", 'ultimaINCUpdate');
                }
            }

            req = req + '}';
            gs.log("Update req body " + req, 'ultimaINCUpdate');
            rUpdate.setStringParameterNoEscape('request', req);

			
            var updateResponse = rUpdate.execute();
            var updateResponseBody = updateResponse.getBody();
            var updateHttpStatus = updateResponse.getStatusCode();
            gs.log(" Ultima Incident update response : " + updateResponseBody, 'ultimaINCUpdate');
			
			
		} catch (ex) {
            var message = ex.message;
        }
    }

})(current, previous);

 

 

Below is the screenshot of Update incident API that i have created in rest message.

VIKASMISHRA_0-1684169939770.png

 

Below is the error in logs that i am getting

VIKASMISHRA_1-1684169963154.png

 

4 REPLIES 4

Weird
Mega Sage

Seems like you're way over complicating things.
Instead of 

var req = '{"u_customer_reference":"' + current.number + '"';

You could just write an object and push values into it:

var req = {};
req.u_customer_reference =  current.number.toString();
if (current.impact.changes())
req.impact = current.impact.toString();
if (current.urgency.changes())
req.urgency = current.urgency.toString();


Then after you've gone through every value you could then at the end use JSON.stringify(req); to escape characters and then set the request body as the whole thing:

var req = {};
req.u_customer_reference =  current.number.toString();
if (current.impact.changes())
req.impact = current.impact.toString();
if (current.urgency.changes())
req.urgency = current.urgency.toString();
req.work_notes = current.work_notes.getJournalEntry(1);
req.comments = current..comments.getJournalEntry(1);

var rUpdate = new sn_ws.RESTMessageV2('Ultima', 'Update Incident');
rUpdate.setRequestBody(JSON.stringify(req));
rUpdate.execute();


Since the JSON.stringify is done at the end, you don't need to do it separately for each field.
Note that instead of toString() you could also use getValue() or getDisplayValue() depending on your needs and fields.

As per you said i used below mentgioned code but it is not at all working as it is coming in the else part and printing the first 2 logs but after that from your code starts it not going ingto it.

 

(function executeRule(current, previous /*null when async*/ ) {
    if (current.u_external_ticket_no == '') {
		gs.log("OUTISE LOOP","Sequence 1 Test");
        try {
			gs.log("Inside Try Create LOOP","Sequence 2 Test");
            var rInsert = new sn_ws.RESTMessageV2('Ultima', 'Create Incident');

            //override authentication profile 
            //authentication type ='basic'/ 'oauth2'
            //r.setAuthenticationProfile(authentication type, profile name);

            //set a MID server name if one wants to run the message on MID
            //r.setMIDServer('MY_MID_SERVER');

            //if the message is configured to communicate through ECC queue, either
            //by setting a MID server or calling executeAsync, one needs to set skip_sensor
            //to true. Otherwise, one may get an intermittent error that the response body is null
            //r.setEccParameter('skip_sensor', true);
            var descbody = current.description;
            descbody = descbody.replace(/(\r\n|\n|\r|\\n)/gm, "");
            descbody = JSON.stringify(descbody);
            descbody = descbody.replace(/"/g, "'");
            descbody = descbody.replace(/\\/g, "\\\\");

            var shortDesc = current.short_description;
            shortDesc = shortDesc.replace(/(\r\n|\n|\r|\\n)/gm, "");
            shortDesc = JSON.stringify(shortDesc);
            shortDesc = shortDesc.replace(/"/g, "'");
            shortDesc = shortDesc.replace(/\\/g, "\\\\");

            rInsert.setStringParameterNoEscape('short_description', shortDesc);
            rInsert.setStringParameterNoEscape('description', descbody);

            rInsert.setStringParameterNoEscape('caller_id', 'Costa_API_user');
            rInsert.setStringParameterNoEscape('impact', current.impact);
            rInsert.setStringParameterNoEscape('urgency', current.urgency);
            rInsert.setStringParameterNoEscape('state', 1);
            rInsert.setStringParameterNoEscape('contact_type', 'Integration');
            rInsert.setStringParameterNoEscape('category', '910'); // cloud
            rInsert.setStringParameterNoEscape('subcategory', '1015'); // virtual
            rInsert.setStringParameterNoEscape('u_customer_reference', current.number);



            var insertResponse = rInsert.execute();
            var insertResponseBody = insertResponse.getBody();
            var insertHttpStatus = insertResponse.getStatusCode();
            var parsedData = JSON.parse(insertResponseBody);
            gs.log(parsedData.result.number + " Ultima Incident creation response : " + insertResponseBody, 'UltimaINCInsert');
            current.u_external_ticket_no = parsedData.result.number;
			current.state =11;
            current.update();

        } catch (ex) {
            var message = ex.message;
        }
    } else {
		gs.log('testing 1');
        try {	gs.log("Inside Try Update LOOP","Sequence 3 Test");
            var req = {};
req.u_customer_reference =  current.number.toString();
if (current.impact.changes())
req.impact = current.impact.toString();
if (current.urgency.changes())
req.urgency = current.urgency.toString();
req.work_notes = current.work_notes.getJournalEntry(1);
req.comments = current.comments.getJournalEntry(1);

var rUpdate = new sn_ws.RESTMessageV2('Ultima', 'Update Incident');
rUpdate.setRequestBody(JSON.stringify(req));
rUpdate.execute();
		var updateResponseBody = updateResponse.getBody();
      var updateHttpStatus = updateResponse.getStatusCode();
  gs.log('checking req value ' + '' + req);
			 gs.log('update response ' + '' + updateResponseBody);
			 gs.log('update status ' + '' + updateHttpStatus);
        } catch (ex) {
            var message = ex.message;
        }
    }

})(current, previous);

Basically getting update status as 500

Try logging some values that provide more information.
Now you know you're in the else condition. You'll then want to check the contents of req before you send it.

You also didn't have "var updateResponse" defined before "rUpdate.execute();", so you would miss the response even if the integration triggered.

(function executeRule(current, previous /*null when async*/ ) {
    if (current.u_external_ticket_no == '') {
        gs.log("OUTISE LOOP", "Sequence 1 Test");
        try {
            gs.log("Inside Try Create LOOP", "Sequence 2 Test");
            var rInsert = new sn_ws.RESTMessageV2('Ultima', 'Create Incident');

            //override authentication profile 
            //authentication type ='basic'/ 'oauth2'
            //r.setAuthenticationProfile(authentication type, profile name);

            //set a MID server name if one wants to run the message on MID
            //r.setMIDServer('MY_MID_SERVER');

            //if the message is configured to communicate through ECC queue, either
            //by setting a MID server or calling executeAsync, one needs to set skip_sensor
            //to true. Otherwise, one may get an intermittent error that the response body is null
            //r.setEccParameter('skip_sensor', true);
            var descbody = current.description;
            descbody = descbody.replace(/(\r\n|\n|\r|\\n)/gm, "");
            descbody = JSON.stringify(descbody);
            descbody = descbody.replace(/"/g, "'");
            descbody = descbody.replace(/\\/g, "\\\\");

            var shortDesc = current.short_description;
            shortDesc = shortDesc.replace(/(\r\n|\n|\r|\\n)/gm, "");
            shortDesc = JSON.stringify(shortDesc);
            shortDesc = shortDesc.replace(/"/g, "'");
            shortDesc = shortDesc.replace(/\\/g, "\\\\");

            rInsert.setStringParameterNoEscape('short_description', shortDesc);
            rInsert.setStringParameterNoEscape('description', descbody);

            rInsert.setStringParameterNoEscape('caller_id', 'Costa_API_user');
            rInsert.setStringParameterNoEscape('impact', current.impact);
            rInsert.setStringParameterNoEscape('urgency', current.urgency);
            rInsert.setStringParameterNoEscape('state', 1);
            rInsert.setStringParameterNoEscape('contact_type', 'Integration');
            rInsert.setStringParameterNoEscape('category', '910'); // cloud
            rInsert.setStringParameterNoEscape('subcategory', '1015'); // virtual
            rInsert.setStringParameterNoEscape('u_customer_reference', current.number);



            var insertResponse = rInsert.execute();
            var insertResponseBody = insertResponse.getBody();
            var insertHttpStatus = insertResponse.getStatusCode();
            var parsedData = JSON.parse(insertResponseBody);
            gs.log(parsedData.result.number + " Ultima Incident creation response : " + insertResponseBody, 'UltimaINCInsert');
            current.u_external_ticket_no = parsedData.result.number;
            current.state = 11;
            current.update();

        } catch (ex) {
            var message = ex.message;
        }
    } else {
        gs.log('testing 1');
        try {
            gs.log("Inside Try Update LOOP", "Sequence 3 Test");
            var req = {};
            req.u_customer_reference = current.number.toString();
            if (current.impact.changes())
                req.impact = current.impact.toString();
            if (current.urgency.changes())
                req.urgency = current.urgency.toString();
            req.work_notes = current.work_notes.getJournalEntry(1);
            req.comments = current.comments.getJournalEntry(1);
			
			gs.log(JSON.stringify(req, null, '\t'), "Sequence 3 Test"); //Added log for what req contains
			
            var rUpdate = new sn_ws.RESTMessageV2('Ultima', 'Update Incident');
            rUpdate.setRequestBody(JSON.stringify(req));
            var updateResponse = rUpdate.execute(); //You were missing updateResponse here
            var updateResponseBody = updateResponse.getBody();
            var updateHttpStatus = updateResponse.getStatusCode();
            gs.log('Update response body ' + '' + updateResponseBody, "Sequence 3 Test");
            gs.log('update status ' + '' + updateHttpStatus, "Sequence 3 Test");
        } catch (ex) {
            var message = ex.message;
        }
    }

})(current, previous);