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

@Vijay Baokar 

yes

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

it didn't work @Ankur Bawiskar 

i think below line is causing the issue

return plKeys.join(','); 

I have 16 records for "Notebook" which has diff PL Key values and same with "Desktop" hence i need below result

Notebook - G7,8N,TA,MP,AN
Desktops - GA,UT,I1,DG,US,BO,IL 

 
 

@Vijay Baokar ,

What result are you getting now with the provided script ?

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

@Shraddha Kadam Set function won't work in Servicenow, I tried that already and Array.from also gave the same result

Hi @Ankur Bawiskar I have updated the same function but it has solved that additional"," issue but still not getting expected result.

currently i am trying with  "pl.addQuery("india_classification", 'Notebook');" on var pl = new GlideRecord("x_ihpp2_sup_scoped_sup_product_mapping"); and getting below result, it should not add the same KEY to Desktops because i have not user Desktops in condition.

 

Notebook - G7,8N,TA,MP,AN,M1,8J,GB,6U,IK,UV
Desktops - G7,8N,TA,MP,AN,M1,8J,GB,6U,IK,UV

 

however i need to use dynamic condition like "

//pl.addQuery("india_classification", classification);" but its not giving any result
expected is:
Notebook - G7,8N,TA,MP,AN,M1,8J,GB,6U,IK,UV
Desktops - GA,UT,I1,DG,US,BO,IL,2C,TB,I0,R7,7F,M0,IQ,5U,5X

 

 

 

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");
        if (!bs.get(bsg)) return "";
        var businessgroup = bs.name;

        gs.info("Business GRP :" + businessgroup);

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

        gs.info("Market SUB :" + marketsubseg);

        var indArray = ind.split(',').filter(function(item) {
            return item && item.trim();
        });

        var indClass = [];
            var pl2 = new GlideRecord("x_ihpp2_sup_scoped_sup_product_mapping");
            pl2.addQuery('sys_id', 'IN' ,ind );
            pl2.query();

            while(pl2.next())
            {
            indClass.push(pl2.india_classification.toString());
            }
            gs.info('VBA :'+ indClass);


        var resultLines = [];

        for (var j = 0; j < indArray.length; j++) {
            var classification = indArray[j];
            var plKeys = [];
            gs.info("vb_CLASSification :" + classification.toString());

            var pl = new GlideRecord("x_ihpp2_sup_scoped_sup_product_mapping");
            pl.addQuery("india_classification", 'Notebook');
            //pl.addQuery("india_classification", classification);
            pl.addQuery("business_group", businessgroup);
            pl.addQuery("market_sub_segment", marketsubseg);
            pl.query();

            gs.info("VB_Product Line Key :" + pl.getRowCount());

            while (pl.next()) {
                var key = pl.pl_key.toString().trim();
                if (key) plKeys.push(key);
                gs.info("Product Line Key :" + plKeys);
            }

            // Remove duplicates
            plKeys = Array.from(new Set(plKeys));

            if (plKeys.length > 0) {
                var line = indClass[j] + " - " + plKeys.join(',');
                gs.info("VB_LINE :" + line);    

                resultLines.push(line);

                gs.info("VB_Value :" + resultLines);
            }
        }

        return resultLines.join('\n');
    },