Urgent - Workflow Script - Assignment group need to set properly based on Company code

AbdurRahmanSnow
Giga Guru

Good evening. I have an urgent P2 incident.
The requirement is, when Catalog form is submitted, based on company code, it should set different Assignment groups.
For now, the below is working perfectly fine.

AU, IN, HK, SG tickets should assign to APAC SSC SAP MDM ENG
CN, KR, TW, MY, JP tickets should assign to APAC SSC SAP MDM

But for other company codes, example., when INF1 or AE24 is selected or other, it is coming to above APAC SSC SAP MDM ENG. I have found the issue in Workflow script that these are commented out.

For example, INF1 or AE24 needs to go to EMEA SSC SAP MDM group and not to APAC SSC SAP MDM ENG. 
The script is attached below. Please help me out with this.
All the remaining company codes need to uncommented and should get assigned properly properly to the remaining groups. Please help me out. This is urgent.

setGroup();

function setGroup() {

    var sysID = '';

    // var sapCodeENG = ["INF2", "DM05", "TC03", "AU25", "HK05", "HK09", "TC07", "IN15", "IN24", "SG01", "IN16", "SG05", "SG06", "SG14", "Z016"];
    var sapCodeENG = ["AU25", "HK05", "HK09", "IN15", "IN24", "SG01", "IN16", "SG05", "SG06", "SG14"];

    // var sapCode = ["JP04", "JP05", "JP06", "KR02", "KR03", "MY01", "MY02", "IN19", "CN04", "CN10", "CN19", "TW02", "TW03"];
    var sapCode = ["JP04", "JP05", "JP06", "KR02", "KR03", "MY01", "MY02", "CN04", "CN10", "CN19", "TW02", "TW03"];

    //EMEA Region conditions
    workflow.info('submitted request type Line No. 14: ' + current.variables.request_type);
    if (current.variables.request_type == 'New' && current.variables.company_code.u_ssc == 'ZGBR') {
        sysID = '3e7fde881bd6bc9004bb1fc3b24bcbc0'; // EMEA SSC SAP MDM Group
    }

    if (current.variables.request_type == 'Extend' && current.variables.what_company_code_are_you_extending.u_ssc == 'ZGBR') {
        sysID = '3e7fde881bd6bc9004bb1fc3b24bcbc0'; // EMEA SSC SAP MDM Group
    }

    // New and Extend US Region conditions
    if (current.variables.request_type == 'New' && (current.variables.company_code.u_ssc == 'ZUSA' || current.variables.company_code.u_ssc == 'ZUSI')) {
        sysID = '027f9e881bd6bc9004bb1fc3b24bcb71'; // US SSC SAP MDM Group
    }

    if ((current.variables.request_type == 'Extend' && current.variables.what_company_code_are_you_extending.u_ssc == 'ZUSA') || (current.variables.request_type == 'Extend' && (current.variables.what_company_code_are_you_extending.u_ssc == 'ZUSI'))) {
        sysID = '027f9e881bd6bc9004bb1fc3b24bcb71'; // US SSC SAP MDM Group
    }

    if (current.variables.account_group == 'ZAUS') {
        sysID = 'fd7f5e881bd6bc9004bb1fc3b24bcbdf'; // US T&F SAP MDM Group
    }




    //APAC Region
    // New Amend GRQ0324499

    // Set Assignment group  "APAC SSC SAP MDM ENG"
    var sapIndex = 0;
    var sapLength = sapCodeENG.length;

    //     for (sapIndex = 0; sapIndex < sapLength; sapIndex++) {

    //     if (current.variables.company_code.u_company_code == sapCodeENG[sapIndex]) {
    //         sysID = '7f629a941b459d906b7ab165464bcbb5'; // APAC SSC SAP MDM ENG
    //     }
    // }
    workflow.info("Line 52 Country code");
    for (sapIndex = 0; sapIndex < sapCodeENG.length; sapIndex++) {
        if (current.variables.company_code.u_company_code == sapCodeENG[sapIndex]) {
            workflow.info("Line 55 Country code" + sapCodeENG[sapIndex]);
            sysID = '7f629a941b459d906b7ab165464bcbb5'; // APAC SSC SAP MDM ENG
        }
    }

    //Set Assignment group "APAC SSC SAP MDM"  
    var sapCodeIndex = 0;
    var sapCodeLength = sapCode.length;

    // for (sapCodeIndex = 0; sapCodeIndex < sapCodeLength; sapCodeIndex++) {

    //     if (current.variables.company_code.u_company_code == sapCode[sapCodeIndex]) {
    //         sysID = '667fde881bd6bc9004bb1fc3b24bcb7a'; // APAC SSC SAP MDM Group
    //     }
    // }
    workflow.info("Line 69 Country code");
    for (sapCodeIndex = 0; sapCodeIndex < sapCode.length; sapCodeIndex++) {
        if (current.variables.company_code.u_company_code == sapCode[sapCodeIndex]) {
            workflow.info("Line 73 Country code" + sapCode[sapCodeIndex]);
            sysID = '667fde881bd6bc9004bb1fc3b24bcb7a'; // APAC SSC SAP MDM Group
        }
    }



    //     if (current.variables.request_type == 'New' && current.variables.company_code.u_ssc == 'ZSGP') {
    //         sysID = '667fde881bd6bc9004bb1fc3b24bcb7a'; // APAC SSC SAP MDM Group
    //     }

    //     if (current.variables.request_type == 'Extend' && current.variables.what_company_code_are_you_extending.u_ssc == 'ZSGP') {
    //         sysID = '667fde881bd6bc9004bb1fc3b24bcb7a'; // APAC SSC SAP MDM
    //     } Comented for this ticket: GRQ0324499


    if (current.variables.request_type == 'Modify' && current.variables.account_group != 'ZAUS') {
        var nCountZGBR = 0;
        var nCountZUSA = 0;
        var nCountZSGP = 0;
        var nCount = 0;
        var sGroupSysID = '3e7fde881bd6bc9004bb1fc3b24bcbc0'; // EMEA SSC SAP MDM Group - Default Group SysID


        var grSSC = new GlideRecord('u_sap_company_code_to_vendor');
        grSSC.addQuery('u_vendor_number', current.variables.sap_vendor_number);
        grSSC.query('u_active', true);
        grSSC.query();

        var grCount = grSSC.getRowCount();

        while (grSSC.next()) {
            nCount++;

            if (grSSC.u_company_code.u_ssc == 'ZGBR') {
                nCountZGBR = nCountZGBR + 1;
                gs.info('EMEACount ' + nCountZGBR);
            }
            if (grSSC.u_company_code.u_ssc == 'ZUSA' || grSSC.u_company_code.u_ssc == 'ZUSI') {
                nCountZUSA = nCountZUSA + 1;
                gs.info('USCount ' + nCountZUSA);
            }
            if (grSSC.u_company_code.u_ssc == 'ZSGP') {
                nCountZSGP = nCountZSGP + 1;
                gs.info('APACCount ' + nCountZSGP);
            }
        }
        if (nCountZGBR == nCount) {
            sGroupSysID = '3e7fde881bd6bc9004bb1fc3b24bcbc0'; // EMEA SSC SAP MDM Group
        } else if (nCountZUSA == nCount) {
            sGroupSysID = '027f9e881bd6bc9004bb1fc3b24bcb71'; // US SSC SAP MDM Group
        }

        // if (current.variables.account_group == 'ZTRD' && current.variables.request_type == 'Modify') {
        //     workflow.info('Line 126 executed for ZTRD');
        //     sGroupSysID = '667fde881bd6bc9004bb1fc3b24bcb7a';
        // }

        var countryGroup = isPartOfsapCode(current.variables.company_code.getDisplayValue());
        workflow.info('Country group is ' + countryGroup.toString());

        if (current.variables.account_group == 'ZTRD') {
            workflow.info('Line 132' + current.variables.request_type);
            if (current.variables.account_group == 'ZTRD' && (current.variables.request_type == 'Modify' && countryGroup == '667fde881bd6bc9004bb1fc3b24bcb7a')) {
                workflow.info('Line 134' + current.variables.request_type);
                sGroupSysID = '667fde881bd6bc9004bb1fc3b24bcb7a'; //APAC SSC SAP MDM
            } else {
                workflow.info('Line 140' + current.variables.request_type);
                sGroupSysID = '7f629a941b459d906b7ab165464bcbb5'; //APAC SSC SAP MDM ENG
            }
        }




        //else if (nCountZSGP == nCount) {
        //             sGroupSysID = '667fde881bd6bc9004bb1fc3b24bcb7a'; // APAC SSC SAP MDM Group
        //         }     Comented for this ticket: GRQ0324499


        //Use the Group SysID variable sGroupSysID where required.


        sysID = sGroupSysID;
    }
    workflow.info('Extend company code without SSC: ' + current.variables.what_company_code_are_you_extending);
    workflow.info('Extend company code with SSC: ' + current.variables.what_company_code_are_you_extending.u_ssc);
   
    var extendCompanyCodeGroup = isPartOfsapCode(current.variables.what_company_code_are_you_extending.getDisplayValue());
    workflow.info('The Extend code is: ' + extendCompanyCodeGroup + 'Line 146' + current.variables.request_type);
   
    if (current.variables.request_type == 'Extend') {
        workflow.info('Extend group' + extendCompanyCodeGroup);
       if( current.variables.account_group == 'ZTRD' && extendCompanyCodeGroup == '667fde881bd6bc9004bb1fc3b24bcb7a') {
        workflow.info('Extend group 167' + extendCompanyCodeGroup);
        workflow.info('Request type ' + current.variables.request_type + ' group returned: ' + extendCompanyCodeGroup);
        sysID = '667fde881bd6bc9004bb1fc3b24bcb7a'; //APAC SSC SAP MDM
    } else {
        workflow.info('Extend group 171' + extendCompanyCodeGroup);
        workflow.info('Line 172' + current.variables.request_type);
        sysID = '7f629a941b459d906b7ab165464bcbb5'; //APAC SSC SAP MDM ENG
    }
}

workflow.scratchpad.group = sysID;
current.assignment_group = sysID;

//Not best practice, should be in a system property.

current.business_service = 'a5b70f0ddbc0fb442cb33307f496192e'; //Finance (SAP) Services
current.u_service = '8bcf9d680f3b52007b3a3e7ce1050e4e'; // Purchase to Pay
current.u_application = '8da228addbe5fa80b82c79600f961953'; // P1 - Maintain Vendor Details
current.u_category = 'bdc925391b5d7f008d22c9506e4bcbc9'; // Other "Please specify in description"

}

