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

@tanguturi0527 

possibly you are getting same comments and hence script is updating it with same value

if that's the case then it's not your fault but something which 3rd party should handle.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar ,

Thanks for the help and suggestion.

Can't we set some logic from our end to skip the comments updation if it is same?

Regards,
Vinod Kumar.

@tanguturi0527 

Ideally it should be handled by the 3rd party team.

It will be an extra effort in ServiceNow to check this

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

Thanks for the suggestion,

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.

Mark Manders
Mega Patron

Since you are getting everything and if new comments, you get the new comments, it is updating them. How are the comments working in Trustwave? What are you getting? Because if you only get the latest comment, you could be missing comments. Or do you retrieve all? 

You need to check on what you get in (log  jsonCmnts content) and validate that against your worknotes (log rcntWrkNotes). Since you are evaluating them against each other and you are doing some regex on your work notes, it could very well be that the content is not exactly the same, including all spaces, line breaks, etc. With your script it really needs to be a one-on-one match, or it isn't seen as the same.


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