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
dineshkumardineshkumar 

Avoid duplicate on bulkify trigger -pls check my code-guide me..

Good Day,
when I upload bulk data on monthlycountva object.some field fetch from account va object and update on monthly count va object.
Condition
monthlycountva account+va=accountva account+va
Here some of duplicate record found in account va object.
monthlycountva object:I want to insert and update from account va object (like placement type,type).if duplicate record found on account va object it will treat as one or its take value from any one of record.
how to avoid that dupliacate record and update all monthlycountva object data.please guide me.
My code:
rigger UpdatefromAccVA on MonthlyVACount__c (after insert,after update)
{
   list<MonthlyVACount__c> mva = trigger.new;
   list<Account_VA__c> acva = new list<Account_VA__c>();
   list<MonthlyVACount__c> mvupdate = new  list<MonthlyVACount__c>();
   list<MonthlyVACount__c> mva1 = new  list<MonthlyVACount__c>();
   list<string>  accid = new list<string>();
   list<string>  vaid = new list<string>();
   list<string> mvaid = new list<string>();
   for(MonthlyVACount__c mvax : mva)
   {
    accid.add(mvax.Account__c);
    vaid.add(mvax.VA__c);
    mvaid.add(mvax.id);
   }
     acva = [select id,account__c,VA__C, Type__c,Placement_Type__c,No_of_Hours__c,effective_Date__c,Case_Outcome__c from Account_VA__c where (account__c in : accid and VA__c in : vaid)];
     mva1 = [select id,Account__c,VA__c,VA_Type__c,Placement_Type__c,VACount__c,Effective_Date__c,Latest_Case_Outcome__c from MonthlyVACount__c where id in : mvaid];
     if(acva != null)
    
     {
    
      for(MonthlyVACount__c mvaxx : mva1)
      {
       for(Account_VA__c acvax : acva)
       {
        if(mvaxx.Account__c == acvax.account__c && mvaxx.VA__c == acvax.VA__C && mvaxx.Account__c!=null &&mvaxx.VA__c!=null)
        {
       
         mvaxx.VA_Type__c=acvax.Type__c;
         mvaxx.Placement_Type__c=acvax.Placement_Type__c;
       //  mvaxx.VACount__c= vacount;
        // mvaxx.Effective_Date__c=acvax.Effective_Date__c;
         mvaxx.Latest_Case_Outcome__c=acvax.Case_Outcome__c;
         mvupdate.add(mvaxx);
         }  
       }
      }
  
      update mvupdate;

    }
}
ShashForceShashForce
Hi Dinesh,

Please try something like this:

if(mvaxx.Account__c == acvax.account__c && mvaxx.VA__c == acvax.VA__C && mvaxx.Account__c!=null &&mvaxx.VA__c!=null)
        {
       
         if(mvaxx.VA_Type__c!=acvax.Type__c){mvaxx.VA_Type__c=acvax.Type__c};
         if(mvaxx.Placement_Type__c!=acvax.Placement_Type__c){mvaxx.Placement_Type__c=acvax.Placement_Type__c};
       //  mvaxx.VACount__c= vacount;
        // mvaxx.Effective_Date__c=acvax.Effective_Date__c;
         if(mvaxx.Latest_Case_Outcome__c=acvax.Case_Outcome__c){mvaxx.Latest_Case_Outcome__c=acvax.Case_Outcome__c};
         if((mvaxx.VA_Type__c==acvax.Type__c)||(mvaxx.Placement_Type__c!=acvax.Placement_Type__c)||(mvaxx.Latest_Case_Outcome__c=acvax.Case_Outcome__c)){mvupdate.add(mvaxx)};
         }

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank