function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RAAMRAAM 

How to prevent adding duplicate values.

 i select a few contacts and mass add them to my junction object,how to prevent from adding dulicates values ie contacts into my junction object??

this is my final working code

public class customController2
{

    public class mcontact
    {
        contact c;
        boolean selected=false;
       
        public mcontact( contact con)
        {
            c=con;
        }
      
        public Contact getContact()
        {
            return c;
        }
       
        public void setContact(Contact con)
        {
            c=con;
        }
           
        public boolean getSelected()
        {
          return selected;
        }
     
      public void setSelected(boolean sb)
      {
          this.selected = sb;
      }
    }
   
    Contact contact;
    MailList__c maillist;
    MailList_History__c mailhistory;
   // Integer i;
    String[] conts = new String[]{};
   
   public Contact getContact()
   {
    if(contact == null)
    contact = new Contact();
    return contact;
   }
  
    public MailList__c getMaillist()
   {
    if(maillist == null)
    maillist = new MailList__c();
    return maillist;
   }
  
   public MailList_History__c getMailhistory()
   {
       if(mailhistory == null)
       mailhistory = new MailList_History__c();
       return mailhistory;
   }
  
   //mContact [] Contacts;
   List<mContact> contacts = new  List<mContact>();
   //MailList_History__c [] mh = [select id,contact__c,MailList_Name__c from MailList_History__c];
   
    public List<mContact> getContacts ()
    {
         
            contacts = new List<mContact>();
            Contact[] original_contacts;
         
            original_contacts = [select Id, Name, Phone, MailingCity, Email from Contact];
     
            for(Contact a : original_contacts)
            {
                mContact cs = new mContact(a);
                contacts.add(cs);                 
            }
           
        return contacts ;
    }
        
    public PageReference step1()
   {
       return Page.Wizard1;
   }
  
   public PageReference step2()
   {
       return Page.Wizard2;
   }
  
  public PageReference save()
  {
          for(mContact c: contacts )
      {
             if(c.getSelected())
             {Contact con = c.getContact();
            
                 ID conid=con.ID;
                 MailList_History__c m = new 
MailList_History__c(Contact__c=conid,MailList_Name__c=mailhistory.MailList_Name__c,Status__c=mailhistory.Status__c);
                 insert m;
                 //i++;           
             }
      }
    
      return Page.Wizard2;
  }
}

Anand@SAASAnand@SAAS
You could have field that concatenates ContactID & MailListId through a workflow or trigger and use that as external id. This way instead of doing "insert" you do "upsert" that will never result in duplicates.