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
Vigneshwaran LoganathanVigneshwaran Loganathan 

Set Not Removing Duplicate Element Problem

My code is,

list<VA_Allowance__c> AdjAllow = new list<VA_Allowance__c>(); 
set<VA_Allowance__c> adjallowset=new set<VA_Allowance__c>();
 
 AdjAllow = [select Amount__c,VA__c,Status__c from VA_Allowance__c where Status__c='Active' and Payroll_Adjustment_only__c=true and (Frequency__c=: pc.Payroll_Cycle_type__c or Frequency__c=:mode or Frequency__c='Both') and Payroll_cycle_year__c =:pc.Payroll_cycle_year__c];
         
         adjallowset.addall(Adjallow); // Adding to Set 
         adjallow.addall(adjallowset); // Still Duplicate Exists  

         System.debug('>>>>>>>adjallow'+adjallow);
         for(VA_Allowance__c adjallowx:adjallow)
         {
           allowvaid.add(adjallowx.VA__c);
         }
    }
    
      for(Payslip__c psx:pays)
        {
         for(VA_Allowance__c allowx :adjallow)
         {
            if(psx.Payslips__c == allowx.VA__c && allowx.status__c =='active')
                   {
                       Paysliplineitem__c allpsl = new Paysliplineitem__c();
                        
                       if(allowx.mode__c == 'One time' )
                        {
                         allowx.Payslip_IDs__c = psx.name;
                        }
                       allowy.add(allowx);
                   }
            }
          }
              insert allplx; 
              update allowy;     
Ramesh KosalairamanRamesh Kosalairaman
Hi
validate before add to list
//Check Duplicate 
 set<Id> FindDuplicate= new set<Id>(); //Check Duplicate
if(!FindDuplicate.contains(bonusx.Id))//Check Duplicate 
bonusy.add(bonusx);//Check Duplicate 
​FindDuplicate.add(bonusx.Id);//Check Duplicate