You need to sign in to do that
Don't have an account?
Sukriti Sharma
Write a test class for CreateContact trigger
I wrote a trigger to either create or update the associated Contact when an Account is either create or updated. But how can I write one test class for it.
The trigger I made
CreateContact.apxt
The trigger I made
CreateContact.apxt
trigger CreateContact on Account (after insert, after update, before insert) { if(Trigger.isupdate && Trigger.isAfter){ set<id> getaccountid =new set<id>(); for(account acc : trigger.new){ getaccountid.add(acc.id); system.debug(acc.id); } list<contact> conlist = [SELECT id, accountid from contact where accountid in:getaccountid]; List<Contact> addCon = new List<Contact>(); for(Contact cons : conlist){ Account acc = Trigger.newmap.get(cons.accountid); cons.Lastname = acc.Name; cons.Fax = acc.Fax; cons.Phone = acc.Phone; //cons.Email = acc.Email; cons.MailingStreet = acc.BillingStreet; cons.MailingCity = acc.BillingCity; cons.MailingCountry = acc.BillingCountry; addCon.add(cons); } update addCon; } if(Trigger.isinsert && Trigger.isbefore){ List<Contact> addCon = new List<Contact>(); for(Account acc : trigger.new){ //Account acc = Trigger.newmap.get(cons.accountid); if(acc.Create_Contact__c == true){ Contact cons = new Contact(); cons.Lastname = acc.Name; cons.Fax = acc.Fax; cons.Phone = acc.Phone; cons.MailingStreet = acc.BillingStreet; cons.MailingCity = acc.BillingCity; cons.MailingCountry = acc.BillingCountry; addCon.add(cons); } } insert addCon; } }
Please use below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
try with below code.
If this helps, Please mark it as best answer.
Thanks!!