Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Copy Approval Record Comments to Parent Record

Cody Henslee
Kilo Expert

Hello,

I have written a business rule that copies comments from an approval record to the activity stream of the parent record. This took some trial, error, and community support as I am not typically a developer, I just play one since our institution has been forced to down-size over the past year.

My issue now is that I want to augment this business rule so that it does nothing if comments field is blank or the activity stream on the approval record does not have a comments entry.

Currently it adds "Approval Comments:" to the parent record for all approvals rather than approvals that have comments. I feel like I'm missing an additional else statement but I have yet to figure out anything that will work.

(function executeRule(current, previous /*null when async*/ ) {
    var comments;
    if (current.comments != '') {
        comments = current.comments;
    } else {
        comments = current.comments.getJournalEntry(1);
    }
    var gr = current.sysapproval.getRefRecord();
    gr.comments = "Approval Comments: \n\n" + comments;
    gr.update();

})(current, previous);
1 ACCEPTED SOLUTION

Jaspal Singh
Mega Patron
Mega Patron

Hi Cody,

 

Below should help.

(function executeRule(current, previous /*null when async*/ ) {
    var comments;
    if (current.comments != '') {
        comments = current.comments;
    } else {
        comments = current.comments.getJournalEntry(1);
    }
    var gr = current.sysapproval.getRefRecord();
if(comments!='') //if there is something only then if will work
{
    gr.comments = "Approval Comments: \n\n" + comments;
}

else{//do nothing}

    gr.update();

})(current, previous);

View solution in original post

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Hi Cody,

 

Below should help.

(function executeRule(current, previous /*null when async*/ ) {
    var comments;
    if (current.comments != '') {
        comments = current.comments;
    } else {
        comments = current.comments.getJournalEntry(1);
    }
    var gr = current.sysapproval.getRefRecord();
if(comments!='') //if there is something only then if will work
{
    gr.comments = "Approval Comments: \n\n" + comments;
}

else{//do nothing}

    gr.update();

})(current, previous);

Thank you Jaspal for your quick response. Your assistance has helped me find a solution.

I ran into some issues with the script you provided but I was able to find success by making a few slight adjustments. Initially it wasn't copying anything over until I added gr.update(); to the if statement validating that the comments variable is not empty.

(function executeRule(current, previous /*null when async*/ ) {
    var comments;
    if (current.comments != '') {
        comments = current.comments;
    } else {
        comments = current.comments.getJournalEntry(1);
    }
    var gr = current.sysapproval.getRefRecord();
	if (comments !='')
	{
		gr.comments = "Approval Comments: \n\n" + comments;
	gr.update();
	}
	else{
    gr.update();
	}

})(current, previous);

Great to know it worked. You can close the thread by marking thread as correct so that it does not appear in unanswered list.

I am searching for the similar requirement and thankfully I found this thread.

There is a requirement from client to update the RITM comments with the approval comments.

I used the code in this thread and it is working absolutely fine. But there is a small correction needed on this in my scenario.

Well, In a particular RITM If there are 4 approvals having (same)comments in all those approvals then the RITM comments are getting updated 4 times.

Can we make any modifications in this code so that only once the RITM comments will get updated?

@Cody Henslee , @Jaspal Singh . Any help on this?