You need to sign in to do that
Don't have an account?
Narasimha 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;
}
}
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;
}
}
This can easily be achieved without code.
However if your intention is to learn apex, then try the below
1. Trigger
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
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
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!