Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

I have an issue with Business Rule logic under incident

RahulS890357628
Tera Contributor

I have created a Business Rule , run after update and delete. In advance section I have created one script, from that script I am able to return incident fields but I also need which fields are updated  and once deleted I need incident Id. 

 

I have added code snippet there i use one variable updatedFields that also not working to identify which fields are updated.

 

Thank you advance.

 

Code block

 

 

 

(function executeRule(current, previous /*null when async*/) {

var gr = new GlideRecord('incident');
gr.get(current.sys_id);
var restMessage = new sn_ws.RESTMessageV2('Dqlabs Web Test', 'Send Webhook');
restMessage.setEndpoint('https://589c-103-21-79-42.ngrok-free.app/api/channel_action/servicenow_hook/');
restMessage.setStringParameterNoEscape('incident.number', gr.number);
restMessage.setStringParameterNoEscape('incident.short_description', gr.short_description);
restMessage.setStringParameterNoEscape('incident.state', gr.state);
restMessage.setStringParameterNoEscape('incident.priority', gr.priority);
restMessage.setStringParameterNoEscape('incident.urgency', gr.urgency);
// Add the event type parameter
var eventType = 'incident_' + current.operation() ;

var commentContent = "";
if (current.comments.changes()) {
var gr2 = new GlideRecord('sys_journal_field');
gr2.addQuery('element_id', current.sys_id); // Match the current record
gr2.addQuery('element', 'comments'); // Target the comments field
gr2.orderByDesc('sys_created_on'); // Sort by most recent
gr2.setLimit(1); // Fetch only the latest entry
gr2.query();

if (gr2.next()) {
commentContent = gr2.value.toString(); // Fetch the actual comment
eventType = "comment_updated";
gs.log('Exact Comment: ' + commentContent);
} else {
gs.log('No comments found for this record.');
}
}

var updatedFields = [];
var fieldsToCheck = [
'short_description', 'state', 'priority', 'urgency', 'comments'
];
fieldsToCheck.forEach(function(fieldName) {
var oldValue = previous.getValue(fieldName);
var newValue = current.getValue(fieldName);

// Log field changes for debugging
gs.log('Field: ' + fieldName + ' Old Value: ' + oldValue + ' New Value: ' + newValue);

// Add updated fields to the list
updatedFields.push({
field: fieldName,
old_value: oldValue,
new_value: newValue
});
});

// If any fields were updated, include them in the webhook
if (updatedFields.length > 0) {
restMessage.setStringParameterNoEscape('updated_fields', JSON.stringify(updatedFields));
}

restMessage.setStringParameterNoEscape('incident.comment', commentContent);
restMessage.setStringParameterNoEscape('event_type', eventType);
var response = restMessage.execute();
var httpResponseStatus = response.getStatusCode();
gs.log('Webhook response status: ' + httpResponseStatus);

})(current, previous);

 

0 REPLIES 0