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
Narasimha Reddy 164Narasimha Reddy 164 

i am new to sf and i want to write handler classs and helper class for the below trigger so if you assist it would be great

trigger updatecontact on Account (after insert, after update) {
list<Contact> conlist=new list<Contact>([select Id,Name,AccountId,Industry__c from Contact where AccountId IN:Trigger.new ]);
    map<Id,Account>mapAccountIdAccount=new map<Id,Account>([select Id,Industry,AccountSubType1__c,AccountType1__c,businessunit__c,ProductInterest__c,
                                                            TranslocRelationship__c from Account where Id IN:Trigger.new]);
  list<Contact>contactstobeupdate=new list<Contact>();
    if(Trigger.isInsert || Trigger.isUpdate && Trigger.isAfter) {
        
        for(contact obj:conlist){
            
            Account objAccount=mapAccountIdAccount.get(obj.AccountId);
            obj.TranslocRelationship__c =objAccount.TranslocRelationship__c;
            obj.ProductInterest__c=objAccount.ProductInterest__c;
            //obj.Industry__c=objAccount.Industry;
            obj.AccountType__c=objAccount.AccountType1__c;
            obj.AccountSubType__c=objAccount.AccountSubType1__c;
            contactstobeupdate.add(obj);
        }
        update contactstobeupdate;
    }
  }
vishal-negandhivishal-negandhi

This can easily be achieved without code. 

However if your intention is to learn apex, then try the below

1. Trigger

Trigger AccountTrigger on Account (after insert, after update){
	if(Trigger.isAfter){
		if(Trigger.isInsert){
			AccountTriggerHandler.afterInsert(trigger.newMap);
		}
		else if(Trigger.isInsert){
			AccountTriggerHandler.afterUpdate(trigger.oldMap, trigger.newMap);
		}
	}
}

Note: it's always a good idea to pass new map instead of new list and especially important in update triggers to also pass oldmap, so that if you're ever writing a logic where you need to compare if the field value has changed, oldMap is useful.

2. Trigger Handler

public class AccountTriggerHandler {
	public static void afterInsert(Map<Id, Account> newMap){
		AccountHelper.updateContacts(newMap);
	}
	
	public static void afterUpdate(Map<Id, Account> oldMap, Map<Id, Account> newMap){
		AccountHelper.updateContacts(newMap);
	}
}

Note: It's not mandatory to have handler + helper, you can also write your logic inside handler classes. One important reason to have handlers and helpers separated is to be able to segregate your business logic elsewhere. 

In the above handler, I could have used just one method for both insert and update and then called helper.updatecontacts, but right now you've one scenario and in future when you've more logic, it's always better to have a separate method for each trigger event.

3. Helper

public class AccountHelper {
	public static void updateContacts(Map<Id, Account> newMap){
		List<Contact> lstContactsToUpdate = new List<Contact>();
		
		for(Contact contactRecord : [select Id,Name,AccountId,Industry__c from Contact where AccountId IN :newMap.keyset()]){
			Account accountRecord = newMap.get(contactRecord.AccountId);
            contactRecord.TranslocRelationship__c = accountRecord.TranslocRelationship__c;
            contactRecord.ProductInterest__c = accountRecord.ProductInterest__c;
            //contactRecord.Industry__c = accountRecord.Industry;
            contactRecord.AccountType__c = accountRecord.AccountType1__c;
            contactRecord.AccountSubType__c = accountRecord.AccountSubType1__c;
            lstContactsToUpdate.add(contactRecord);
		}
		
		if(!lstContactsToUpdate.isEmpty){
			update lstContactsToUpdate;
		}
	}
}

Hope this helps you. 

There might be some syntax errors, please fix them. I just wrote this here so didn't validate all the syntax and variable names. 

 

Best of luck!