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

Ankur Bawiskar
Tera Patron
Tera Patron

@tanguturi0527 

what's the trigger for your business rule or script?

are you pulling the comments from external system ?

what's the business requirement here?

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,

I am using schedule job to trigger the integration and it runs every 1 hr.

Yes, I am pulling all the incident details from the external system and creating it in ServiceNow.
Requirement is to create the incident in ServiceNow when a ticket created in external system. For that "u_fusion_case_id" is the unique key here.
If any incident has the "u_fusion_case_id" then it will update that incident and if not, it will create the new one in ServiceNow.
Here, whenever the business rule runs, the comments are getting updated again and again even when there is no new update in the comment.
As attached, you could see every update has the same comment and client doesn't need it. They need the comment to be updated only when there is a new update.   

Thanks & Regards,
Vinod Kumar.

@tanguturi0527 

so you have a job to insert/update the incident based on "u_fusion_case_id"

Now next what's your Business rule doing?

what's the trigger for it?

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 ,

Sorry, I might have miss guided you. There is no business rule, it's just the scheduled job which will insert or update the records from external system to SNOW.

Regards,
Vinod Kumar.