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

J Siva
Tera Sage

Hi @Khaja Shaik 

I suggest performing the validation in a separate script (If) block and storing the group sys_id in the workflow.scratchpad variable. If the scratchpad variable is not empty, then request group approval; otherwise, skip the group approval.

Try the below script in your script block.

workflow.scratchpad.approval_group = '';

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 (!gs.nil(requestedFor)) {
    var agency = new GlideRecord('core_company');
    if (!gs.nil(agency.get(requestedFor.company))) {
        var network = agency.u_network_name;
        if (!gs.nil(network)) {
            if (network == 'cor') {
                workflow.scratchpad.approval_group = cor;
            } else if (network == 'con') {
                workflow.scratchpad.approval_group = con;
            } else if (network == 'oog') {
                workflow.scratchpad.approval_group = oog;
            } else {
                current.work_notes = "other 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
    }

}

 

 

 

Regards,
Siva

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

@Khaja Shaik 

Thank you for marking my response as helpful.

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

ugutha
Tera Contributor
var answer = []; // Ensure it's an actual JavaScript array
// Define the group sys_ids
var oog = 'ae506541d7225dcf871dc849cebb35fc'; // OOG Group
var cor = 'bgskydkts71dae506541c849cebb35b9'; // COR Group
var con = '56121503875ae506541c849cebb356a';  // CON Group
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 + ''; // Cast to string for safe comparison
       gs.log("Network: " + network);
       if (network.toLowerCase() == 'cor') {
           answer.push(cor);
       } else if (network.toLowerCase() == 'con') {
           answer.push(con);
       } else if (network.toLowerCase() == 'oog') {
           answer.push(oog);
       } else {
           current.work_notes = "Other network";
           current.update(); // Log unexpected network
       }
   } else {
       current.work_notes = "Agency is not specified or not found.";
       current.update();
   }
} else {
   current.work_notes = "Requested for is not specified.";
   current.update();
}

Hi @Khaja Shaik ,

 

Use the below script.
var answer = []; // Ensure it's an actual JavaScript array

// Define the group sys_ids
var oog = 'ae506541d7225dcf871dc849cebb35fc'; // OOG Group
var cor = 'bgskydkts71dae506541c849cebb35b9'; // COR Group
var con = '56121503875ae506541c849cebb356a';  // CON Group

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 + ''; // Cast to string for safe comparison
       gs.log("Network: " + network);

       if (network.toLowerCase() == 'cor') {
           answer.push(cor);
       } else if (network.toLowerCase() == 'con') {
           answer.push(con);
       } else if (network.toLowerCase() == 'oog') {
           answer.push(oog);
       } else {
           current.work_notes = "Other network";
           current.update(); // Log unexpected network
       }
   } else {
       current.work_notes = "Agency is not specified or not found.";
       current.update();
   }
} else {
   current.work_notes = "Requested for is not specified.";
   current.update();
}


Please hit helpful if the solution works for you.