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

This script is not clearing the duplicate entry in case (ie., Case 1,Case 2, etc)

So, In this case, when 1 was entered again, it should be cleared off saying duplicate entry

Hi @Anna_Servicenow ,
Try this one:-
Please check all the field name and add error msg if any 

  function chkArr(data, field) {
        var val = data.toString()
        if ((val != "") && (myArray.indexOf(val) == -1)) {
            myArray.push(val);
        } else {
            switch (field) {
                case "u_caseid_1":
                    current.u_caseid_1= "";
                    break;
                case "u_caseid_2":
                    current.u_caseid_2= "";
                    break;
                case "u_caseid_3":
                    current.u_caseid_3= "";
                    break;
                case "u_caseid_4":
                    current.u_caseid_4= "";
                    break;
               case "u_caseid_5":
                    current.u_caseid_5= "";
                    break;
              case "u_caseid_6":
                    current.u_caseid_6= "";
                    break;
             case "u_caseid_7":
                    current.u_caseid_7= "";
                    break;
            case "u_caseid_8":
                    current.u_caseid_8= "";
                    break;
            case "u_caseid_9":
                    current.u_caseid_9= "";
                    break;
            case "u_caseid_10":
                    current.u_caseid_10= "";
                    break;
            }
        }
    }

    chkArr(current.u_caseid_1,"u_caseid_1");
    chkArr(current.u_caseid_2,"u_caseid_2");
    chkArr(current.u_caseid_3,"u_caseid_3");
    chkArr(current.u_caseid_4,"u_caseid_4");
    chkArr(current.u_caseid_5,"u_caseid_5");
    chkArr(current.u_caseid_6,"u_caseid_6");
    chkArr( current.u_caseid_7,"u_caseid_7");
    chkArr(current.u_caseid_8,"u_caseid_8");
    chkArrcurrent.u_caseid_9,"u_caseid_9");
    chkArr(current.u_caseid_10,"u_caseid_10");

    current.connect_case_ids_2 = myArray.toString();

 

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