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
jadenjaden 

trigger works fine when direct change to account but not in batch

I have a trigger that is calling an @future to avoid hitting both SOQL limits and script limits.  It workis fine when you go into the account and make the change manually but not when run for a batch. 

 

Here is the trigger:

 

trigger ZipAcctChangeShare on Account (before update)  {

 	set<id> idso = new set<id>();
 	set<string> zipso = new set<string>();
	For(Account acc :trigger.old)
	{
		//Only send id's for those accounts with zip changes
		//system.debug('the old id trigger.oldMap.get(Acc.Id) is  ' + trigger.oldMap.get(Acc.Id));
		//system.debug('the new id trigger.newMap.get(Acc.Id) is ' + trigger.newMap.get(Acc.Id) + ' the acc.dd is ' + acc.id);
		//If (trigger.newMap.get(Acc.Id).BillingPostalCode != Acc.BillingPostalCode)
		If (trigger.newMap.get(Acc.Id).BillingPostalCode != Acc.BillingPostalCode)
		{ 
			idso.add(Acc.Id);
			zipso.add(Acc.BillingPostalCode);
		}
	}

	//system.debug('the size of idso is ' + idso.size());
	if(idso.size() > 0 && zipso.Size() > 0)
    	ProcessZipChangeOnAcct.ChangeShare(idso,zipso);
}

 

Here is the @future

 

public class ProcessZipChangeOnAcct {
@Future
    public static void ChangeShare(Set<Id> idso, Set<String> zipso) {
 	
 		//Build list of old zip code needed 
	 	list<Account> zipsrecs = [SELECT id, BillingPostalCode from Account WHERE id in :idso]; 
		
		//Get All territory zip records for zip codes from above
		List<Territory_Zip_Code__c> tc = new List<Territory_Zip_Code__c>();
		For(List< Territory_Zip_Code__c> t: [SELECT id, GroupId__c, name FROM Territory_Zip_Code__c WHERE Name in :zipso])
	                Tc.addall(t);
	              
		//List to hold account share to be deleted
		List<AccountShare> tbdACCShList = New List<AccountShare>();
	
		//get list of AccountShares that have an ID matching the Account ID’s in the set
		List<AccountShare> lAS = [Select id, AccountID, UserOrGroupID From AccountShare WHERE AccountID in : idso];
		
		For(Account acc :zipsrecs)
		{
           for (Territory_Zip_Code__c tzc: tc)
           { 
 	       	for (AccountShare Ashare :lAS) 
	        {	
			    if(Ashare.accountid == Acc.id && AShare.UserOrGroupId == tzc.GroupID__c) 
			 	 	tbdACCShList.add(Ashare);

			}   //for (AccountShare Ashare :lAS)
           }    //For Terr_Zip 
	    }  //For Account 
	      
	   	//If we have records to be deleted, delete the records    
	   	//system.debug('the size of the delete list is ' + tbdACCShList.size());       	 
		If(tbdACCShList.size() > 0)
		{
			try
			{
				Delete tbdACCShList;
			}
			catch (Exception e) {
			}
		}
    }
}

 Thank you for any assitance

Best Answer chosen by Admin (Salesforce Developers) 
jadenjaden

This was re-written using maps by another progrmmer

 

thank you for any assistance

All Answers

spraetzspraetz

What is the error it gives?  or does it just simply not update the records?

jadenjaden

It does not throw an error, it just does not fo the delete of hares at the bottom.

 

Thanks

Starz26Starz26

Is this the same thing we had working without using the @future?

 

When this runs, in debug, is the size of the tbd list correct?

 

If you system.debug(e) does it give any message?

 

 

jadenjaden

Yes it is the same one split up.

 

User ran a bulk test and said no errors were thrown but it did not delete the shares it should. 

 

When I modify an account zip code manually; the correct shares are deleted.

 

Thanks

spraetzspraetz

In your test, do you have Test.StartTest(); and Test.StopTest(); surrounding your operation that kicks off the @Future job?

 

You need to have those surrounding it as @Future normally runs in a separate thread and calling Test.StopTest() waits and forces the job to run synchronously within the test.

 

After you call Test.StopTest() you can then do your queries to determine if it worked properly.

jadenjaden

This was re-written using maps by another progrmmer

 

thank you for any assistance

This was selected as the best answer