1

I am using below code to remove duplicate values from a lookup column called Project Name and for some reason its not working, can someone please help me correct it.

FYI, i have already enforced unique values for the lookup column but it does not seem to do the job.

Below is the code:

    <script language="javascript" type="text/javascript" 
src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
var previousOption2;

$(document).ready(function() {
$('#Company_x0020_Name').parent().next().find('select option').each(function() {
    if (this.text == previousOption2) $(this).remove();
    previousOption2= this.text;
});
});
</script>

This is the ID i get to see: Company_x0020_Name Below is the screenshot of the form: enter image description here

1

A couple of things to check:

  • Your code only removes duplicates when they occur one after another, not separately.
  • Check you jquery selector. Check the html of the page and see if you are selecting the select element correctly. You need the internal name of the field. Assuming it is the same as the display name you will need the following change:

>

<script language="javascript" type="text/javascript" 
src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
var previousOption;

$(document).ready(function() {
$('#Project_x0020_Name').parent().next().find('select option').each(function() {
    if (this.text == previousOption) $(this).remove();
    previousOption= this.text;
});
});
</script>
  • Thanks Paiman, that worked for Project Name. Question, I have one more lookup field on the same form with a name "Company Name". I added one more web part for Company Name as well but its not hiding duplicates from Company Name drop-down, any reason? I added the code in a separate web part: – mdevm Jul 13 '18 at 3:16
  • I updated my question description and added a screenshot of the form. The first script editor web part contains code for cascading and 2nd and 3rd contain code for removing duplicates for Project Name and Company Name respectively. Project Name worked but Company name still shows duplicates, both are lookup columns. Project Name is the Parent and Company name is child. – mdevm Jul 13 '18 at 3:23
  • not sure why, but you might want to debug into the jquery on the browser to see why it is not working. Try $('#Company_x0020_Name').parent().next().find('select option') in the browser console see if it returns anything. Also check if Company_x0020_Name is the internal name of the field – Paiman Samadian Jul 13 '18 at 4:23
  • Paiman, I updated code I am using for Company Name and the ID i m getting from the browser. Please check, I do not understand why is the same code not working for one and works for other field. – mdevm Jul 13 '18 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.