The CreatorCon Call for Content is officially open! Get started here.

How to find duplicate array elements

Anna_Servicenow
Tera Guru

I have a requirement to get the case ids enter in Case ID 1, CaseID 2.. Case ID 10 to connect case id column .

 

Anna_Servicenow_0-1719586896338.png

 

 

I could achieve this with below script. But now I need to include these conditions as well.

> In case duplicate value is added in case id 1(upto 10) , it should be cleared

> Also the duplicate shouldn't be present in connect case id

 

......

 

BR > before -update

 var myArray = [];

    if (current.u_caseid_1 != '') {
        myArray[0] = current.u_caseid_1;
    }
    if (current.u_caseid_2 != '') {
        myArray[1] = current.u_caseid_2;
    }
    if (current.u_caseid_3 != '') {
        myArray[2] = current.u_caseid_3;
    }
    if (current.u_caseid_4 != '') {
        myArray[3] = current.u_caseid_4;
    }
    if (current.u_caseid_5 != '') {
        myArray[4] = current.u_caseid_5;
    }
    if (current.u_caseid_6 != '') {
        myArray[5] = current.u_caseid_6;
    }
    if (current.u_caseid_7 != '') {
        myArray[6] = current.u_caseid_7;
    }
    if (current.u_caseid_8 != '') {
        myArray[7] = current.u_caseid_8;
    }
    if (current.u_caseid_9 != '') {
        myArray[8] = current.u_caseid_9;
    }
    if (current.u_caseid_10 != '') {
        myArray[9] = current.u_caseid_10;
    }
     current.connect_case_ids_2 = myArray.toString();
1 ACCEPTED SOLUTION

Hi @Anna_Servicenow 

Use OnSubmit client script instead of BR and refer following sample script

function onSubmit() {
//Type appropriate comment here, and begin script below
var allCasesArr = [];
for (i = 1; i <= 10; i++) {
var caseField = "u_caseid_" + i;
var caseVal = g_form.getValue(caseField).toString();

if (caseVal != "" && allCasesArr.indexOf(caseVal) != -1) {
g_form.addErrorMessage("Duplicate Case Entry");
g_form.clearValue(caseField);

} else if (caseVal != "")
allCasesArr.push(caseVal);

}

g_form.setValue('connect_case_ids_2', allCasesArr.toString());
}

Thanks,

Swathi Peddireddy

View solution in original post

7 REPLIES 7

Bert_c1
Kilo Patron

Hi,

 

There is the "ArrayUtil" script include that has several methods that can help you. Review that in you instance.

Tried arrayutils and its not working

Sandeep Rajput
Tera Patron
Tera Patron

@Anna_Servicenow Please update the script as follows and see if it works for you.

 var myArray = [];

    if (current.u_caseid_1 != '' && myArray.indexOf(current.u_caseid_1)==-1) {
        myArray[0] = current.u_caseid_1;
    }
    if (current.u_caseid_2 != '' && myArray.indexOf(current.u_caseid_2)==-1) {
        myArray[1] = current.u_caseid_2;
    }
    if (current.u_caseid_3 != '' && myArray.indexOf(current.u_caseid_3)==-1) {
        myArray[2] = current.u_caseid_3;
    }
    if (current.u_caseid_4 != '' && myArray.indexOf(current.u_caseid_4)==-1) {
        myArray[3] = current.u_caseid_4;
    }
    if (current.u_caseid_5 != '' && myArray.indexOf(current.u_caseid_5)==-1) {
        myArray[4] = current.u_caseid_5;
    }
    if (current.u_caseid_6 != '' && myArray.indexOf(current.u_caseid_6)==-1) {
        myArray[5] = current.u_caseid_6;
    }
    if (current.u_caseid_7 != '' && myArray.indexOf(current.u_caseid_7)==-1) {
        myArray[6] = current.u_caseid_7;
    }
    if (current.u_caseid_8 != '' && myArray.indexOf(current.u_caseid_8)==-1) {
        myArray[7] = current.u_caseid_8;
    }
    if (current.u_caseid_9 != '' && myArray.indexOf(current.u_caseid_9)==-1) {
        myArray[8] = current.u_caseid_9;
    }
    if (current.u_caseid_10 != '' && myArray.indexOf(current.u_caseid_10)==-1) {
        myArray[9] = current.u_caseid_10;
    }
     current.connect_case_ids_2 = myArray.toString();

Hope this helps.

 

Deepankumar
Tera Expert

Hi @Anna_Servicenow ,
Try this code working on my PDI

Deepankumar_0-1719596928778.png

CODE:-

var myArray = [];

    function chkArr(val) {
		if((val != "") && (myArray.indexOf(val) == -1)){
         myArray.push(val);
		} 
 }
    chkArr(current.u_caseid_1 .toString());
    chkArr(current.u_caseid_2.toString());
    chkArr(current.u_caseid_3.toString());
    chkArr(current.u_caseid_4.toString());
    chkArr(current.u_caseid_5.toString());
    chkArr(current.u_caseid_6.toString());
    chkArr( current.u_caseid_7.toString());
    chkArr(current.u_caseid_8.toString());
    chkArrcurrent.u_caseid_9.toString());
    chkArr(current.u_caseid_10.toString());

    current.connect_case_ids_2 = myArray.toString();