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 

Trigger fails during batch or mass update

Hi,

  We have few trigger which is working as expected. during mass update it is failing if the batch size is 1 it is working as expected if it is more than 1 it is failing and not working as expected. 

 Please suggest me how to fix this issue. 

Thanks
Sudhir
AmulAmul
Disable workflow rules..and try again
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi Amul, 

  This issue is not work workflow it is related with data laoder when user trys to update more than batch size of 1 its not working as expected.

Thanks
Sudhir
JethaJetha
Please post your code here, we will guide you how to bulify your trigger to handle multiple records or mass update.
sudhirn@merunetworks.comsudhirn@merunetworks.com
HI Jetha, 

  Please find the code below
trigger logosince on Account (Before update) 
{
if(checkRecursive.runOnce())
{
  Try
 {
   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);
  
  }

List<AggregateResult> gr = [ 
     SELECT min(closedate) from opportunity 
     WHERE StageName in ('1 - Closed Won','Booked') and
            (accountid in :firstid or 
            accountid in :secondid or 
            accountid in :actid)];
             
     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;
              }
  } 
}     
       
}

Thanks
Sudhir
JethaJetha
This trigger looks good to me, can you please tell us the error which you are getting.
sudhirn@merunetworks.comsudhirn@merunetworks.com
Jetha, Problem is this when user do a mass update with batch size more than 1 in data loader it is failing and trigger is not updating as expected. if we set to batch size = 1 it is working as expected 

 Do you know how to make this fix for this issue. will recursive or any other will work to fix this issue.

Thanks
Sudhir
JethaJetha
No No this trigger is not recurssive in any way, you can remove [checkRecursive.runOnce()] line from your code, your code will work.

Can you please tell, for how many records it's throwing error.
sudhirn@merunetworks.comsudhirn@merunetworks.com
out of 12 records 4 to 5 are are not updated properly if i manually update one at a time it is udpating correctly. 
JethaJetha
Let me rewrite your trigger and try that one..........
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks Jetha this issue is happening only in data loader when doing a mass update. 
JethaJetha
Try Below version
 
trigger logosince on Account (Before update) 
{
	Try
	{
		list<id> actid = new list<id>();
		list<id> actparentid = new list<id>();

		for( account gact : Trigger.New)
		{
			actid.add(gact.id);
			actparentid.add(gact.parentid);
		}    

		list<id> parentId = new list<id>(); 
		list<id> grandParent = new list<id>();

		if ( !actparentid.isempty() )
		{
			list<Account> parentAccount = [Select id, parentid from account 
										   where parentid  in : actparentid and parentid != '']; 
	  
			for( account gfirstact : parentAccount)
			{
				parentId.add(gfirstact.id);
				grandParent.add(gfirstact.parentid);   
			}
		}

		list<id> childAccount = new list<id>(); 

		if ( !actparentid.isempty())
		{
			list<Account> Secondact = [Select id, parentid from account 
									   where parentid in :actid]; 
	  
			for( account gfirstact : Secondact)
			{
				childAccount.add(gfirstact.id);
			}
		}
		
		List<string> stages = new List<string>{'1 - Closed Won','Booked'};
		List<AggregateResult> gr = [SELECT min(closedate) from opportunity 
									WHERE StageName in : stages and
									(accountid in :childAccount or 
									 accountid in :parentId or 
									 accountid in :grandParent)];
			 
		for (AggregateResult ar : gr)  
		{
			for(Account acts : trigger.new) 
			{
				acts.Logo_Since__c = (Datetime)ar.get('expr0') + 1; 
			}
		}     
	 
	}
 
	catch (System.NullPointerException e) 
	{
		for(Account acts : trigger.new) 
		{
			acts.Logo_Since__c = null;
		}
	}   
}

 
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks Jetha I am testing just want to know what is the chagnes you have made to the trigger to fix this issue. 
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi Jetha,
 
    Trigger is not working its not working as expected it is failing to update the values 

Thanks
Sudhir
JethaJetha
Sorry friend, I tried my level best but unfortunately we both couldn't make it. On 28-Apr-2016 4:30 pm, Salesforce Developer Community
sudhirn@merunetworks.comsudhirn@merunetworks.com
Np Jetha you didnt tell me what changes you have made to the code to fix I can find a fix if there is something wrong
JethaJetha
I just tried to remove redundent variable, nothing else from functional point of view.

Keep on trying, my best wishes is with you buddy....
sudhirn@merunetworks.comsudhirn@merunetworks.com
I planning to change the code to @future notation adding them inside a class
GauravGargGauravGarg

Hi Sudhir,

Can you please share the scenario implemented using above trigger logic, so that I could help you here. 

Thanks,

Gaurav

sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi Gaurav, 
  
    Thanks for your responce I have fixed this issue since this was quite old case open 

    I am in a need of help in creating a test class for trigger can you help me in below case 

     https://developer.salesforce.com/forums/ForumsMain?id=906F0000000kFzGIAU

Thanks
Sudhir 

 
GauravGargGauravGarg
sure Sudhir I will help you out in that. I would require some time to write test class. Still can you please share the scenario that would be helpfull in writing test class. 

Thanks,
Gaurav
Email: gauravgarg.nmim@gmail.com
skype: gaurav62990
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi Gaurav,

 If you see the thread have written trigger and apex class are been called inside the trigger also there is a custom object called discount_schedule_matrx__c which has the threashould information 

 Here on Quote there are 4 custom fields level1 to level4 to update this field trigger is writen on quotelineitem based on discount and amount provided for products trigger use a logic inside and updates value on quote. 

 Please give me a logic how to build a test class for this case it will be great help from yourside if you give me a test class prepared will follow the same 

Thanks
Sudhir
GauravGargGauravGarg
Hi Sudhir,

I looked your code and found you have made multiple queries within the QuoteLineItem trigger on quoteLineItem that would not be good way to write a code. I will look forward into this code and update you. 

Please contact me on email: gauravgarg.nmims@gmail.com or skype: gaurav62990
Thanks,
Gaurav