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

Test coverage for a trigger
I was trying to get a good coverage for a trigger , but no luck so far
here is my trigger on Opportunity
Map<String, Integer> classificationTypePriorities = new Map<String, Integer>{
'Sustainer' => 1,
'Donor' => 2,
'Fundraiser' => 3,
'Buyer' => 4,
'Activist' => 5,
'Volunteer' => 6,
'Prospect' => 7,
'Lead' => 8
};
Map<String, Id> classificationTypes = new Map<String, Id>();
for (cv__Engagement_Channel__c c : [SELECT Id, Name FROM cv__Engagement_Channel__c WHERE Name IN :classificationTypePriorities.keySet()]) {
classificationTypes.put(c.Name, c.Id);
}
for(Opportunity opp : trigger.new){
if(opp.RecordTypeId == rd.Id && opp.cv__Contact__r.cv__Primary_Engagement_Channel__r.id ==classificationTypes.get('Prospect')&& (opp.Child_ID__c == null || opp.Child_ID__c == ''))
conids.add(opp.cv__Contact__c);
}
and the test clas is
static testMethod void testAssignmentType()
{
List<cv__Engagement_Channel__c> classificationTypesList = new List<cv__Engagement_Channel__c>{
new cv__Engagement_Channel__c(Name = 'Sustainer'),
new cv__Engagement_Channel__c(Name = 'Donor'),
new cv__Engagement_Channel__c(Name = 'Fundraiser'),
new cv__Engagement_Channel__c(Name = 'Buyer'),
new cv__Engagement_Channel__c(Name = 'Activist'),
new cv__Engagement_Channel__c(Name = 'Volunteer'),
new cv__Engagement_Channel__c(Name = 'Prospect'),
new cv__Engagement_Channel__c(Name = 'Lead')
};
insert classificationTypesList;
Map<String , Id> classificationTypes = new Map<String, Id>();
for (cv__Engagement_Channel__c ct : classificationTypesList) {
classificationTypes.put(ct.Name, ct.Id);
}
List<RecordType> rt = [Select Id From RecordType Where Name = 'Household' and SobjectType = 'Account'];
Account a = new Account();
a.recordtypeId = rt[0].Id;
a.Name = 'Test';
insert a;
Contact c = new Contact();
c.lastName='testing';
c.Client_Id__c = '42412';
c.AccountId = a.Id;
c.cv__Primary_Engagement_Channel__c = classificationTypes.get('Prospect');
c.cv__Addressee__c = 'Mr.xyz';
insert c;
RecordType rd = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' AND Name = 'Recurring Donation'];
Opportunity opp = new Opportunity();
opp.RecordTypeId = rd.Id;
opp.cv__Recurring_Gift__c = rg.id;
opp.Name = 'Test Opp';
opp.AccountId = a.id;
opp.cv__Contact__c = c.id;
opp.CampaignId = cn.id;
opp.Amount = 30;
opp.CloseDate = Date.today();
opp.StageName = 'Received';
opp.cv__Payment_Type__c = 'Check';
insert opp;
System.debug('primary classification'+opp.cv__Contact__r.cv__Primary_Engagement_Channel__c) ;
Not sure whats going on but the debug logs shows opp.cv__Contact__r.cv__Primary_Engagement_Channel__c as null . Any help is appreciated
here is my trigger on Opportunity
Map<String, Integer> classificationTypePriorities = new Map<String, Integer>{
'Sustainer' => 1,
'Donor' => 2,
'Fundraiser' => 3,
'Buyer' => 4,
'Activist' => 5,
'Volunteer' => 6,
'Prospect' => 7,
'Lead' => 8
};
Map<String, Id> classificationTypes = new Map<String, Id>();
for (cv__Engagement_Channel__c c : [SELECT Id, Name FROM cv__Engagement_Channel__c WHERE Name IN :classificationTypePriorities.keySet()]) {
classificationTypes.put(c.Name, c.Id);
}
for(Opportunity opp : trigger.new){
if(opp.RecordTypeId == rd.Id && opp.cv__Contact__r.cv__Primary_Engagement_Channel__r.id ==classificationTypes.get('Prospect')&& (opp.Child_ID__c == null || opp.Child_ID__c == ''))
conids.add(opp.cv__Contact__c);
}
and the test clas is
static testMethod void testAssignmentType()
{
List<cv__Engagement_Channel__c> classificationTypesList = new List<cv__Engagement_Channel__c>{
new cv__Engagement_Channel__c(Name = 'Sustainer'),
new cv__Engagement_Channel__c(Name = 'Donor'),
new cv__Engagement_Channel__c(Name = 'Fundraiser'),
new cv__Engagement_Channel__c(Name = 'Buyer'),
new cv__Engagement_Channel__c(Name = 'Activist'),
new cv__Engagement_Channel__c(Name = 'Volunteer'),
new cv__Engagement_Channel__c(Name = 'Prospect'),
new cv__Engagement_Channel__c(Name = 'Lead')
};
insert classificationTypesList;
Map<String , Id> classificationTypes = new Map<String, Id>();
for (cv__Engagement_Channel__c ct : classificationTypesList) {
classificationTypes.put(ct.Name, ct.Id);
}
List<RecordType> rt = [Select Id From RecordType Where Name = 'Household' and SobjectType = 'Account'];
Account a = new Account();
a.recordtypeId = rt[0].Id;
a.Name = 'Test';
insert a;
Contact c = new Contact();
c.lastName='testing';
c.Client_Id__c = '42412';
c.AccountId = a.Id;
c.cv__Primary_Engagement_Channel__c = classificationTypes.get('Prospect');
c.cv__Addressee__c = 'Mr.xyz';
insert c;
RecordType rd = [SELECT Id FROM RecordType WHERE SObjectType = 'Opportunity' AND Name = 'Recurring Donation'];
Opportunity opp = new Opportunity();
opp.RecordTypeId = rd.Id;
opp.cv__Recurring_Gift__c = rg.id;
opp.Name = 'Test Opp';
opp.AccountId = a.id;
opp.cv__Contact__c = c.id;
opp.CampaignId = cn.id;
opp.Amount = 30;
opp.CloseDate = Date.today();
opp.StageName = 'Received';
opp.cv__Payment_Type__c = 'Check';
insert opp;
System.debug('primary classification'+opp.cv__Contact__r.cv__Primary_Engagement_Channel__c) ;
Not sure whats going on but the debug logs shows opp.cv__Contact__r.cv__Primary_Engagement_Channel__c as null . Any help is appreciated
Take a look at this post , hope this will help you
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html
Regards,
Abhi Tripathi
Salesforce Certified Developer