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
Robert WynterRobert Wynter 

Need help creating a new class

Still having a hard time wrapping my head around classes. If someone could please help me create a class for the the below trigger. I'd like to learn/ understand this better for my self. So if you wouldn't mind, if your willing to help write it, could you please add some discriptive comments withing the class so I can understand the relationship between the trigger and the class. Thx much
 
trigger UpdateChildAccount on Account (after update){

//create a List of all accounts being saved
List<account> accountsBeingSaved = [SELECT id, name, parentId FROM Account WHERE parentid IN :trigger.new]; 

//create an empty List to hold child accounts later on
List<account> childAccountsToUpdate = new List<Account>(); 

	//loop through all Accounts being saved and call it 'a'
	for(Account a:trigger.new)
	{
		for(account ac:accountsBeingSaved) //loop through first list of all accounts being saved and call it 'ac'
		{ 
		if(ac.parentid == a.id) //'ac' is a child and 'a' is the parent
		{
			//add this child account to our List
			childAccountsToUpdate.add(new Account(Id=ac.Id,OwnerId=a.OwnerId,Approval_Progress__c=a.Approval_Progress__c,Status__c=a.Status__c)); 
		} 
		}
	}
	//check to see if the List is empty or not 
	if (childAccountsToUpdate.size() > 0)
	{ 
		try 
		{
			update childAccountsToUpdate; //update the child accounts
		} 
		catch(DmlException e) 
		{ 
			//catch exceptions in system.debug
			System.debug('An unexpected error has occurred: ' + e.getMessage());
		}
	}

}

 
Best Answer chosen by Robert Wynter
Amit Chaudhary 8Amit Chaudhary 8
Please use below code for your trigger
trigger UpdateChildAccount on Account (after update)
{
	AccountHandler handler = new AccountHandler();
	if(Trigger.isAfter )
	{
		if(Trigger.isUpdate)
		{
			handler.doAfterUpdate( trigger.new ,trigger.old , trigger.newMap , trigger.oldMap)
		}
	}
}

Please create one class "AccountHandler" for account handler 
public class AccountHandler 
{

public void doAfterUpdate(List<Account> lstNewAccount,List<Account> listOldAccount,Map<Id,Account> newAccountMap ,Map<Id,Account> oldAccountMap )
{
	List<account> accountsBeingSaved = [SELECT id, name, parentId FROM Account WHERE parentid IN :lstNewAccount]; 
	List<account> childAccountsToUpdate = new List<Account>(); 
	for(Account a: lstNewAccount)
	{
		for(account ac:accountsBeingSaved) //loop through first list of all accounts being saved and call it 'ac'
		{ 
		if(ac.parentid == a.id) //'ac' is a child and 'a' is the parent
		{
			//add this child account to our List
			childAccountsToUpdate.add(new Account(Id=ac.Id,OwnerId=a.OwnerId,Approval_Progress__c=a.Approval_Progress__c,Status__c=a.Status__c)); 
		} 
		}
	}
	if (childAccountsToUpdate.size() > 0)
	{ 
		try 
		{
			update childAccountsToUpdate; //update the child accounts
		} 
		catch(DmlException e) 
		{ 
			//catch exceptions in system.debug
			System.debug('An unexpected error has occurred: ' + e.getMessage());
		}
	}
}

public void doBeforeUpdate( List<Account> lstNewAccount,List<Account> listOldAccount,Map<Id,Account> newAccountMap ,Map<Id,Account> oldAccountMap )
{
	
}
}

Please refer blog how to stop recursive trigger :-
http://amitsalesforce.blogspot.in/search/label/Trigger


Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please use below code for your trigger
trigger UpdateChildAccount on Account (after update)
{
	AccountHandler handler = new AccountHandler();
	if(Trigger.isAfter )
	{
		if(Trigger.isUpdate)
		{
			handler.doAfterUpdate( trigger.new ,trigger.old , trigger.newMap , trigger.oldMap)
		}
	}
}

Please create one class "AccountHandler" for account handler 
public class AccountHandler 
{

public void doAfterUpdate(List<Account> lstNewAccount,List<Account> listOldAccount,Map<Id,Account> newAccountMap ,Map<Id,Account> oldAccountMap )
{
	List<account> accountsBeingSaved = [SELECT id, name, parentId FROM Account WHERE parentid IN :lstNewAccount]; 
	List<account> childAccountsToUpdate = new List<Account>(); 
	for(Account a: lstNewAccount)
	{
		for(account ac:accountsBeingSaved) //loop through first list of all accounts being saved and call it 'ac'
		{ 
		if(ac.parentid == a.id) //'ac' is a child and 'a' is the parent
		{
			//add this child account to our List
			childAccountsToUpdate.add(new Account(Id=ac.Id,OwnerId=a.OwnerId,Approval_Progress__c=a.Approval_Progress__c,Status__c=a.Status__c)); 
		} 
		}
	}
	if (childAccountsToUpdate.size() > 0)
	{ 
		try 
		{
			update childAccountsToUpdate; //update the child accounts
		} 
		catch(DmlException e) 
		{ 
			//catch exceptions in system.debug
			System.debug('An unexpected error has occurred: ' + e.getMessage());
		}
	}
}

public void doBeforeUpdate( List<Account> lstNewAccount,List<Account> listOldAccount,Map<Id,Account> newAccountMap ,Map<Id,Account> oldAccountMap )
{
	
}
}

Please refer blog how to stop recursive trigger :-
http://amitsalesforce.blogspot.in/search/label/Trigger


Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
 
This was selected as the best answer
Robert WynterRobert Wynter
If I understand correctly I should move my existing code from my trigger into a class "AccountHandler" then in my trigger call the class object? is that correct
Amit Chaudhary 8Amit Chaudhary 8
Yes , you are right,

Please refer below link for more help
https://developer.salesforce.com/forums/ForumsMain?id=906F000000093cKIAQ
https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices
http://www.embracingthecloud.com/2010/07/08/ASimpleTriggerTemplateForSalesforce.aspx



Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
 
Robert WynterRobert Wynter
Thank you very helpfull
Robert WynterRobert Wynter
it worked great in the Sandbox nut in production it failed. Too many SOQL queries: 101
 
caused by element : FlowRecordUpdate.myRule_1_A1
 
caused by: Too many SOQL queries: 101
 
Amit Chaudhary 8Amit Chaudhary 8
Hi Robert Wynter,

Please port your code here or send me your code on my email id. I will help you.
Same account trigger code is failing in production ? If yes then please send me all account trigger code (if you have multiple account trigger.

Happy to help u

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
ethan greyethan grey
Great answer!