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
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Dataloader trigger works batch size 1 explodes otherwise with same value

Hi Experts, 

 Wrote two types of trigger both are working fine as expected problem is when dataloader with batch size > 1 mass update is done it is exploding with same values not sure what is the issue with the code please suggest me the soloution to fix this issue. 

Trigger 1 
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);
        
}    
 //act.clear();
 
  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);   
    }
  // firstact.clear();
   
   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);   
    }
   
   //Secondact.clear();
   
   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; 
              }
         } 
        // gr.clear();
      
  }

  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; 
              }
         }   
         //sgr.clear();
          
 }        
          
     
   catch (System.NullPointerException e) {
    system.debug('Null Exception');
            for(Account acts : trigger.new) 
              {
                acts.SubscriptionLogoSince__c = null;
              }
  } 
 }      
}

Trigger -2 
trigger newlogosince on Account bulk (before update) 
{

   
   Set<Id> accountId = new Set<Id>();
   List<Id> chkoneprt = new List<Id>();
   List<Id> chkoneid = new List<Id>();
   Map<String, Account> mact = new Map<String, Account>();

   String mactid;
   String mactparentid;
   list<id> firstid = new list<id>(); 
   list<id> firstparentid = new list<id>();
   list<id> secondid = new list<id>(); 
   list<id> secondparentid = new list<id>(); 

   for (Account a : Trigger.New) 
    {     
      accountId.add(a.id);
      chkoneid.add(a.id);
      chkoneprt.add(a.parentid);
      mact.put(a.id, a);
      system.debug('Print Map Values' + mact.put(a.id, a)); 
      } 

   for (account aa : mact.values() )
   {
     System.debug('Map Account ID ' + aa.id);  
     System.debug('Map Parent ID ' + aa.parentid); 
     mactid = aa.id; 
     mactparentid = aa.parentid; 
   }

   system.debug('mactid' + mactid);
   system.debug('mactparentid' + mactparentid);

   if ( mactparentid != null )
   {
   Map<String, Account> firstact = new Map<String, Account>(
                       [Select id, parentid from account 
                       where parentid = :mactparentid and parentid != '']); 

   for (account afirst: firstact.values())
   {
     System.debug('Map First Account ID ' + afirst.id);  
     //System.debug('Map First Parent ID ' + afirst.parentid); 
       firstid.add(afirst.id);
    }
       
   Map<String, Account> secondact = new Map<String, Account>(
                       [Select id, parentid from account 
                       where parentid = :mactid]); 
       
   for (account asecond: secondact.values())
   {
     System.debug('Map Second Account ID ' + asecond.id);  
     //System.debug('Map Second Parent ID ' + asecond.parentid);    
     secondid.add(asecond.id);  
    }
       
   }
   
Try
   {
List<AggregateResult> gr = [ 
     SELECT min(closedate) from opportunity 
     WHERE (accountid in :firstid or 
            accountid in :secondid or 
            accountid = :mactid)
            //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; 
              system.debug('logo since' + (Datetime)ar.get('expr0') + 1); 
              //update acts;    
              }
         } 
  }

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


Try
{  
List<AggregateResult> sgr = [ 
     SELECT min(closedate) from opportunity 
     WHERE (accountid in :firstid or 
            accountid in :secondid or 
            accountid = :mactid)
            //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;
              }
  } 
 
}

Please suggest me how to fix this issue. 

Thanks
Sudhir
deepak balur 19deepak balur 19
Csn you post the exact error please?
sudhirn@merunetworks.comsudhirn@merunetworks.com

Hi Deepak,

   There is no error issue is its not updating when bulk updates are made throught data loader when batch size is more than 1

   Hope you understand the requirement. 
 

Thanks
Sudhir