You need to sign in to do that
Don't have an account?
Francois Roux
Test returns 0% code coverage - Cross-object field update trigger
Problem: I have written a trigger that updates cross-object fields. The trigger passes and works, but the test returns 0% code coverage. Can anyone help correct the test?
Custom object Fundraise__c has a lookup relationship to Account object and to Fund_c object. The trigger updates FundABC__c, FundDEF__c and Fund__GHI checkbox fields in Account object with 'true' everytime Fundraise__c gets created or updated with field Fund__c equal to FundABC, FundDEF or FundGHI respectively.
Trigger:
trigger UpdateAcct on Fundraise__c (after insert, after update) {
Set<Id> acid = new Set<Id>();
for(Fundraise__c temp0:Trigger.New) {
acid.add(temp0.Account__c);
}
Map<Id,Account> act = new Map<Id,Account>([Select Id from Account where Id in :acid]);
Set<Account> acts = new Set<Account>();
List<Account> acts1 = new List<Account>();
for(Fundraise__c temp:Trigger.New) {
if(temp.Fund__c == 'FundABC') {
if(act.containsKey(temp.Account__c)) {
Account upd = act.get(temp.Account__c);
upd.FundABC__c = true ;
acts.add(upd);
}
}
if(temp.Fund_Copy__c == 'FundDEF') {
if(act.containsKey(temp.Account__c)) {
Account upd = act.get(temp.Account__c);
upd.FundDEF__c = true ;
acts.add(upd);
}
}
if(temp.Fund_Copy__c == 'FundGHI') {
if(act.containsKey(temp.Account__c)) {
Account upd = act.get(temp.Account__c);
upd.FundGHI__c = true ;
acts.add(upd);
}
}
}
acts1.addAll(acts);
update acts1;
}
Test:
@isTest
public class testUpdateAcct {
static testmethod void invokeTrigger() {
Account ac = new Account();
ac.Name = 'Test Account';
Database.SaveResult sr = Database.insert(ac, false);
if(sr.isSuccess()) {
Fundraise__c p = new Fundraise__c();
p.Name = 'Test Fundraise';
p.Account__c = sr.getId();
Database.SaveResult sr1 = Database.insert(p, false);
if(sr1.isSuccess()) {
Id pid = sr1.getId();
Fundraise__c p1 = [Select Id, Name from Fundraise__c where Id =:pid];
p1.Fund__c = 'FundABC';
update p1;
p1.Fund__c = 'FundDEF';
update p1;
p1.Fund__c = 'FundGHI';
update p1;
p1.Fund__c = '';
update p1;
}
}
}
}
Custom object Fundraise__c has a lookup relationship to Account object and to Fund_c object. The trigger updates FundABC__c, FundDEF__c and Fund__GHI checkbox fields in Account object with 'true' everytime Fundraise__c gets created or updated with field Fund__c equal to FundABC, FundDEF or FundGHI respectively.
Trigger:
trigger UpdateAcct on Fundraise__c (after insert, after update) {
Set<Id> acid = new Set<Id>();
for(Fundraise__c temp0:Trigger.New) {
acid.add(temp0.Account__c);
}
Map<Id,Account> act = new Map<Id,Account>([Select Id from Account where Id in :acid]);
Set<Account> acts = new Set<Account>();
List<Account> acts1 = new List<Account>();
for(Fundraise__c temp:Trigger.New) {
if(temp.Fund__c == 'FundABC') {
if(act.containsKey(temp.Account__c)) {
Account upd = act.get(temp.Account__c);
upd.FundABC__c = true ;
acts.add(upd);
}
}
if(temp.Fund_Copy__c == 'FundDEF') {
if(act.containsKey(temp.Account__c)) {
Account upd = act.get(temp.Account__c);
upd.FundDEF__c = true ;
acts.add(upd);
}
}
if(temp.Fund_Copy__c == 'FundGHI') {
if(act.containsKey(temp.Account__c)) {
Account upd = act.get(temp.Account__c);
upd.FundGHI__c = true ;
acts.add(upd);
}
}
}
acts1.addAll(acts);
update acts1;
}
Test:
@isTest
public class testUpdateAcct {
static testmethod void invokeTrigger() {
Account ac = new Account();
ac.Name = 'Test Account';
Database.SaveResult sr = Database.insert(ac, false);
if(sr.isSuccess()) {
Fundraise__c p = new Fundraise__c();
p.Name = 'Test Fundraise';
p.Account__c = sr.getId();
Database.SaveResult sr1 = Database.insert(p, false);
if(sr1.isSuccess()) {
Id pid = sr1.getId();
Fundraise__c p1 = [Select Id, Name from Fundraise__c where Id =:pid];
p1.Fund__c = 'FundABC';
update p1;
p1.Fund__c = 'FundDEF';
update p1;
p1.Fund__c = 'FundGHI';
update p1;
p1.Fund__c = '';
update p1;
}
}
}
}
create instance for Fundraise__c out side the If loop and use this instance inside the loop
and give acid ='some sample id';
Account a = new Account();
a.fieldname='sample data';
a.filedname='sample data';
insert a;
Fundraise__C fr = new Fundraise__c();
fr.fieldname='some data';
fr.filedname='some data';
insert fr;
and you have to pass the trigger.new value
in testmethod
thank you
@isTest
public class testUpdateAcct {
static testmethod void invokeTrigger() {
Account ac = new Account();
ac.Name = 'Test Account';
ac.Investment_Capacity__c = 'Yes-High';
ac.Account_Origin__c = 'PERS';
ac.Investor_Type__c = 'Bank';
ac.Region__c = 'West Europe';
ac.Role__c = 'LP';
insert ac;
Fund__c fu = new Fund__c();
fu.Name = 'Test Fund';
insert fu;
Fundraise__c fr = new Fundraise__c();
fr.Name = 'Test Fundraise';
fr.Account__c = ac.id;
insert fr;
Test.startTest();
fr.Fund__c = 'FundABC';
update fr;
fr.Fund__c = 'FundDEF';
update fr;
fr.Fund__c = 'FundHGI';
update fr;
Test.stopTest();
}
}