Workflow script not working - Service Catalog Form

AbdurRahmanSnow
Giga Guru

Good afternoon. I have a requirement on a Catalog Form.

When Request type is Modify and Account group is ZTRD, then it should check the below company code:

 

AU, IN, HK, SG company code should assign to APAC SSC SAP MDM ENG Assignment group 
CN, KR, TW, MY, JP company code should assign to APAC SSC SAP MDM Assignmentgroup

 

but when Request Type is Modify and account group as ZTRD, for any company code, it by default setting APAC SSC SAP MDM ENG. 

Please check this code and rectify this issue.

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';
            } else {
                workflow.info('Line 137' + 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.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]) {
                sysID = '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]) {
                sysID = '7f629a941b459d906b7ab165464bcbb5'; // APAC SSC SAP MDM ENG
            }
        }
    }

}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@AbdurRahmanSnow 

try this

setGroup();

function setGroup() {
    var sysID = '';

    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
    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
    if (current.variables.request_type == 'Modify' && current.variables.account_group == 'ZTRD') {
        var companyCode = current.variables.company_code.u_company_code;

        if (sapCodeENG.includes(companyCode)) {
            sysID = '7f629a941b459d906b7ab165464bcbb5'; // APAC SSC SAP MDM ENG
        } else if (sapCode.includes(companyCode)) {
            sysID = '667fde881bd6bc9004bb1fc3b24bcb7a'; // APAC SSC SAP MDM
        }
    }

    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"
}

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

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

Good job adding the logs, add some more, and share which ones you are getting or not getting that you are expecting, rather than us trying to digest and discern this entire script.  Each value used in if conditions (like current.variables.company_code.u_ssc) should be logged, and a log inside each if condition if you are unsure if the script in the if blocks is being executed.

Ankur Bawiskar
Tera Patron
Tera Patron

@AbdurRahmanSnow 

try this

setGroup();

function setGroup() {
    var sysID = '';

    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
    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
    if (current.variables.request_type == 'Modify' && current.variables.account_group == 'ZTRD') {
        var companyCode = current.variables.company_code.u_company_code;

        if (sapCodeENG.includes(companyCode)) {
            sysID = '7f629a941b459d906b7ab165464bcbb5'; // APAC SSC SAP MDM ENG
        } else if (sapCode.includes(companyCode)) {
            sysID = '667fde881bd6bc9004bb1fc3b24bcb7a'; // APAC SSC SAP MDM
        }
    }

    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"
}

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

@AbdurRahmanSnow 

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

@AbdurRahmanSnow 

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