Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Remove duplicates from the string variable type.

soundarya_123
Tera Contributor

There is a use case table with a field called 'mandate variables,' where backend variable names are added. This use case table is associated with the current catalog item. I have written a script to retrieve the variable names from the 'mandate variables' field in the use case, but it is not removing the duplicate entries from the array.

 

var va = '6e263fa6875e121046e3ca6e8bbb358a';

var arr = [];

var gr = new GlideRecord('u_sc_catalog_item_use_case');

gr.addQuery('u_catalog_item', va); //itemObj

gr.query();

 

 

var allMandateVariables = [];

var uniqueMandateVariables = [];

 

while (gr.next()) {

    var mandateVariables = gr.getValue('u_mandate_variables');

    if (mandateVariables) { // Check if mandateVariables is not empty

        allMandateVariables.push(mandateVariables);

   

    }

}

 

uniqueMandateVariables = new ArrayUtil().unique(allMandateVariables);

gs.print(uniqueMandateVariables);

9 REPLIES 9

soundarya_123
Tera Contributor

both the above answers weren't working. As per @Robert H  suggestion it's not returning any values.

@soundarya_123 

share your latest script

what does the field "u_mandate_variables" contains? single value or multiple comma separated values

share logs as per below

var va = '6e263fa6875e121046e3ca6e8bbb358a';
var allMandateVariables = [];
var gr = new GlideRecord('u_sc_catalog_item_use_case');
gr.addQuery('u_catalog_item', va); //itemObj
gr.query();
while (gr.next()) {
    var mandateVariables = gr.getValue('u_mandate_variables');
    gs.info('mandateVariables is' + mandateVariables);
    if (mandateVariables) { // Check if mandateVariables is not empty
        allMandateVariables.push(mandateVariables);
    }
}
gs.info('Before unique' + allMandateVariables);
var uniqueMandateVariables = new ArrayUtil().unique(allMandateVariables);
gs.print('After unique' + uniqueMandateVariables);

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

PFB, 

soundarya_123_1-1744289600239.png

 

@soundarya_123 

see as I rightl pointed out there are more than 1 values in that u_mandate_variables field and hence while pushing into array it's not working and hence unique logic is not working

the 2nd & 3rd iteration shows 2 values without comma separated

Is that field supposed to have 1 value or more than 1 value?

if more than 1 then add comma as separator

try this code and it uses split on space

var va = '6e263fa6875e121046e3ca6e8bbb358a';

var gr = new GlideRecord('u_sc_catalog_item_use_case');
gr.addQuery('u_catalog_item', va); //itemObj
gr.query();

var allMandateVariables = [];

while (gr.next()) {
    var mandateVariables = gr.getValue('u_mandate_variables');
    if (mandateVariables) { // Check if mandateVariables is not empty
        var variablesArray = mandateVariables.split(' '); // Split the string into an array
        allMandateVariables = allMandateVariables.concat(variablesArray); // Add to the main array
    }
}

var uniqueMandateVariables = new ArrayUtil().unique(allMandateVariables);
gs.print(uniqueMandateVariables);

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

Hello @soundarya_123 ,

 

Sorry, my personal instance was down so I was doing this from memory.

Please use this line instead:

allMandateVariables = allMandateVariables.concat(mandateVariables.split(',')); 

I have updated my original reply as well.

 

Regards,

Robert