Array returning unexpected result with addition ","

Vijay Baokar
Kilo Sage

Hi Folks,

I have defined few arrays which i am later storing into a field (,) separated this is almost achieved but getting extra "," before the value

 

Current result:

MFP - ,MC,GS,L6,G8,4L,C5,9C,IT,E0,
,BWC - ,MA,8A,6A,R4,4M,4X,PQ,LE,
,Consumer Printer (HPS) - ,L9,LG,E4,IR,2Q,

 

expected result:

 

MFP - MC,GS,L6,G8,4L,C5,9C,IT,E0
BWC - MA,8A,6A,R4,4M,4X,PQ,LE
Consumer Printer (HPS) - L9,LG,E4,IR,2Q

 

Note : MFP , BWC , Consumer Printer (HPS) these are Business area which has associated PL keys which are MC,GS,L6,G8,4L,C5,9C,IT,E0, or MA,8A,6A,R4,4M,4X,PQ,LE,

 

i am calling Script Include from on change client script, below is the SI:

 

var supProductMappingAjax1 = Class.create();
supProductMappingAjax1.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    getProductMapping: function() {

        var bsnGroup = this.getParameter('sysparm_businessGrp');
        var marketSub = this.getParameter('sysparm_marketSubSeg');

        gs.info("PS - Business Group is " + bsnGroup + "-" + marketSub);
        var arrSysIDs = [];
        var plKeys = [];
        var bg = [];
        var mss = [];

        var bs = new GlideRecord("cmdb_ci_service_business");
        bs.addQuery('sys_id', 'IN', bsnGroup);
        //  bs.get(bsnGroup);
        bg.push(bs.name);

        var so = new GlideRecord("service_offering");
        so.addQuery('sys_id', 'IN', marketSub);
         mss.push(so.name);
 
        var tempArray = [];
        for (var i = 0; i < bg.length(); i++) {
            for (var j = 0; j < mss.length(); j++) {

                var mapping = new GlideRecord("x_ihpp2_sup_scoped_sup_product_mapping");
                mapping.addQuery("business_group", bg[i]);
                mapping.addQuery("market_sub_segment", mss[j]);
                mapping.query();
                while (mapping.next()) {

                 
                    if (!tempArray.includes(mapping.india_classification.toString())) {
                        tempArray.push(mapping.india_classification.toString());
                        if (!arrSysIDs.includes(mapping.sys_id.toString())) {
                            arrSysIDs.push(mapping.sys_id.toString());
                        }
                    }
                    plKeys.push(mapping.india_classification.toString());
                }
            }
        }
        //   }
        gs.info("PS - mapping records are " + arrSysIDs);
        return arrSysIDs.join(',');
    },

    getPLkeys1: function() {

        var ind = this.getParameter('sysparm_indiaCLS');
        var bsg = this.getParameter('sysparm_bGroup');
        var mss = this.getParameter('sysparm_market');
        gs.info("PS - indiaclass is " + bsg + "--" + mss + "--" + ind);

        var bs = new GlideRecord("cmdb_ci_service_business");
        bs.get(bsg);
        var businessgroup = bs.name;

        var so = new GlideRecord("service_offering");
        so.get(mss);
        var marketsubseg = so.name;

        var arrSysIDs = [];
        var indArray = [];
        var plKeys = [];
        arrSysIDs.push(ind.split(','));
        gs.info("ind classification count " + arrSysIDs.length);

        for (var i = 0; i < arrSysIDs.length; i++) {

            var pm = new GlideRecord("x_ihpp2_sup_scoped_sup_product_mapping");
            pm.addQuery("sys_id", arrSysIDs[i]);
            pm.query();
            while (pm.next()) {
                indArray.push(pm.india_classification.toString());
            }
        }

        gs.info("ind classification array length - " + indArray.length + "-values- " + indArray);
        for (var j = 0; j < indArray.length; j++) {

            plKeys.push(indArray[j] + "  -  ");

            var pl = new GlideRecord("x_ihpp2_sup_scoped_sup_product_mapping");
            pl.addQuery("india_classification", indArray[j]);
            pl.addQuery("business_group", businessgroup);
            pl.addQuery("market_sub_segment", marketsubseg);
            pl.query();
            while (pl.next()) {

                plKeys.push(pl.pl_key.toString());

                gs.info("PS - classification " + indArray[j] + " PL Keys " + plKeys);
            }
            plKeys.push("\n");

        }

        gs.info("PS - pl keys are " + plKeys);
        return plKeys.join(',');

    },

    type: 'supProductMappingAjax1'
});
 

 

 

 

