+ Start a Discussion
matthew.w.hampton.ax1837matthew.w.hampton.ax1837 

Apex Class Passes 100% but Trigger Not Working

Good Afternoon:

I  have the following Apex Trigger and Test Class written. The Test Class passes with 100% code coverage but when I insert or update the records, the trigger is not firing. Here are both the trigger and the test class. If anyone can help explain what I've done wrong here, please let me know.

Thanks,

Matt

trigger FiopticsNotifyMeUpdate on Fioptics_Notify_Me__c (before insert, before update) {

Set<String> street = new Set<String>();
for (Fioptics_Notify_Me__c fioptics : Trigger.new) {
        street.add(fioptics.Street_Name__c);
}

Map<String, Address__c> addressMap3 = new Map<String, Address__c>();
for(Address__c address3:[Select ID, Name, House_Number__c, Parsed_Street_Name__c, Zip_Code__c, Special_Location_Value__c, Fiber_Qualified__c, IPTV_Qualified__c from Address__c where Parsed_Street_Name__c in : street])
     addressMap3.put(address3.Parsed_Street_Name__c, address3);

List<Fioptics_Notify_Me__c> notificationsToUpdate = new List <Fioptics_Notify_Me__c>();
     for(Fioptics_Notify_Me__c a : trigger.new){
          if(addressMap3.containskey(a.Street_Name__c) && addressMap3.get(a.Street_Name__c).Special_Location_Value__c == a.Apt_Floor_Unit__c && addressMap3.get(a.Street_Name__c).Zip_Code__c == a.Zip_Code__c && addressMap3.get(a.Street_Name__c).House_Number__c == a.House_Number__c && addressMap3.get(a.Street_Name__c).Fiber_Qualified__c == 'N' && addressMap3.get(a.Street_Name__c).IPTV_Qualified__c == 'N'){
                     a.Address__c = addressMap3.get(a.Street_Name__c).ID;}
         else {
          if(addressMap3.containskey(a.Street_Name__c) && addressMap3.get(a.Street_Name__c).Special_Location_Value__c == a.Apt_Floor_Unit__c && addressMap3.get(a.Street_Name__c).Zip_Code__c == a.Zip_Code__c && addressMap3.get(a.Street_Name__c).House_Number__c == a.House_Number__c && (addressMap3.get(a.Street_Name__c).Fiber_Qualified__c == 'Y' || addressMap3.get(a.Street_Name__c).IPTV_Qualified__c == 'Y')){
                     a.Address__c = addressMap3.get(a.Street_Name__c).ID;
                     a.Fioptics_Qualified__c = TRUE;}
                     }
update notificationsToUpdate;
         }
         }

@IsTest
private class FiopticsNotifyMeUpdateTestClass {
    static testMethod void validateFiopticsNotifyMeUpdate() {

          Address__c a = new Address__c (Name = '123 Main Street 45202', House_Number__c='123', Parsed_Street_Name__c='Main St', Zip_Code__c='45202');
          insert a;

          Fioptics_Notify_Me__c b = new Fioptics_Notify_Me__c (Name = 'Matt Hampton', House_Number__c = '123', Street_Name__c='Main St', Zip_Code__c='45202');
          insert b;

          Fioptics_Notify_Me__c b2 = [Select Name, Address__c, Fioptics_Qualified__c from Fioptics_Notify_Me__c where Address__c = :a.ID];

          system.assertequals(b2.Address__c, a.ID);          
         
          Address__c c = new Address__c (Name = '456 Main Street 45202', House_Number__c='456', Parsed_Street_Name__c='Main St', Zip_Code__c='45202', HSIA_Qualification__c='G');
          insert c;

          Fioptics_Notify_Me__c d = new Fioptics_Notify_Me__c (Name = 'Matt Hampton', House_Number__c = '456', Street_Name__c='Main St', Zip_Code__c='45202');
          insert d;

          Fioptics_Notify_Me__c d2 = [Select Name, Address__c, Fioptics_Qualified__c from Fioptics_Notify_Me__c where Address__c = :c.ID];

          system.assertequals(d2.Fioptics_Qualified__c, TRUE); 
          system.assertequals(d2.Address__c, c.ID);                    
    }
}
justin_sfdcjustin_sfdc

Hi Matt,

Please look at the codes that are bolded down. You had forgotten to add the changes into the list "notificationsToUpdate". Also its better to perform the dml operation after the for loop, you had it inside the loop. And always update only if the list has any value into it.
I would also add debug statements to check if the changes have occured:

List<Fioptics_Notify_Me__c> notificationsToUpdate = new List <Fioptics_Notify_Me__c>();
     for(Fioptics_Notify_Me__c a : trigger.new){
          if(addressMap3.containskey(a.Street_Name__c) && addressMap3.get(a.Street_Name__c).Special_Location_Value__c == a.Apt_Floor_Unit__c && addressMap3.get(a.Street_Name__c).Zip_Code__c == a.Zip_Code__c && addressMap3.get(a.Street_Name__c).House_Number__c == a.House_Number__c && addressMap3.get(a.Street_Name__c).Fiber_Qualified__c == 'N' && addressMap3.get(a.Street_Name__c).IPTV_Qualified__c == 'N'){
                     a.Address__c = addressMap3.get(a.Street_Name__c).ID;}
         else {
          if(addressMap3.containskey(a.Street_Name__c) && addressMap3.get(a.Street_Name__c).Special_Location_Value__c == a.Apt_Floor_Unit__c && addressMap3.get(a.Street_Name__c).Zip_Code__c == a.Zip_Code__c && addressMap3.get(a.Street_Name__c).House_Number__c == a.House_Number__c && (addressMap3.get(a.Street_Name__c).Fiber_Qualified__c == 'Y' || addressMap3.get(a.Street_Name__c).IPTV_Qualified__c == 'Y')){
                     a.Address__c = addressMap3.get(a.Street_Name__c).ID;
                     a.Fioptics_Qualified__c = TRUE;}
                     }
notificationsToUpdate.add(a);
System.debug('Notifications to Update are: ' + notificationsToUpdate);
}
if(notificationsToUpdate.size()>0) {
     update notificationsToUpdate;
}


Let me know if you would like further help!

Thanks,
justin~sfdc
 

zzj8810zzj8810
trigger dont fire, might its not active, please check that