• Francois Roux
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 12
    Replies
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;
         }        
      }
   }
}

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;
         }        
      }
   }
}
I'm relatively new to Apex: I have a custom object with a lookup relationship to Accounts, and need to update an account field when a field from a custom object is populated with a value. I need a trigger. Any help?
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;
         }        
      }
   }
}

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;
         }        
      }
   }
}
I'm relatively new to Apex: I have a custom object with a lookup relationship to Accounts, and need to update an account field when a field from a custom object is populated with a value. I need a trigger. Any help?
I have a custom object with a lookup relationship to accounts.

I need a trigger that updates an account field when a field from the custom object is populated with a value.