You need to sign in to do that
Don't have an account?
Sukriti Sharma
Write a test class for creating and updating contact trigger on account
I wrote a trigger as followes:-
1) Create a contact
1) Create a contact
- If the checkbox (Create_Contact__c) while creating account is marked create contact wih same details.
- If the checkbox is not marked create only account.
- If the user is updating the contact which has a related contact, update with the details added.
trigger CreateContactOnAccount on Account (before insert,after insert, before update, after update) { if(Trigger.isBefore || Trigger.isInsert){ List<Contact> addCon = new List<Contact>(); for(Account acc : trigger.new){ if(acc.Create_Contact__c == true){ Contact cons = new Contact(Lastname = acc.Name, AccountId = acc.id, Fax = acc.Fax, Phone = acc.Phone, MailingStreet = acc.BillingStreet, MailingCity = acc.BillingCity, MailingCountry = acc.BillingCountry, MailingPostalCode=acc.BillingPostalCode); addCon.add(cons); } } insert addCon; } if(Trigger.isupdate && Trigger.isAfter){ set<id> getaccountid =new set<id>(); //to get the accountid which is updating for(account account : trigger.new){ getaccountid.add(account.id); list<contact> contactlist = [SELECT id, accountid FROM contact WHERE accountid =:getaccountid]; //gets the contact with same accountid List<Contact> addCon = new List<Contact>(); for(Contact cons : contactlist){ Account acc = Trigger.newmap.get(cons.accountid); cons.Lastname = account.Name; //cons.AccountId = account.id; cons.Fax = account.Fax; cons.Phone = account.Phone; cons.MailingStreet = account.BillingStreet; cons.MailingCity = account.BillingCity; cons.MailingCountry = account.BillingCountry; addCon.add(cons); } update addCon; } } }The Test class I have written so far. It is working for validateCreateContact() but not working for validateUpdateContact(). The error is logs is "List is out of bound". Please help me with the test class.
@isTest public class CreateContactOnAccountTest { @istest static void validateCreateContact(){ Account acc = new Account(); acc.Name='Test Account' ; acc.Phone= '7868754543'; acc.Create_Contact__c = true; Test.startTest(); insert acc; system.debug(acc.id); Test.stopTest(); list<contact> contactlist = [SELECT Id, Name,Phone, AccountId FROM contact WHERE AccountId =: acc.id]; System.debug(contactlist[0]); System.assertEquals('Test Account', contactlist[0].Name); } @isTest static void validateUpdateContacts(){ Account accnew = new Account(); accnew.Name = 'Test Account 2'; accnew.Phone = '76776537265'; insert accnew; accnew.Fax = '767653656'; accnew.BillingCity = 'New Delhi'; Test.startTest(); update accnew; Test.stopTest(); list<contact> contlist = [SELECT Id, Name,Phone, AccountId, Fax, MailingCity FROM contact WHERE AccountId =: accnew.id]; System.debug(contlist[0]); System.assertEquals('767653656', contlist[0].Fax); System.assertEquals('New Delhi', contlist[0].MailingCity); } }
Try Below Test Class 100% Coverage Please Mark It As Best Answer If It Helps
Thank You!
All Answers
Try Below Test Class 100% Coverage Please Mark It As Best Answer If It Helps
Thank You!