How to compare comments from external system to ServiceNow recent comment and if same skip

tanguturi0527
Tera Contributor

Hi All,

I need some help regarding the comments update from external system to ServiceNow.

We are trying to integrate ServiceNow with Trustwave system and we are able to get the incidents from their system but whenever the script runs the same comments are getting updated again and again.

I tried to build the logic to compare both the recent comments from TrustWave and ServiceNow but it still updates. Attached the script below 

 

Can anyone please help?

 

var r = new sn_ws.RESTMessageV2('TrustWave', 'Sample');
 
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode;
var jsonData = JSON.parse(responseBody);
 
for (var i = 0; i < jsonData.items.length; i++) {
    var incState = jsonData.items[i]['status'];
    var incShrtDesc = jsonData.items[i]['subject'];
    var incDesc = jsonData.items[i]['description'];
    var incCaseId = jsonData.items[i]['number'];
    var incCmnts = jsonData.items[i]['notes'];
    var jsonCmnts = JSON.stringify(incCmnts);
    gs.log(incCaseId);
    var gr1 = new GlideRecord('incident');
    gr1.addQuery('u_fusion_case_id', incCaseId);
    gr1.query();
    if (gr1.next()) {
        gr1.caller_id = 'sys_id';
        if (incState == 'NEW') {
            gr1.state = '1';
gr1.incident_state = '1';
        } else if (incState == 'IN_PROGRESS') {
            gr1.state = '2';
gr1.incident_state = '2';
        } else if (incState == 'ON_HOLD') {
            gr1.state = '3';
gr1.incident_state = '3';
            gr1.hold_reason = '4';
var dueDt = new GlideDateTime();
            dueDt.addDays(3);
            gr1.follow_up = dueDt;
        } else if (incState == 'CLOSED') {
            gr1.state = '6';
gr1.incident_state = '6';
        }
        gr1.contact_type = 'self-service';
        gr1.impact = '3';
        gr1.urgency = '3';
        gr1.assignment_group = 'group_sys_id';
        gr1.short_description = incShrtDesc;
        gr1.description = incDesc;
var rcntWrkNotes = gr1.work_notes.getJournalEntry(1).match(/\n.*/gm).join("\n");
gs.log("RecentWorkNotes:" +rcntWrkNotes);
if(rcntWrkNotes != jsonCmnts){
gs.log("VinodIfExtered" +jsonCmnts);
gr1.work_notes = jsonCmnts;
}
        gr1.setWorkflow(false);
        gr1.update();
 
    } else {
        gr1.initialize();
        gr1.caller_id = 'sys_id';
        if (incState == 'NEW') {
            gr1.state = '1';
gr1.incident_state = '1';
        } else if (incState == 'IN_PROGRESS') {
            gr1.state = '2';
gr1.incident_state = '2';
        } else if (incState == 'ON_HOLD') {
            gr1.state = '3';
gr1.incident_state = '3';
            gr1.hold_reason = '4';
            var dueDt = new GlideDateTime();
            dueDt.addDays(3);
            gr1.follow_up = dueDt;
        } else if (incState == 'CLOSED') {
            gr1.state = '6';
gr1.incident_state = '6';
        }
        gr1.contact_type = 'self-service';
        gr1.impact = '3';
        gr1.urgency = '3';
        gr1.assignment_group = 'group_sys_id';
        gr1.short_description = incShrtDesc;
        gr1.description = incDesc;
        gr1.u_fusion_case_id = incCaseId;
        gr1.work_notes = jsonCmnts;
        gr1.insert();
 
    }
}
13 REPLIES 13

Hi @Mark Manders ,

I have kept the log statements for both jsonCmnts and rcntWrkNotes and I see there is no difference but still it is entering to IF and comments getting updated.

The regex I applied is to remove the user details and timestamp information from the work notes.

Regards,
Vinod Kumar.

