Array returning unexpected result with addition ","
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2025 10:51 PM - edited 07-14-2025 10:53 PM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2025 10:57 PM
Hello @Vijay Baokar ,
Which function are you using in the client script from this Script Include?
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2025 11:06 PM
@Shraddha Kadam its "getPLkeys1"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2025 11:10 PM
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');
},
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2025 11:31 PM
@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 )