function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Francois RouxFrancois Roux 

Trigger shows Code Coverage 0% after test in sandbox


Problem: I have written a trigger and a test. In the sandbox, the test saves and passes, but the trigger shows Code Coverage 0%. Can anybody help? Thanks in advance.
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 updateAcct1 on Fundraise__c (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 testUpdateAcct1 { 
   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 = 'Essai';   
         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_Copy__c = 'FundABC';
            update p1;
            p1.Fund_Copy__c = 'FundDEF';
            update p1;
            p1.Fund_Copy__c = 'FundGHI';
            update p1;
            p1.Fund__c = '';
            update p1;
         }        
      }
   }
}
Akhil AnilAkhil Anil
Hi Francois,

Are you facing the issue only in sandbox ?

 
Akhil AnilAkhil Anil
Try the below snippet and let me know. Do you have two different fields named Fund__c and Fund_Copy__c ?
 
@isTest(SeeAllData=true)
public class testUpdateAcct1 { 
   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 = 'Essai';   
         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_Copy__c = 'FundDEF';
            update p1;
            p1.Fund_Copy__c = 'FundGHI';
            update p1;
            p1.Fund__c = '';
            update p1;
            p1.Fund_Copy__c = '';
            update p1;
         }        
      }
   }
}

 
Francois RouxFrancois Roux
You're right, I have only one field: Fund__c which is a look up from Fundraise__c into Fund. I corrected that. The test still does not work, in production either: I get 0% coverage in my organization at time of deployment. I must be missing something completly. Hint: there is no parent-child relationship, only look-up from Fundraise__c into Account and Fund.
Francois RouxFrancois Roux
Help help help ...
Francois