How to send Group approvals based on Requested for's network

Khaja Shaik
Tera Contributor


Hi All,

I have a below requirement:
we have 3 networks OOG, COR and CON for the requested for's company and each network has separate group like OOG Group, COR Group and CON Group.
When the Requested for's network is CON need to send approval to CON Group, in same way based on the requested for network need to send corresponding Group approvals. I am using below script in the Approval - Group activity of the workflow. 

Issue: The group approval is not triggering and it is skipping this activity through approved path and moving to the next activity.
Note: I am getting below warning message in the logs.

KhajaShaik_0-1747037157470.png

 

 

Please correct If I have done any mistake in the script.

Thanks in advance!!

 

Script used in the Approval - Group activit:

var answer = [];

var oog = 'ae506541d7225dcf871dc849cebb35fc'; //OOG Group sys id
var cor = 'bgskydkts71dae506541c849cebb35b9'; //COR Group sys id
var con = '56121503875ae506541c849cebb356a';//CON Group sys id

var requestedFor = current.variables.requested_for;
if (requestedFor) {
    var agency = new GlideRecord('core_company');
    if (agency.get(requestedFor.company)) {
        var network = agency.u_network_name;
        if (network) {

            gs.log("Network: " + network + "COR Group: " + cor);
            if (network == 'cor') {

                answer.push(cor);
            } else if (network == 'con') {
                answer.push(con);

            } else if (network == 'oog') {
                answer.push(oog);

            } else {

                current.work_notes = "otrer network";
                current.update(); // Save the change to work notes
            }

        } else {

            current.work_notes = "Network is not specified.";
            current.update(); // Save the change to work notes
        }
    } else {
        current.work_notes = "Agency is not specified.";
        current.update(); // Save the change to work notes
    }

}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Khaja Shaik 

I will suggest to have workflow IF activity which checks if approval has to go or not

that activitiy will return 'yes' or 'no'

If it returns 'yes' then take that to Group Approval

If it returns 'no' then skip the approval and update the work notes

Workflow IF Activity

answer = ifScript()

function ifScript() {

    var arr = [];

    var oog = 'ae506541d7225dcf871dc849cebb35fc'; // OOG Group sys_id
    var cor = 'bgskydkts71dae506541c849cebb35b9'; // COR Group sys_id
    var con = '56121503875ae506541c849cebb356a'; // CON Group sys_id

    var requestedFor = current.variables.requested_for;

    if (requestedFor) {
        var user = new GlideRecord('sys_user');
        if (user.get(requestedFor)) {
            var companyId = user.getValue('company');
            var agency = new GlideRecord('core_company');
            if (agency.get(companyId)) {
                var network = agency.getValue('u_network_name');
                if (network) {
                    network = network.toLowerCase();
                    gs.log("Network: " + network);

                    if (network == 'cor') {
                        arr.push(cor);
                    } else if (network == 'con') {
                        arr.push(con);
                    } else if (network == 'oog') {
                        arr.push(oog);
                    } else {
                        gs.log("Unknown network: " + network);
                    }
                } else {
                    gs.log("Network is not specified for company: " + companyId);
                }
            } else {
                gs.log("Company not found for requested_for user.");
            }
        } else {
            gs.log("Requested for user not found.");
        }
    } else {
        gs.log("Requested for is empty.");
    }
    if (arr.length > 0)
        return 'yes';
    else
        return 'no';
}

Workflow Group Approval Activity script

answer = [];

var oog = 'ae506541d7225dcf871dc849cebb35fc'; // OOG Group sys_id
var cor = 'bgskydkts71dae506541c849cebb35b9'; // COR Group sys_id
var con = '56121503875ae506541c849cebb356a'; // CON Group sys_id

var requestedFor = current.variables.requested_for;

if (requestedFor) {
    var user = new GlideRecord('sys_user');
    if (user.get(requestedFor)) {
        var companyId = user.getValue('company');
        var agency = new GlideRecord('core_company');
        if (agency.get(companyId)) {
            var network = agency.getValue('u_network_name');
            if (network) {
                network = network.toLowerCase();
                gs.log("Network: " + network);

                if (network == 'cor') {
                    answer.push(cor);
                } else if (network == 'con') {
                    answer.push(con);
                } else if (network == 'oog') {
                    answer.push(oog);
                } else {
                    gs.log("Unknown network: " + network);
                }
            } else {
                gs.log("Network is not specified for company: " + companyId);
            }
        } else {
            gs.log("Company not found for requested_for user.");
        }
    } else {
        gs.log("Requested for user not found.");
    }
} else {
    gs.log("Requested for is empty.");
}

AnkurBawiskar_0-1747040256867.png

 

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

6 REPLIES 6

Khaja Shaik
Tera Contributor

Hi All,

Thank you for your reply!
I have changed the my script like below and removed the all the else conditions now its working fine.
I want to update the RITM work notes like "Waiting for approvals". If I try to put this code(current.work_notes = "Waiting for approvals.";
current.update();) in side the conditions it is not triggering approvals.

Please correct below script for the Work notes updating.

var answer = [];

var oog = 'ae506541d7225dcf871dc849cebb35fc'; //OOG Group sys id
var cor = 'bgskydkts71dae506541c849cebb35b9'; //COR Group sys id
var con = '56121503875ae506541c849cebb356a';//CON Group sys id

var requestedFor = current.variables.requested_for;
if (requestedFor) {
    var agency = new GlideRecord('core_company');
    if (agency.get(requestedFor.company)) {
        var network = agency.u_network_name;
        if (network) {

           
            if (network == 'cor') {

                answer.push(cor);
            } else if (network == 'con') {
                answer.push(con);

            } else if (network == 'oog') {
                answer.push(oog);

            }

        }
    }

}

@Khaja Shaik As far as I know, you cannot modify the records inside approval script using current object.

Either you need to use GlideRecord and query the record instead of using current object or use script block.