function isPartOfsapCode(countryCode) {
    workflow.info('The country code is: ' + countryCode);
    var sGroupSysID = '3e7fde881bd6bc9004bb1fc3b24bcbc0';
    var sysID = sGroupSysID;


    var sapCodeENG = ["AU25", "HK05", "HK09", "IN15", "IN24", "SG01", "IN16", "SG05", "SG06", "SG14"];

    var sapCode = ["JP04", "JP05", "JP06", "KR02", "KR03", "MY01", "MY02", "CN04", "CN10", "CN19", "TW02", "TW03"];

    for (var sapCodeIndex = 0; sapCodeIndex < sapCode.length; sapCodeIndex++) {
        //if (current.variables.company_code.u_company_code == sapCode[sapCodeIndex]) {
        if (countryCode == sapCode[sapCodeIndex]) {
            return '667fde881bd6bc9004bb1fc3b24bcb7a'; // APAC SSC SAP MDM
        }
        // }
    }


    for (var sapIndex = 0; sapIndex < sapCodeENG.length; sapIndex++) {
        //if (current.variables.company_code.u_company_code == sapCodeENG[sapIndex]) {

        if (countryCode == sapCodeENG[sapIndex]) {
            return '7f629a941b459d906b7ab165464bcbb5'; // APAC SSC SAP MDM ENG
        }
        //}
    }

}



