Business Rule- generate an automatic comment for all RITM approval and rejection emails

curbabel
Tera Contributor

Hello! I want to update this #BusinessRule in my Approval table to do the following: 

Picture20.png

 

I want it to generate an automatic comment for all my RITM approval and rejection email notifications, simply saying: "RITM### has been approved/rejected by APPROVER."

 

The script below is not retriveing the correct sys_id Approver Name.
In addition, I need help breaking up the script to generate an automatic message for the Approved RITM's and a seperate one for the Rejected RITM's. 

 

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

    gs.info('Mj08 Inside BR');
    var gr = new GlideRecord("sc_req_item");
    gr.addQuery("sys_id", current.sysapproval);
    gr.query();
    if (gr.next()) {
        var commentsfull = current.comments.getJournalEntry(1);

        if (current.approval_source == 'email') {
            var commentswithoutref = commentsfull.substring(0, commentsfull.indexOf('Ref:MSG'));
            gs.log("//test//" + commentswithoutref);

            gr.comments = commentswithoutref;
        } else

        {
            //gr.comments = commentsfull;
            var appr = new GlideRecord('sysapproval_approver');
            appr.addQuery('sysapproval', gr.sys_id);
            appr.addEncodedQuery('state=approved^NQstate=rejected');
            appr.query();
            if (appr.next()) {
                var approverName = appr.approver.getDisplayValue();
            }
            gr.comments = gr.number + " has been approved by " + approverName;
        }
        gr.update();

    }







})(current, previous);

 

 

This is what my approval and rejection emails currently look like:

 

Picture21.png

 

Picture22.png

 

Can someone please help me.

2 ACCEPTED SOLUTIONS

johnfeist
Mega Sage
Mega Sage

Hi Lisa,

Based on your description and code I'm assuming that the issue with the name is when the approval was not done by email (your else condition).  I'm curious why you are requerying sysapproval_approver since current should be the record of the approval.  Have you tried just having the else condition just being

var approverName = current.approver.getDisplayValue();
gr.comments = gr.number + " has been approved by " + approverName;

I can see cases where if there are multiple approvers for one requested item your logic could get the wrong approver.

 

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

View solution in original post

Hi Lisa,

If you are referring to the additional comment,  just take the simple approach

if (current.state == "approved") {
    gr.comments = gr.number + " has been approved by " + approverName;
}
else {
   gr.comments = gr.number + " has been rejected by " + approverName;
}

If you are looking to send different emails for approved and rejected, add a State = Approved  condition to the existing notification.  Then make a copy of that notification where the state condition = Rejected.  From there you can set the text for each.  The alternative is have the body of the notification set by an email script.  I suspect that once you have the correct message entered as the comment, the rest will resolve automatically.

 

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

View solution in original post

4 REPLIES 4

johnfeist
Mega Sage
Mega Sage

Hi Lisa,

Based on your description and code I'm assuming that the issue with the name is when the approval was not done by email (your else condition).  I'm curious why you are requerying sysapproval_approver since current should be the record of the approval.  Have you tried just having the else condition just being

var approverName = current.approver.getDisplayValue();
gr.comments = gr.number + " has been approved by " + approverName;

I can see cases where if there are multiple approvers for one requested item your logic could get the wrong approver.

 

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hello Johnfeist! Thank you for responding. This helped me get the correct Approver.

 

However, for my Rejection emails I would like for a message to say "RITM### - has been jected by - Approver" are you able to help me with this portion? 

Hi Lisa,

If you are referring to the additional comment,  just take the simple approach

if (current.state == "approved") {
    gr.comments = gr.number + " has been approved by " + approverName;
}
else {
   gr.comments = gr.number + " has been rejected by " + approverName;
}

If you are looking to send different emails for approved and rejected, add a State = Approved  condition to the existing notification.  Then make a copy of that notification where the state condition = Rejected.  From there you can set the text for each.  The alternative is have the body of the notification set by an email script.  I suspect that once you have the correct message entered as the comment, the rest will resolve automatically.

 

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Bert_c1
Kilo Patron

Hi Lisa,

 

johnfeist makes valid comments.  Reviewing you script (assuming it works for 'approved' state) additional logic is needed for the 'rejected' state.  See following example:

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

    gs.info('Mj08 Inside BR');
    var gr = new GlideRecord("sc_req_item");
    gr.addQuery("sys_id", current.sysapproval);
    gr.query();
    if (gr.next()) {
        var commentsfull = current.comments.getJournalEntry(1);

        if (current.approval_source == 'email') {
            var commentswithoutref = commentsfull.substring(0, commentsfull.indexOf('Ref:MSG'));
            gs.info("Mj08: commentswithoutref = " + commentswithoutref);

            gr.comments = commentswithoutref;
        } else {
            //gr.comments = commentsfull;
            var appr = new GlideRecord('sysapproval_approver');
            appr.addQuery('sysapproval', gr.sys_id);
			// find those where state is 'approved' or 'rejected'
            appr.addEncodedQuery('state=approved^NQstate=rejected');
            appr.query();
			var approverName = 'Not Found';		// initialize outside of "if" so lifetime lasts outside of "if"
            if (appr.next()) {
                approverName = appr.approver.getDisplayValue();
            }
			if (appr.state == 'approved')
				gr.comments = gr.number + " has been approved by " + approverName;
			if (appr.state == 'rejected')
				gr.comments = gr.number + " has been rejected by " + approverName;
        }
        gr.update();

    }

})(current, previous);

 

your encodedQuery value "'state=approved^NQstate=rejected" use of 'NQ' is not familiar, my test using that returned records with either value. But seems to be what you want.