You need to sign in to do that
Don't have an account?

Adding the Trigger in the Apex class in Salesforce
UseCase In Account Object:
Created a Trigger when the ParentAccount is selected in account object ,need to copy the phone number and name and insert the record.
how can i add the Trigger in the Apex Class and Write Test Class...
Apex Class:
=========
Created a Trigger when the ParentAccount is selected in account object ,need to copy the phone number and name and insert the record.
how can i add the Trigger in the Apex Class and Write Test Class...
trigger CopyAccountTriggerTest on Account (before insert) { if(checkRecursiveTest.runOnce()) { Set<Id> accountIds = new Set<Id>(); for (Account acc : Trigger.new) { if (acc.ParentId != null){ accountIds.add(acc.ParentId); } if(accountIds.size() > 0){ List<Account> accounts = [select id,Name,Phone from account where id in :accountIds ]; for (Account acc2 : Trigger.new) { if (acc2.ParentId != null){ acc2.Name = 'Duplicate'+accounts[0].Name; acc2.Phone= 'Duplicate'+accounts[0].Phone; } } } } } }
Apex Class:
=========
public class checkRecursiveTest { private static boolean run = true; public static boolean runOnce(){ if(run){ run=false; return true; }else{ return run; } } }
Here is the code for you reference with the Test class 100% Coverage. You do not need checkRecursiveTest.runOnce since you are running on before Insert so not including that in my code.
Test Class
I hope this will help
All Answers
This seems to be duplicate for the other question . Can you close this question by marking this as best answer so it won't be duplicate efforts for others.
Thanks,
Here is the code for you reference with the Test class 100% Coverage. You do not need checkRecursiveTest.runOnce since you are running on before Insert so not including that in my code.
Test Class
I hope this will help