Send Updated fields in the payload
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2023 09:04 AM
I am currently trying to send the updated fields to a third-party application. However, when I update any field using a business rule, I notice that all values, except the updated one, appear as null. Similarly, the same issue occurs, and the corresponding fields become blank in the third-party application.
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
gs.info("Check Exe @@@");
var restMessage = new sn_ws.RESTMessageV2('SS', 'Update Ticket');
restMessage.setStringParameterNoEscape('id', current.u_ticked_id);
restMessage.setStringParameterNoEscape('token', 'wjsjjjjjjjjjjjjjjjjjjjj');
if (current.u_environment) {
restMessage.setStringParameterNoEscape('environment', current.u_environment);
}
if (current.state) {
restMessage.setStringParameterNoEscape('status', current.state);
}
if (current.category) {
restMessage.setStringParameterNoEscape('state', current.category);
}
if (current.sys_updated_by == current.u_pst_assignee.user_name || current.sys_updated_by == current.assigned_to.user_name) {
restMessage.setStringParameterNoEscape('note', current.comments);
}
var requestBody = restMessage.getRequestBody();
gs.info("Request Payload sent to third party: " + requestBody);
gs.info('Check Exe here@@@');
var response = restMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
current.work_notes = responseBody;
gs.log("Check Exe @@@ Resp " + responseBody);
if (httpStatus === 200) {
// Proceed with processing the response
var responseJSON = responseBody.substring(10, responseBody.length - 1);
var parsedJSON = JSON.parse(responseJSON);
var targetIncidentNumber = parsedJSON['number'];
var targetIncidentSysId = parsedJSON['sys_id'];
var targetNumber = parsedJSON['u_ticked_id'];
var env = parsedJSON['u_environment'];
var notes = parsedJSON['comments'];
var logString = 'Check Exe @@@ Target Incident Number - ' + targetIncidentNumber +
'\nTarget Incident Sys ID - ' + targetIncidentSysId +
'\nTarget Incident ID - ' + targetNumber +
'\nEnvironment - ' + env +
'\nNotes - ' + notes;
gs.log(logString, 'BR- Test');
} else {
gs.error("Check Exe @@@ Error: HTTP Status " + httpStatus);
}
})(current, previous);
for ex : when I change the environment all other values are getting blank
custom_fields":[{"id":"description","value":null},{"id":"waiting_for_reason","value":null},{"id":"severity","value":"low"},{"id":"environment","value":"staging"},{"id":"state","value":"1"},{"id":"status","value":null},{"id":"note","value":null}
Thank you

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2023 01:55 AM
@Vamshi_ch123 This is expected as comments are stores in sys_journal_field table and current.comments will only have value when the comments are changed. Still if you wish to get the last comment during update then you can do it as follows.
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
gs.info("Check Exe @@@");
var restMessage = new sn_ws.RESTMessageV2('SS', 'Update Ticket');
restMessage.setStringParameterNoEscape('id', current.u_ticked_id+'');
restMessage.setStringParameterNoEscape('token', 'wjsjjjjjjjjjjjjjjjjjjjj');
if (current.u_environment) {
restMessage.setStringParameterNoEscape('environment', current.u_environment+'');
}
if (current.state) {
restMessage.setStringParameterNoEscape('status', current.state+'');
}
if (current.category) {
restMessage.setStringParameterNoEscape('state', current.category+'');
}
if (current.sys_updated_by == current.u_pst_assignee.user_name || current.sys_updated_by == current.assigned_to.user_name) {
restMessage.setStringParameterNoEscape('note', current.comments.getJournalEntry(1));
}
var requestBody = restMessage.getRequestBody();
gs.info("Request Payload sent to third party: " + requestBody);
gs.info('Check Exe here@@@');
var response = restMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
current.work_notes = responseBody;
gs.log("Check Exe @@@ Resp " + responseBody);
if (httpStatus === 200) {
// Proceed with processing the response
var responseJSON = responseBody.substring(10, responseBody.length - 1);
var parsedJSON = JSON.parse(responseJSON);
var targetIncidentNumber = parsedJSON['number'];
var targetIncidentSysId = parsedJSON['sys_id'];
var targetNumber = parsedJSON['u_ticked_id'];
var env = parsedJSON['u_environment'];
var notes = parsedJSON['comments'];
var logString = 'Check Exe @@@ Target Incident Number - ' + targetIncidentNumber +
'\nTarget Incident Sys ID - ' + targetIncidentSysId +
'\nTarget Incident ID - ' + targetNumber +
'\nEnvironment - ' + env +
'\nNotes - ' + notes;
gs.log(logString, 'BR- Test');
} else {
gs.error("Check Exe @@@ Error: HTTP Status " + httpStatus);
}
})(current, previous);
Please mark this answer helpful and correct if it addresses your issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2023 02:35 AM
I tried but now whenever update is happening, I get below error
{"message":"Invalid JSON string posted"}
JSON Payload for note: 18/12/2023 15:57:54 - Vamshi C (Additional comments) test
I tried to escape the special characters using JSON.stringify but still have the same error
{"message":"Invalid JSON string posted"}
JSON Payload for ibs_note1: "18/12/2023 15:57:54 - Vamshi C (Additional comments)\ntest\n\n"
- restMessage.setStringParameterNoEscape('note', JSON.stringify(current.comments.getJournalEntry(1)));Thank you