You need to sign in to do that
Don't have an account?
Jonathan Sheehan
Problem with Test Code
My test code is only covering 36% of my Apex trigger and I'm stuck on how to get it up...Can someone please point me in the right direction?
TRIGGER:
trigger TempFieldsToCampaignAndEvent on Contact (after insert) {
List<CampaignMember> membersToAdd = new List<CampaignMember>();
List<Event> NewEvents = new List<Event>();
for (Contact c: Trigger.new) {
if(c.TempCampaign__c != null) {
CampaignMember cm = new CampaignMember(CampaignId=c.TempCampaign__c, ContactId=c.Id, Status=c.TempCampaignStatus__c);
membersToAdd.add(cm);
}
system.debug(c.TempEventDescription__c);
if(c.TempEventDescription__c != null){
Event e = new Event();
e.StartDateTime = c.TempEventTime__c;
e.ActivityDateTime = c.TempEventTime__c;
e.Subject = c.TempEventSubject__c;
e.WhatId = c.TempCampaign__c;
e.Description = c.TempEventDescription__c;
e.WhoId = c.Id;
e.OwnerId = '0051a000000hmuN';
e.DurationInMinutes = 0;
NewEvents.add(e);
}
}
database.insert (membersToAdd, false);
insert NewEvents;
}
TEST CLASS
@IsTest
public class AAAllTest {
public testMethod static void contactInsertion() {
// Consider setting some address fields as required by copyAddressFields
Contact c = new Contact(lastname='testing', firstname='apex');
insert c;
//Make some assertions based on the results of the copyAddressFields call
}
public testMethod static void contactInsertionFails() {
// Set some fields that will cause an Exception in copyAddressFields
Contact c = new Contact(lastname='testing', firstname='apex');
insert c;
//Make some assertions that errors were added to the Contact
}
public testMethod static void bulkifedInsertion() {
Contact[] contactsToCreate = new Contact[]{};
for(Integer x=0; x<200;x++){
Contact ct = new Contact(lastname='testing',firstname='apex');
contactsToCreate.add(ct);
}
Test.startTest();
insert contactsToCreate;
Test.stopTest();
}
public testMethod static void CampaignInsertion() {
// Consider setting some address fields as required by copyAddressFields
Campaign m = new Campaign(Name='Test Campaign');
insert m;
//Make some assertions based on the results of the copyAddressFields call
}
public testMethod static void EventInsertion() {
// Consider setting some address fields as required by copyAddressFields
date ctd = Date.today(); // will set the todays date
Event e = new Event(Subject='Test', StartDateTime=ctd, EndDateTime=ctd, WhoId='0031700000AU6P9', DurationInMinutes=0);
insert e;
}
public testMethod static void CampaignMembers() {
// Consider setting some address fields as required by copyAddressFields
date ctd = Date.today(); // will set the todays date
CampaignMember cm = new CampaignMember(CampaignId='70117000001ARcU', ContactId='0031700000AU6P4', Status='Responded');
database.insert (cm, false);
//Make some assertions based on the results of the copyAddressFields call
}
}
TRIGGER:
trigger TempFieldsToCampaignAndEvent on Contact (after insert) {
List<CampaignMember> membersToAdd = new List<CampaignMember>();
List<Event> NewEvents = new List<Event>();
for (Contact c: Trigger.new) {
if(c.TempCampaign__c != null) {
CampaignMember cm = new CampaignMember(CampaignId=c.TempCampaign__c, ContactId=c.Id, Status=c.TempCampaignStatus__c);
membersToAdd.add(cm);
}
system.debug(c.TempEventDescription__c);
if(c.TempEventDescription__c != null){
Event e = new Event();
e.StartDateTime = c.TempEventTime__c;
e.ActivityDateTime = c.TempEventTime__c;
e.Subject = c.TempEventSubject__c;
e.WhatId = c.TempCampaign__c;
e.Description = c.TempEventDescription__c;
e.WhoId = c.Id;
e.OwnerId = '0051a000000hmuN';
e.DurationInMinutes = 0;
NewEvents.add(e);
}
}
database.insert (membersToAdd, false);
insert NewEvents;
}
TEST CLASS
@IsTest
public class AAAllTest {
public testMethod static void contactInsertion() {
// Consider setting some address fields as required by copyAddressFields
Contact c = new Contact(lastname='testing', firstname='apex');
insert c;
//Make some assertions based on the results of the copyAddressFields call
}
public testMethod static void contactInsertionFails() {
// Set some fields that will cause an Exception in copyAddressFields
Contact c = new Contact(lastname='testing', firstname='apex');
insert c;
//Make some assertions that errors were added to the Contact
}
public testMethod static void bulkifedInsertion() {
Contact[] contactsToCreate = new Contact[]{};
for(Integer x=0; x<200;x++){
Contact ct = new Contact(lastname='testing',firstname='apex');
contactsToCreate.add(ct);
}
Test.startTest();
insert contactsToCreate;
Test.stopTest();
}
public testMethod static void CampaignInsertion() {
// Consider setting some address fields as required by copyAddressFields
Campaign m = new Campaign(Name='Test Campaign');
insert m;
//Make some assertions based on the results of the copyAddressFields call
}
public testMethod static void EventInsertion() {
// Consider setting some address fields as required by copyAddressFields
date ctd = Date.today(); // will set the todays date
Event e = new Event(Subject='Test', StartDateTime=ctd, EndDateTime=ctd, WhoId='0031700000AU6P9', DurationInMinutes=0);
insert e;
}
public testMethod static void CampaignMembers() {
// Consider setting some address fields as required by copyAddressFields
date ctd = Date.today(); // will set the todays date
CampaignMember cm = new CampaignMember(CampaignId='70117000001ARcU', ContactId='0031700000AU6P4', Status='Responded');
database.insert (cm, false);
//Make some assertions based on the results of the copyAddressFields call
}
}
Please try below code.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
All Answers
Please try below code.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
Your trigger looks good implementation as per SFDC best practices but I like to point out usage of e.OwnerId = '0051a000000hmuN'; IF you are doing any implementation of apex code do not hard code any SFDC Id, If you do this code of line will not work in production. So plz try to avoid this.
For test coverage plz try below code, which will cover 100 %.
Plz use TempEventTime__c=system.now() instead of TempEventTime__c=system.Datetime.
Let me know above code works for you, If yes, then mark this as best ans. This will help others to solve similar issues.