6 REPLIES 6

blaze1
Tera Contributor

To address the issue and ensure that the tickets with company codes like INF1 or AE24 are assigned to the correct EMEA SSC SAP MDM group, we need to uncomment and add the appropriate checks for these codes.


The key is to check for these company codes and set the assignment group accordingly. Below is an updated version of your script with the necessary changes:


setGroup();


function setGroup() {
    var sysID = '';


    // Previously commented out: Add the company codes for EMEA region (INF1, AE24, etc.)
    var sapCodeENG = ["AU25", "HK05", "HK09", "IN15", "IN24", "SG01", "IN16", "SG05", "SG06", "SG14"];
    var sapCode = ["JP04", "JP05", "JP06", "KR02", "KR03", "MY01", "MY02", "CN04", "CN10", "CN19", "TW02", "TW03"];


    // EMEA Region conditions - for company codes INF1, AE24, etc.
    workflow.info('submitted request type Line No. 14: ' + current.variables.request_type);
    if (current.variables.request_type == 'New' && current.variables.company_code.u_ssc == 'ZGBR') {
        sysID = '3e7fde881bd6bc9004bb1fc3b24bcbc0'; // EMEA SSC SAP MDM Group
    }


    if (current.variables.request_type == 'Extend' && current.variables.what_company_code_are_you_extending.u_ssc == 'ZGBR') {
        sysID = '3e7fde881bd6bc9004bb1fc3b24bcbc0'; // EMEA SSC SAP MDM Group
    }


    // New and Extend US Region conditions
    if (current.variables.request_type == 'New' && (current.variables.company_code.u_ssc == 'ZUSA' || current.variables.company_code.u_ssc == 'ZUSI')) {
        sysID = '027f9e881bd6bc9004bb1fc3b24bcb71'; // US SSC SAP MDM Group
    }


    if ((current.variables.request_type == 'Extend' && current.variables.what_company_code_are_you_extending.u_ssc == 'ZUSA') || (current.variables.request_type == 'Extend' && (current.variables.what_company_code_are_you_extending.u_ssc == 'ZUSI'))) {
        sysID = '027f9e881bd6bc9004bb1fc3b24bcb71'; // US SSC SAP MDM Group
    }


    if (current.variables.account_group == 'ZAUS') {
        sysID = 'fd7f5e881bd6bc9004bb1fc3b24bcbdf'; // US T&F SAP MDM Group
    }


    // APAC Region
    var sapIndex = 0;
    var sapLength = sapCodeENG.length;


    workflow.info("Line 52 Country code");
    for (sapIndex = 0; sapIndex < sapLength; sapIndex++) {
        if (current.variables.company_code.u_company_code == sapCodeENG[sapIndex]) {
            workflow.info("Line 55 Country code" + sapCodeENG[sapIndex]);
            sysID = '7f629a941b459d906b7ab165464bcbb5'; // APAC SSC SAP MDM ENG
        }
    }


    var sapCodeIndex = 0;
    var sapCodeLength = sapCode.length;


    workflow.info("Line 69 Country code");
    for (sapCodeIndex = 0; sapCodeIndex < sapCodeLength; sapCodeIndex++) {
        if (current.variables.company_code.u_company_code == sapCode[sapCodeIndex]) {
            workflow.info("Line 73 Country code" + sapCode[sapCodeIndex]);
            sysID = '667fde881bd6bc9004bb1fc3b24bcb7a'; // APAC SSC SAP MDM Group
        }
    }


    // EMEA Region - INF1, AE24, etc. should be assigned to EMEA SSC SAP MDM
    var emeaCodes = ["INF1", "AE24"];  // Add any other EMEA codes as needed
    for (var i = 0; i < emeaCodes.length; i++) {
        if (current.variables.company_code.u_company_code == emeaCodes[i]) {
            sysID = '3e7fde881bd6bc9004bb1fc3b24bcbc0'; // EMEA SSC SAP MDM Group
        }
    }


    if (current.variables.request_type == 'Modify' && current.variables.account_group != 'ZAUS') {
        var nCountZGBR = 0;
        var nCountZUSA = 0;
        var nCountZSGP = 0;
        var nCount = 0;
        var sGroupSysID = '3e7fde881bd6bc9004bb1fc3b24bcbc0'; // Default Group SysID for EMEA


        var grSSC = new GlideRecord('u_sap_company_code_to_vendor');
        grSSC.addQuery('u_vendor_number', current.variables.sap_vendor_number);
        grSSC.query('u_active', true);
        grSSC.query();


        var grCount = grSSC.getRowCount();


        while (grSSC.next()) {
            nCount++;


            if (grSSC.u_company_code.u_ssc == 'ZGBR') {
                nCountZGBR = nCountZGBR + 1;
                gs.info('EMEACount ' + nCountZGBR);
            }
            if (grSSC.u_company_code.u_ssc == 'ZUSA' || grSSC.u_company_code.u_ssc == 'ZUSI') {
                nCountZUSA = nCountZUSA + 1;
                gs.info('USCount ' + nCountZUSA);
            }
            if (grSSC.u_company_code.u_ssc == 'ZSGP') {
                nCountZSGP = nCountZSGP + 1;
                gs.info('APACCount ' + nCountZSGP);
            }
        }
        if (nCountZGBR == nCount) {
            sGroupSysID = '3e7fde881bd6bc9004bb1fc3b24bcbc0'; // EMEA SSC SAP MDM Group
        } else if (nCountZUSA == nCount) {
            sGroupSysID = '027f9e881bd6bc9004bb1fc3b24bcb71'; // US SSC SAP MDM Group
        }


        var countryGroup = isPartOfsapCode(current.variables.company_code.getDisplayValue());
        workflow.info('Country group is ' + countryGroup.toString());


        sysID = sGroupSysID;
    }


    workflow.scratchpad.group = sysID;
    current.assignment_group = sysID;


    current.business_service = 'a5b70f0ddbc0fb442cb33307f496192e'; // Finance (SAP) Services
    current.u_service = '8bcf9d680f3b52007b3a3e7ce1050e4e'; // Purchase to Pay
    current.u_application = '8da228addbe5fa80b82c79600f961953'; // P1 - Maintain Vendor Details
    current.u_category = 'bdc925391b5d7f008d22c9506e4bcbc9'; // Other "Please specify in description"
}


