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.
Lookup Page code:
<script type="text/javascript">
function selectAllEmployees(){
var selectedNames = [];
var selectedIds = [];
[].forEach.call(document.querySelectorAll('input:checked'),
function(v) {
selectedNames.push(v.dataset.fullName);
selectedIds.push(v.dataset.emailValue);
});
closeAndPassParams(selectedNames,selectedIds);
}
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(/^,/, '');
self.close();
}
</script>
<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()" />
</div>
</apex:pageBlockButtons>