Put the two strings (jsonCmnts and rcntWrkNotes) next to each other in an online compare tool and check if they are really the same and don't contain any spaces, line breaks or other hidden content. 

I do agree with Ankur that this should be handled in the other system (it was already send, why send it again). 
You can also try to query to the sys_journal_field table and compare the exact value in the latest work notes 'value' field which gets you around having to put the regex on there (I do think that could be causing the issue).


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hi @Mark Manders,

I already tried comparing both the comments in difference checker and could see there is no difference but still no luck.

I asked Trustwave team to fix that form their end and they said that it cannot be done and asked us to build the logic to skip the comments.

The logic which I build looks promising but not sure what I am missing here.

Thanks & Regards,
Vinod Kumar.

Renat Akhmedov
Tera Contributor

Hi, please take a look at the script below, I have updated your script, 

 

var r = new sn_ws.RESTMessageV2('TrustWave', 'Sample');
var response = r.execute();
var responseBody = response.getBody();
var jsonData = JSON.parse(responseBody);

for (var i = 0; i < jsonData.items.length; i++) {
    var incState = jsonData.items[i]['status'];
    var incShrtDesc = jsonData.items[i]['subject'];
    var incDesc = jsonData.items[i]['description'];
    var incCaseId = jsonData.items[i]['number'];
    var incCmnts = jsonData.items[i]['notes'];  
    var jsonCmnts = JSON.stringify(incCmnts).trim();  // Trim extra spaces

    gs.log(incCaseId);
    
    var gr1 = new GlideRecord('incident');
    gr1.addQuery('u_fusion_case_id', incCaseId);
    gr1.query();

    if (gr1.next()) {
        gr1.caller_id = 'sys_id';
        
        // Update state mapping
        var stateMapping = {
            'NEW': '1',
            'IN_PROGRESS': '2',
            'ON_HOLD': '3',
            'CLOSED': '6'
        };
        if (incState in stateMapping) {
            gr1.state = stateMapping[incState];
            gr1.incident_state = stateMapping[incState];
        }
        
        if (incState == 'ON_HOLD') {
            gr1.hold_reason = '4';
            var dueDt = new GlideDateTime();
            dueDt.addDays(3);
            gr1.follow_up = dueDt;
        }

        // Retrieve the most recent work note properly
        var recentWorkNotes = "";
        var grJournal = new GlideRecord('sys_journal_field');
        grJournal.addQuery('element_id', gr1.sys_id);
        grJournal.addQuery('element', 'work_notes');
        grJournal.orderByDesc('sys_created_on'); // Get latest entry
        grJournal.query();
        
        if (grJournal.next()) {
            recentWorkNotes = grJournal.value.trim();
        }

        gs.log("Recent Work Notes: " + recentWorkNotes);
        gs.log("Incoming Comment: " + jsonCmnts);

        // Only update if new comment is different from the latest work notes
        if (recentWorkNotes !== jsonCmnts) {
            gs.log("Updating work notes...");
            gr1.work_notes = jsonCmnts;
            gr1.update();
        } else {
            gs.log("Skipping duplicate work notes update for " + incCaseId);
        }
    } else {
        // Create a new incident if not found
        gr1.initialize();
        gr1.caller_id = 'sys_id';
        gr1.short_description = incShrtDesc;
        gr1.description = incDesc;
        gr1.u_fusion_case_id = incCaseId;
        gr1.contact_type = 'self-service';
        gr1.impact = '3';
        gr1.urgency = '3';
        gr1.assignment_group = 'group_sys_id';
        gr1.work_notes = jsonCmnts;
        gr1.insert();
    }
}

 

 

It looks like the logic for comparing recent work notes with the incoming comments from "TrustWave" is not correctly preventing duplicate updates. As I understand the issue is likely due to how you're retrieving the most recent work_notes entry and formatting it before comparison. I have fixed it, please test it, it should work for you,


If it was helpful - please mark this comment as helpful,


Best regards,
Renat Akhmedov