I have a custom lookup page where users can associate multiple employees to a inputfield. When user close the lookup it pass the selected employee names to the Main Page through CurrentPage.Parameters method. The problem I am facing is that if user select the name unknowingly for the second time it is creating duplicates on the input field. I tried to assign the targetField.Value to another arry and used to array.filter function to remove the duplicates but unsuccessful. enter image description here

enter image description here

Lookup Page code:

<script type="text/javascript">
    function selectAllEmployees(){
        var selectedNames = [];
        var selectedIds   = [];
        function(v) {

    function closeAndPassParams(nameTosend,idToSend){
       var parent = window.opener.document;
       var targetField = parent.getElementById('{!$CurrentPage.parameters.namefield}');
       var targetField1 = parent.getElementById('{!$CurrentPage.parameters.idfield}');
       targetField.value = targetField.value + ',' + nameTosend;
       targetField.value = targetField.value.replace(/^,/, '');   //remove the first comma from the array
       targetField1.value = targetField1.value + ','+ idToSend;
       targetField1.value = targetField1.value.replace(/^,/, '');
<apex:pageBlockButtons location="bottom">
    <div align="Right" draggable="false">
         <apex:commandButton value="Ok" id="selectEmp" onClick="selectAllEmployees();return false;"/>
         <apex:commandButton value="Cancel" onclick="window.top.close()" />

1 Answer 1


You can use the indexOf() method in JS to check whether the value is already present. Based on that add the value only if it was not added earlier.

The indexOf() method returns the position of the first occurrence of a specified value in a string.

if(targetField.value.indexOf(nameTosend) == -1) {
    targetField.value = targetField.value + ',' + nameTosend;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .