I am trying to update the case comments when an incident is opened for the case but struggling to get it working!!

KELIRL
Kilo Expert

I have a requirement to notify a customer via the 'additional comments' in the case record, when there is an incident created for the original case record in CMS using the Service Management Integration plugin..

I have been trying to use a 

Before Insert business rule on the Incident table using this script below (+many more attempts!!)

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

var inc_num = current.number;
var caseGr = new GlideRecord("sn_customerservice_case");
caseGr.addQuery("incident",current.sys_id);
caseGr.addActiveQuery();
caseGr.query();

while(caseGr.next()) {


caseGr.comments= "The following incident has been opened for this case " + current.number + " | " + current.short_description ;
caseGr.update();


}

})(current, previous);

 

Any help would be appreciated

 

Regards,

Seamus

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

comments field is a journal field.

Can you try to use this and check

 

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

var inc_num = current.number;
var caseGr = new GlideRecord("sn_customerservice_case");
caseGr.addQuery("incident",current.sys_id);
caseGr.addActiveQuery();
caseGr.query();

var fieldName = 'comments';

while(caseGr.next()) {

var value = "The following incident has been opened for this case " + current.number + " | " + current.short_description;

caseGr[fieldName].setJournalEntry(value);

caseGr.update();

}

})(current, previous);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Prateek kumar
Mega Sage

Your script should workw with an after BR.

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

var inc_num = current.number;
var caseGr = new GlideRecord("sn_customerservice_case");
caseGr.addQuery("incident",current.sys_id); //I dont see incident as an OOB field, please check this dictionary name something like u_incident if it is custom created
caseGr.addActiveQuery();
caseGr.query();

while(caseGr.next()) {


caseGr.comments= "The following incident has been opened for this case " + current.number + " | " + current.short_description ;
caseGr.update();


}

})(current, previous);

 


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

KELIRL
Kilo Expert

Hi,

 

I tried Ankur's script and it is updating the record however it is also updating all of the case records in the system. So it seems there is an issue with the addQuery statement as per Prateek's comment

caseGr.addQuery("incident",current.sys_id); //I don't see incident as an OOB field, please check this dictionary name something like u_incident if it is custom created.

I struggle to work out how to query the specific case that is related to the incident, does anybody have an example of an addQuery for the case/incident relationship?

 

Thanks in advance,

Seamus

 

 

Try 

caseGr.addQuery("u_incident",current.sys_id); 


Please mark my response as correct and helpful if it helped solved your question.
-Thanks