Copy comments from approval to requested item and from requested item to approval

rody-bjerke
Giga Guru

Hi,

In service portal you have a screen for requested items and one for approvals (the one the approver see).

Is there an easy way to copy the comments from approvals to requested item if someone write something in the approval, and then the other way if someone writes in the activity stream in requested item, copy it from requested item to the approval. (without getting a loop where the message goes back and forward)

Best regards,

1 ACCEPTED SOLUTION

This is the Business Rule running onBefore on sc_task when additional comments change:



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



// This BR is running onBefore on intention, as we need to decide if we wanna update foreign records and cut a specific part of the string before we are saving


var comments = current.comments;


var commentsFromForeignRecord = (comments.startsWith('Customer comments:'));


if (!commentsFromForeignRecord) {


var ritm = current.request_item.getRefRecord();


ritm.comments = 'Supporter comments:\n' + comments;


ritm.update();


}



})(current, previous);



In this specific scenario the customer has the possibility to post comments on RITM level and we want to promote them to sc_task. Other way around the agent is only posting comments on Catalog Task level, which should be available to the customer on the RITM as well.



This is the onBefore BR running on sc_req_item to get the customer comments when additional comments change:



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



// This BR is running onBefore on intention, as we need to decide if we wanna update foreign records and cut a specific part of the string before we are saving


var ctasks = new GlideRecord('sc_task');


ctasks.addQuery('request_item', current.sys_id + '');


ctasks.addActiveQuery();


ctasks.query();


while (ctasks.next()) {


var comments = current.comments;


var commentsAllowedToBeShared = (!comments.startsWith('Supporter comments:'));


if (commentsAllowedToBeShared) {


ctasks.comments = 'Customer comments:\n' + comments;


ctasks.update();


}


}



})(current, previous);


View solution in original post

8 REPLIES 8

This is the Business Rule running onBefore on sc_task when additional comments change:



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



// This BR is running onBefore on intention, as we need to decide if we wanna update foreign records and cut a specific part of the string before we are saving


var comments = current.comments;


var commentsFromForeignRecord = (comments.startsWith('Customer comments:'));


if (!commentsFromForeignRecord) {


var ritm = current.request_item.getRefRecord();


ritm.comments = 'Supporter comments:\n' + comments;


ritm.update();


}



})(current, previous);



In this specific scenario the customer has the possibility to post comments on RITM level and we want to promote them to sc_task. Other way around the agent is only posting comments on Catalog Task level, which should be available to the customer on the RITM as well.



This is the onBefore BR running on sc_req_item to get the customer comments when additional comments change:



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



// This BR is running onBefore on intention, as we need to decide if we wanna update foreign records and cut a specific part of the string before we are saving


var ctasks = new GlideRecord('sc_task');


ctasks.addQuery('request_item', current.sys_id + '');


ctasks.addActiveQuery();


ctasks.query();


while (ctasks.next()) {


var comments = current.comments;


var commentsAllowedToBeShared = (!comments.startsWith('Supporter comments:'));


if (commentsAllowedToBeShared) {


ctasks.comments = 'Customer comments:\n' + comments;


ctasks.update();


}


}



})(current, previous);


That helped alot, thanks


Bruno De Graeve
ServiceNow Employee
ServiceNow Employee

To update the REQ comments with the approval comments, you can write a onbefore BR on the sysapproval_approver table with a Filter Condition "State changes to Rejected"

and a script:

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

// Copy the comments content to task table
var rec = new GlideRecord('task');
rec.addQuery('sys_id',current.document_id);
rec.query();

if (rec.next()) {
rec.comments.setJournalEntry(current.document_id.getDisplayValue()+" rejected with the following reason: " + current.comments);
rec.update();
}

})(current, previous);

From the REQ, you could use another BR to update the RITM.

 

Bruno De Graeve,
Principal Platform Architect, Customer Success, ServiceNow

Manjunatha
Tera Expert

Here is the Business Rule for Copy comments from Approval [sysapproval_approver] to RITM [sc_req_item]

Condition: After, when Additional Comments Changes on Insert and Update

//code

(function executeRule(current, previous ) {
     var compare,approval_comment2,approval_comment,ritm_comment2,ritm_comment;
     approval_comment =current.comments.getJournalEntry(1);

     //Remove timestamp and name from additional comment
     var regex= new RegExp('\\n');
     var i = approval_comment.search(regex);
     if (i>0)
    {
          approval_comment2 = approval_comment.substring(i+1, approval_comment.length);
    }

    var ritm_gr = new GlideRecord('sc_req_item');
    ritm_gr.addQuery('sys_id', current.sysapproval);
    ritm_gr.query();

    if(ritm_gr.next())
   {
          ritm_comment =ritm_gr.comments.getJournalEntry(1);

          //Remove timestamp and name from additional comment
          var i1 = ritm_comment.search(regex);
          if(i1 > 0)
         {
                ritm_comment2 = ritm_comment.substring(i1+1, ritm_comment.length);
         }
         compare = approval_comment2.indexOf(ritm_comment2);

         if(compare == -1) // If No match found
         {
                ritm_gr.comments = approval_comment2.trim();
                ritm_gr.update();
         }
    }


})(current, previous);

 

///////////////////////////

Here is the Business Rule for Copy comments from RITM [sc_req_item] to Approval [sysapproval_approver]

Condition: After, when Additional Comments Changes on Insert and Update

//code

(function executeRule(current, previous) {
        var compare,ritm_comment2,ritm_comment,approval_comment2,approval_comment;
        ritm_comment =current.comments.getJournalEntry(1);

        //Remove timestamp and name from additional comment
        var regex= new RegExp('\\n');
        var i = ritm_comment.search(regex);
        if (i>0)
       {
               ritm_comment2 = ritm_comment.substring(i+1, ritm_comment.length);
       }

       var app_gr = new GlideRecord('sysapproval_approver');
       app_gr.addQuery('sysapproval',current.sys_id);
       app_gr.query();

       if(app_gr.next())
       {
               approval_comment =app_gr.comments.getJournalEntry(1);

               //Remove timestamp and name from additional comment
               var i1 = approval_comment.search(regex);
               if(i1 > 0)
              {
                      approval_comment2 = approval_comment.substring(i1+1, approval_comment.length);
              }
               compare = ritm_comment2.indexOf(approval_comment2);

               if(compare == -1) // If No match found
              {
                      app_gr.comments = ritm_comment2.trim();
                      app_gr.update();
              } 
       }


})(current, previous);