How to insert the value in "Approval For" field of the sn_vul_change_approval source table

Swati_Jindal
Tera Contributor

I need the value in Approval For field because i have to send notification to the assignment group for Vulnerable Item & Remediation Task requested, approved & reject for Exception request.

Swati_Jindal_0-1744301545799.png

 

1 ACCEPTED SOLUTION

@Swati_Jindal 

you cannot set recipient using email script, only set CC and BCC

Is it the OOTB approval.inserted email which triggers

If yes then there you can add the logic in new notification and trigger it via event Queue on approval table

1) Ensure the OOTB "approval.inserted" notification doesn't trigger for your source table

AnkurBawiskar_0-1744350661479.png

 

2) Ensure the new notification triggers only for your source table and then trigger this via your new business rule on sysapproval_approver and specify your new event name

 

AnkurBawiskar_1-1744350702437.png

 

getRefRecord() -> won't work on document ID field
 

Something like this but please enhance as I am not aware of the table and fields

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

    // Add your code here
    var appr_Record = current.document_id;
    var gr = new GlideRecord("sn_vul_change_approval");
    gr.addQuery("sys_id", appr_Record);
    gr.query();
    if (gr.next()) {
        var arr = [];
        var vul_record = gr.vul_record.getRefRecord();
        var assignmentgroup = vul_record.assignment_group.sys_id;
        var groupMembers = new GlideRecord('sys_user_grmember');
        groupMembers.addQuery('group', group);
        groupMembers.query();
        while (groupMembers.next()) {
            arr.push(groupMembers.getValue('user'));
        }
        gs.eventQueue('eventName', current, arr.toString());
    }

})(current, previous);

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

View solution in original post

5 REPLIES 5

Vishal Jaswal
Giga Sage

Hello @Swati_Jindal 

If you want to set "Approval For" field's value as one of the group "Vulnerable Item & Remediation" member, then that can be done via business rule or a flow.
In case where there are multiple group members, then which one do you want to set as approval for (assuming approval for is a reference field to sys_user table) like based upon their skill set, availability, location etc.


Hope that helps!

Swati_Jindal
Tera Contributor
(function runMailScript(current, template, email, email_action, event) {
 
      var appr_Record = current.document_id.getRefRecord();
    var vul_record = appr_Record.vul_record.getRefRecord();
   
    var assignmentgroup = vul_record.assignment_group.getDisplayValue();
    var group = vul_record.assignment_group;
 

 

            var groupMembers = new GlideRecord('sys_user_grmember');
            groupMembers.addQuery('group', group);
            groupMembers.query();
   

 

            while (groupMembers.next()) {
                   var user = groupMembers.user.getRefRecord();
                 var name = user.name;
       email.addAddress('to', groupMembers.user.email.toString());
 
                }
         
    email.setBody("Hi" + name) ;

 

})(current, template, email, email_action, event);




I am writing this mail script for sending mail to all member of assignment group but i am not getting mail

@Swati_Jindal 

you cannot set recipient using email script, only set CC and BCC

Is it the OOTB approval.inserted email which triggers

If yes then there you can add the logic in new notification and trigger it via event Queue on approval table

1) Ensure the OOTB "approval.inserted" notification doesn't trigger for your source table

AnkurBawiskar_0-1744350661479.png

 

2) Ensure the new notification triggers only for your source table and then trigger this via your new business rule on sysapproval_approver and specify your new event name

 

AnkurBawiskar_1-1744350702437.png

 

getRefRecord() -> won't work on document ID field
 

Something like this but please enhance as I am not aware of the table and fields

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

    // Add your code here
    var appr_Record = current.document_id;
    var gr = new GlideRecord("sn_vul_change_approval");
    gr.addQuery("sys_id", appr_Record);
    gr.query();
    if (gr.next()) {
        var arr = [];
        var vul_record = gr.vul_record.getRefRecord();
        var assignmentgroup = vul_record.assignment_group.sys_id;
        var groupMembers = new GlideRecord('sys_user_grmember');
        groupMembers.addQuery('group', group);
        groupMembers.query();
        while (groupMembers.next()) {
            arr.push(groupMembers.getValue('user'));
        }
        gs.eventQueue('eventName', current, arr.toString());
    }

})(current, previous);

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

@Swati_Jindal 

Hope you are doing good.

Did my reply answer your question?

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