14 REPLIES 14

Shraddha Kadam
Mega Sage

Hello @Vijay Baokar ,

 

Which function are you using in the client script from this Script Include?

If my response was helpful, please mark it as correct and helpful.
Thank you.

@Shraddha Kadam  its "getPLkeys1"

Hello @Vijay Baokar ,

 

Try below script and check if that is working -

    getPLkeys1: function() {
    
        // sysparm_indiaCLS is a comma-separated string of sys_ids from x_ihpp2_sup_scoped_sup_product_mapping
        var indSysIds = this.getParameter('sysparm_indiaCLS');
        // sysparm_bGroup is the SysID of the selected Business Group
        var bsgSysId = this.getParameter('sysparm_bGroup');
        // sysparm_market is the SysID of the selected Market Sub-Segment
        var mssSysId = this.getParameter('sysparm_market');
        gs.info("PS - getPLkeys1 params: bsgSysId=" + bsgSysId + ", mssSysId=" + mssSysId + ", indSysIds=" + indSysIds);

        // --- Retrieve Names for Business Group and Market Sub-Segment ---
        var businessgroupName = '';
        var grBusinessGroup = new GlideRecord("cmdb_ci_service_business");
        if (grBusinessGroup.get(bsgSysId)) {
            businessgroupName = grBusinessGroup.name.toString();
        }

        var marketsubsegName = '';
        var grMarketSubSegment = new GlideRecord("service_offering");
        if (grMarketSubSegment.get(mssSysId)) {
            marketsubsegName = grMarketSubSegment.name.toString();
        }

        // Convert the comma-separated string of input SysIDs into an array
        var inputMappingSysIdsArray = indSysIds.split(',');

        // --- Group PL keys by India Classification Name ---
        // This object will store arrays of PL keys, keyed by their India Classification Name.
        // Example: {'MFP': ['MC', 'GS'], 'BWC': ['MA', '8A']}
        var groupedPlKeys = {};

        // Query the product mapping table to get the India Classification Name and associated PL Keys
        // for the specific mapping records passed in `indSysIds` and matching the other filters.
        var grProductMapping = new GlideRecord("x_ihpp2_sup_scoped_sup_product_mapping");
        grProductMapping.addQuery("sys_id", "IN", inputMappingSysIdsArray.join(',')); // Filter by the sys_ids passed
        grProductMapping.addQuery("business_group", businessgroupName);
        grProductMapping.addQuery("market_sub_segment", marketsubsegName);
        grProductMapping.query();

        while (grProductMapping.next()) {
            var indiaClassName = grProductMapping.india_classification.toString();
            var plKey = grProductMapping.pl_key.toString();

            // Initialize the array for this indiaClassName if it doesn't exist
            if (!groupedPlKeys[indiaClassName]) {
                groupedPlKeys[indiaClassName] = [];
            }
            // Add the PL key to the correct group, ensuring no duplicates within the group
            if (!groupedPlKeys[indiaClassName].includes(plKey)) {
                groupedPlKeys[indiaClassName].push(plKey);
            }
        }

        // --- Format the Result Lines ---
        var resultLines = [];
        for (var indiaClassName in groupedPlKeys) {
            if (groupedPlKeys.hasOwnProperty(indiaClassName)) {
                var plKeysForCurrentClass = groupedPlKeys[indiaClassName];

                // Only add a line if there are PL keys associated with this classification
                if (plKeysForCurrentClass.length > 0) {
                    // Construct the line in the desired format: "Business Area - PL1,PL2,PL3"
                    resultLines.push(indiaClassName + " - " + plKeysForCurrentClass.join(','));
                }
            }
        }

        gs.info("PS - final result lines are: " + resultLines.join('\n'));
        return resultLines.join('\n');
    },
If my response was helpful, please mark it as correct and helpful.
Thank you.

@Shraddha Kadam i am getting result as 

Notebook - G7
Desktops - GA

But i want PL keys comma separated

 Notebook - G7,8N,TA,MP

 Desktops - GA,UT,I1,DG,US,BO

 

There are multiple Notebook or Desktop business areas with same name which has diff PL keys but i need to club all PL keys under single category (Notebook or Desktop )