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
SFDC GuestSFDC Guest 

Convert apex trigger into helper class

Hi All,

I am using below trigger to update child records. Can someone please convert logic into helper class and call it from trigger. Thanks in advance.
 
trigger ContactUpdate on Account (after update) {
    Map < Id,  Account > mapAccount = new Map < Id, Account >();
    List<Contact> listContact = new List<Contact>();
   
    for(Account acct : trigger.new)
        mapAccount.put(acct.Id, acct);
   
    listContact = [ SELECT MailingStreet, MailingCity, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
   
    if ( listContact.size() > 0 ) {
        for ( Contact con : listContact ) {
            con.MailingStreet = mapAccount.get(con.AccountId).BillingStreet;
            con.MailingCity = mapAccount.get(con.AccountId).BillingCity;
        }
        update listContact;
    }
}


 
Best Answer chosen by SFDC Guest
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Trigger:
trigger ContactUpdate on Account (after update) {
    if(Trigger.isUpdate) {
        ContactUpdateHandler.updateContact(Trigger.new);
    }
}

Handler:
public class ContactUpdateHandler {
    
    public static void updateContact(List<Account> accounts) {
        Map < Id,  Account > mapAccount = new Map < Id, Account >();
        List<Contact> listContact = new List<Contact>();
        
        for(Account acct : accounts)
            mapAccount.put(acct.Id, acct);
        
        listContact = [ SELECT MailingStreet, MailingCity, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
        
        if ( listContact.size() > 0 ) {
            for ( Contact con : listContact ) {
                con.MailingStreet = mapAccount.get(con.AccountId).BillingStreet;
                con.MailingCity = mapAccount.get(con.AccountId).BillingCity;
            }
            update listContact;
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Rounak SharmaRounak Sharma
Hi ,
if you want to create a helper class for this. You have to create a handler class too. First create a handler class and call that handler class in this trigger with events and all as it it. Then put your logic in a helper class. Have all the methods and logic in helper class and call the helper class in handler class. Like if method in helper is static. Go with
Class name.method name();

Please let me know if you have doubt
Thanks 
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Trigger:
trigger ContactUpdate on Account (after update) {
    if(Trigger.isUpdate) {
        ContactUpdateHandler.updateContact(Trigger.new);
    }
}

Handler:
public class ContactUpdateHandler {
    
    public static void updateContact(List<Account> accounts) {
        Map < Id,  Account > mapAccount = new Map < Id, Account >();
        List<Contact> listContact = new List<Contact>();
        
        for(Account acct : accounts)
            mapAccount.put(acct.Id, acct);
        
        listContact = [ SELECT MailingStreet, MailingCity, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
        
        if ( listContact.size() > 0 ) {
            for ( Contact con : listContact ) {
                con.MailingStreet = mapAccount.get(con.AccountId).BillingStreet;
                con.MailingCity = mapAccount.get(con.AccountId).BillingCity;
            }
            update listContact;
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Nayana KNayana K
You can have one handler class
public class AccountTriggerHandler {

    public void onAfterUpdate(Map<Id, Account> mapAccountNew) {
        /* Logic to update contacts to retain same address as account - START */
        List<Contact> lstContactsToUpdate = new List<Contact>();
        Account objTempAcc;
        for(Contact objContact : [SELECT AccountId FROM Contact WHERE AccountId IN: mapAccountNew.keySet()]) {
            objTempAcc = mapAccountNew.get(objContact.AccountId);
            objContact.MailingStreet = objTempAcc.BillingStreet;
            objContact.MailingCity = objTempAcc.BillingCity;
            lstContactsToUpdate.add(objContact);
        }
        
        if(!lstContactsToUpdate.isEmpty()) {
            update lstContactsToUpdate;
        }
        /* Logic to update contacts to retain same address as account - END */
    }
}

Trigger:
trigger AccountTrigger on Account (after update) {
	AccountTriggerHandler objHandler = new AccountTriggerHandler();
	
	if(Trigger.isAfter && Trigger.isUpdate) {
		objHandler.onAfterUpdate(Trigger.NewMap);
	}
}

 
Bryan Leaman 6Bryan Leaman 6
This seems like a quiz type of problem rather than real-world -- why would you want to force contacts to be located at their company's primary location? And if you're forcing an address and city, shouldn't state and postal code be included?

All that aside ... maybe something like this?  
I know some people like to make their handler more generic and just call an "AfterUpdate" handler method. I prefer to see what is being done when I look at the trigger itself. Also, best practice would be to only update the contacts that need to be updated -- the ones whose street or city differs from the account street or city. 

(I haven't compiled or tested this, so there could be typo's.)
trigger ContactUpdate on Account (after update) {
	if (trigger.isAfter && trigger.isUpdate) {
		AccountTriggerHandler.UpdateContactAddrFromAccount(Trigger.newMap);
	}
}


public class AccountTriggerHandler {
	public static void UpdateContactAddrFromAccount(Map<Id,Account> mapAccount) {
		List<Contact> listContact = AccountContactHelper.ContactsForAccountIds(mapAccount.keySet());
		List<Contact> updContacts = AccountContactHelper.ContactsNeedingAddressUpdate(listContacts, mapAccount);
		update(updContacts);
	}
}


public class AccountContactHelper {
	public static List<Contact> ContactsForAccountIds(Set<Id> acctIds) {
		return [
			SELECT MailingStreet, MailingCity, AccountId 
			FROM Contact 
			WHERE AccountId IN : mapAccount.keySet()
		];
	}

	public static List<Contact> ContactsNeedingAddressUpdate(List<Contact> listContact, Map<Id,Account> mapAccount) {
		List<Contact> rtnContactList = new List<Contact>();
		for(Contact con : listContact) {
			Account acct = mapAccount.get(con.AccountId);
			if (acct.BillingStreet!=con.MailingStreet || acct.BillingCity!=con.MailingCity) {
				rtnContactList.add(new Contact(
					Id=con.Id, 
					MailingStreet=acct.BillingStreet, 
					MailingCity=acct.BillingCity));
			}
		}
		return rtnContactList;
	}
}

 
Ajay K DubediAjay K Dubedi
Hi,

Please try the below code, 

Handler Class : -
 
public class ContactUpdate 
{
    public static void ContactUpdate_Method(List<Account> Account_List)
    {
        Map < Id,  Account > mapAccount = new Map < Id, Account >();
        for(Account acct : Account_List)
        {
            mapAccount.put(acct.Id, acct);
        }
        List<Contact> listContact = [ SELECT MailingStreet, MailingCity, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
        if ( listContact.size() > 0 ) 
        {
            for ( Contact con : listContact ) 
            {
                con.MailingStreet = mapAccount.get(con.AccountId).BillingStreet;
                con.MailingCity = mapAccount.get(con.AccountId).BillingCity;
            }
            update listContact;
        }
    }
}

Trigger:-
 
trigger ContactUpdate on Account (After Update) 
{
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        ContactUpdate.ContactUpdate_Method(Trigger.New);
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com 
SFDC GuestSFDC Guest
Thank you all for your help.