+ Start a Discussion
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Trigger fires wrong update when mass update is done

Hi, 

 I wrote a below trigger which is working perfect no issue I have issue while doing mass update using data loader when batch size is set to more than 1 trigger is updating wrongly to all the records when batch is set to 1 it is working as expected. 

  Please suggest me what is the issue in the code or do i have to add any aditional logic to handle this issue. 
trigger logosince on Account bulk (Before update) 
{
if(checkRecursive.runOnce())
{
  
   Set<Id> accountId = new Set<Id>();
   List<Id> chkoneprt = new List<Id>();
   List<Id> chkoneid = new List<Id>();
   for (Account a : Trigger.New) 
   {     
      accountId.add(a.id);
      chkoneid.add(a.id);
      chkoneprt.add(a.parentid);
      
      } 
       
  list<Account> act = [Select id, parentid from account 
                     where id in :accountId];

  list<id> actid = new list<id>();
  list<id> actparentid = new list<id>();

  actid.clear();
  actparentid.clear();         
              
 for( account gact : act)
{
     actid.add(gact.id);
     actparentid.add(gact.parentid);
        
}    

  system.debug('id  ' + actid);
  system.debug('parentid  ' + actparentid);
  system.debug(actparentid.size());
  system.debug(actparentid.isempty());
  system.debug(!actparentid.isempty());

  list<id> firstid = new list<id>(); 
  list<id> firstparentid = new list<id>();

  if ( !actparentid.isempty() )
  {
  list<Account> firstact = [Select id, parentid from account 
                       where parentid  in :actparentid and parentid != '']; 
      
   for( account gfirstact : firstact)
  {
     firstid.add(gfirstact.id);
     firstid.add(gfirstact.parentid);   
    }
   
   system.debug('First id  ' + firstid);    
  }

  list<id> secondid = new list<id>(); 
  list<id> secondparentid = new list<id>(); 

if ( !actparentid.isempty())
  {
  list<Account> Secondact = [Select id, parentid from account 
                       where parentid in :actid]; 
      
  for( account gfirstact : Secondact)
  {
     secondid.add(gfirstact.id);
     secondid.add(gfirstact.parentid);   
    }
   
   system.debug('Second id  ' + secondid);
  
  }


  Try
   {
List<AggregateResult> gr = [ 
     SELECT min(closedate) from opportunity 
     WHERE (accountid in :firstid or 
            accountid in :secondid or 
            accountid in :actid)
            AND (StageName = '1 - Closed Won' OR StageName = 'Booked') 
             ];
             
     for (AggregateResult ar : gr)  {
        system.debug('Min Opp date' + (Datetime)ar.get('expr0'));
        for(Account acts : trigger.new) 
              {
              acts.Logo_Since__c = (Datetime)ar.get('expr0') + 1; 
              }
         } 
  }

  catch (System.NullPointerException e) {
    system.debug('Null Exception');
            for(Account acts : trigger.new) 
              {
                acts.Logo_Since__c = null;
                acts.SubscriptionLogoSince__c = null;
              }
  }   

  Try
   {  
List<AggregateResult> sgr = [ 
     SELECT min(closedate) from opportunity 
     WHERE (accountid in :firstid or 
            accountid in :secondid or 
            accountid in :actid)
            AND (StageName = '1 - Closed Won' OR StageName = 'Booked') 
            AND ACV_Subscription_Amount__c > 0 ];
             
     for (AggregateResult ar : sgr)  {
        system.debug('Min Opp date' + (Datetime)ar.get('expr0'));
        for(Account acts : trigger.new) 
              {
              acts.SubscriptionLogoSince__c = (Datetime)ar.get('expr0') + 1; 
              }
         }    
 }        
          
     
   catch (System.NullPointerException e) {
    system.debug('Null Exception');
            for(Account acts : trigger.new) 
              {
                acts.SubscriptionLogoSince__c = null;
              }
  } 
 
  


  
 }      
}