function isPartOfsapCode(countryCode) {
    workflow.info('The country code is: ' + countryCode);
    var sGroupSysID = '3e7fde881bd6bc9004bb1fc3b24bcbc0';
    var sysID = sGroupSysID;


    var sapCodeENG = ["AU25", "HK05", "HK09", "IN15", "IN24", "SG01", "IN16", "SG05", "SG06", "SG14"];
    var sapCode = ["JP04", "JP05", "JP06", "KR02", "KR03", "MY01", "MY02", "CN04", "CN10", "CN19", "TW02", "TW03"];


    for (var sapCodeIndex = 0; sapCodeIndex < sapCode.length; sapCodeIndex++) {
        if (countryCode == sapCode[sapCodeIndex]) {
            return '667fde881bd6bc9004bb1fc3b24bcb7a'; // APAC SSC SAP MDM
        }
    }


    for (var sapIndex = 0; sapIndex < sapCodeENG.length; sapIndex++) {
        if (countryCode == sapCodeENG[sapIndex]) {
            return '7f629a941b459d906b7ab165464bcbb5'; // APAC SSC SAP MDM ENG
        }
    }


    return sysID; // Default to EMEA if not found in APAC or US
}
Changes Made:
Added EMEA codes (e.g., INF1, AE24) to the emeaCodes array.
Added a check to assign these company codes to the EMEA SSC SAP MDM group.
Uncommented and modified the code logic to correctly route the tickets for other company codes (like AU, IN, HK, SG, etc.) to the respective groups based on the company code.
Explanation:
EMEA Handling: The script will now assign incidents from company codes like INF1 and AE24 to the EMEA SSC SAP MDM group (sysID = '3e7fde881bd6bc9004bb1fc3b24bcbc0').
APAC and US Handling: The logic for APAC and US regions remains the same, but the EMEA region is now handled separately for those specific company codes.

Blaze Llanos, USMC, MISM
ServiceNow Engineer
Solutions: IT | ServiceNow Expertise Transforming Challenges into Solutions
LinkedIn: https://www.linkedin.com/in/blazellanos/

Kieran Anson
Kilo Patron

I feel like this is unfortunately a great example of over-engineering/too-much complexity. This script has so much logic in it - it's nearly impossible to comprehend. 

 

It's in desperate need of refactoring. Your script is doing much more than choosing the assignment group based on company code, it also has condition logic dependent on request_type. 

 

You may need to list the full requirements of how this work item is too be assigned and re-write the script (ideally using more modern tech like a decision table)

Thank you for your valuable reply. I finally re-wrote the code once again.

Ankur Bawiskar
Tera Patron
Tera Patron

@AbdurRahmanSnow 

script is too long to debug

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