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
nickboekanickboeka 

test class for my trigger

I'm a little stumped on why a trigger I already have deployed in production, is now throwing validation errors when I am trying to deploy a brand new class from my sandbox to prod.  I am getting an error that this trigger needs at least 1% of code covereage, and that I currently have 0.  Trigger is below.  Can someone help me either with a test class that will satisfy the requirement, or what the process is to satisfy the requirement.  thanks in advance for the help

 

trigger TaskFields on Task (before insert, before update) {

    String stringId;
    String subjectId;
       Set<Id> contactsToQuery = new Set<Id>();
    for(Task t : Trigger.new){
        stringId = '' + t.WhoId;
        subjectId = t.subject;
                    contactsToQuery.add(t.WhoId);}        
    if(stringId.substring(0,3) == '003' && subjectId.startsWith('URGENT:')){        
        
    
    for(Task t : Trigger.new){
    Map<Id,Contact> conMap = new Map<Id,Contact>([select Id, AccountId, Account.Name, Owner.Name, Account.Customer_Type__c, 
                             Account.BillingState, Approved_Employer_Rep__c, OwnerId, Owner.Team__c, mkto2__Lead_Score__c,
                             mkto_si__Last_Interesting_Moment_Desc__c, mkto_si__Last_Interesting_Moment_Date__c, Account.Count_of_Open_Active_Ops__c 
                             from Contact where Id in : contactsToQuery]);
    
        
    
        if(conMap.containsKey(t.WhoId)){
            t.AccountID__c= conMap.get(t.WhoId).AccountId;
            t.Account_Name__c=conMap.get(t.WhoId).Account.Name;
            t.Customer_Type__c=conMap.get(t.WhoId).Account.Customer_Type__c;
            t.Account_Billing_State__c=conMap.get(t.WhoId).Account.BillingState;
            t.FEA_Approved__c=conMap.get(t.WhoId).Approved_Employer_Rep__c;
            t.Team__c=conMap.get(t.WhoId).Owner.Team__c;
            t.Account_Owner__c=conMap.get(t.WhoId).Owner.Name;
            t.Lead_Score__c=conMap.get(t.WhoId).mkto2__Lead_Score__c;
            t.Last_Interesting_Moment_Date__c=conMap.get(t.WhoId).mkto_si__Last_Interesting_Moment_Date__c;
            t.Last_Interesting_Moment_Desc__c=conMap.get(t.WhoId).mkto_si__Last_Interesting_Moment_Desc__c;
            t.Active_Account_Opportunities__c=conMap.get(t.WhoId).Account.Count_of_Open_Active_Ops__c;
            
        }
        if( t.Active_Account_Opportunities__c>0 || t.Customer_Type__c == 'Partner' ){
            
            t.OwnerId = conMap.get(t.WhoId).OwnerId;
            }
    }
    }
    else if (stringId.substring(0,3) == '00Q'){
    Set<Id> leadsToQuery = new Set<Id>();
    for(Task t : Trigger.new){
        stringId = '' + t.WhoId;
            leadsToQuery.add(t.WhoId);
    }
    
    Map<Id,Lead> leadMap = new Map<Id,Lead>([select Id, OwnerId, Owner.Name, Company,mkto2__Inferred_State_Region__c,Approved_Employer_Rep__c, mkto2__Lead_Score__c,
                             mkto_si__Last_Interesting_Moment_Desc__c, mkto_si__Last_Interesting_Moment_Date__c,State from Lead where Id in : leadsToQuery]);
    
    for(Task t : Trigger.new){
        if(leadMap.containsKey(t.WhoId)){
            
            t.Account_Name__c=leadMap.get(t.WhoId).Company;
            t.Account_Billing_State__c=leadMap.get(t.WhoId).mkto2__Inferred_State_Region__c;
            t.FEA_Approved__c=leadMap.get(t.WhoId).Approved_Employer_Rep__c;
            t.Account_Owner__c=leadMap.get(t.WhoId).Owner.Name;
            t.Lead_Score__c=leadMap.get(t.WhoId).mkto2__Lead_Score__c;
            t.Last_Interesting_Moment_Date__c=leadMap.get(t.WhoId).mkto_si__Last_Interesting_Moment_Date__c;
            t.Last_Interesting_Moment_Desc__c=leadMap.get(t.WhoId).mkto_si__Last_Interesting_Moment_Desc__c;
        }
      IF(t.Account_Billing_State__c == '' ){
      t.Account_Billing_State__c=leadMap.get(t.WhoId).state;
      }
    }
}
}
MJ Kahn / OpFocusMJ Kahn / OpFocus

Sounds like you have a pre-existing unit test that used to pass, that creates some data, and then creates a Task related to that data. If the validation error happens before your unit test creates the Task, then the Task trigger will never have a chance to fire, so it'll have 0% coverage. You need to fix the validation error - that should allow your test to finish running, which should get you coverage on your trigger.

 

If you're getting a validation error in code that used to pass, it's most likely because someone added or modified a validation rule. Run the test in Production and look through the debug log - you should be able to see which validation rule is failing. To correct the problem, you'll need to either adjust the validation rule or adjust your code so that it conforms to whatever condition the validation rule requires.

nickboekanickboeka
thanks for your reply. I looked and I have no active validation rules on that same object (Task). I do have 3 inactive ones, but those shouldn't affect anything, right? If they do, I can probably just delete them, since they're not being used anyways.

Additionally, I cannot figure out how to test in production, since the Apex Test Execution tool does not allow me to select triggers to test, it seems like it's only classes.

MJ Kahn / OpFocusMJ Kahn / OpFocus

From what you wrote, the error is happening before your Task trigger fires (which is why your trigger has 0% coverage), so it's probably a validation rule on one of the objects your unit test creates before it creates the Task. Check those objects for new validation rules.

 

The Apex Test Execution page allows you to run unit tests. Those unit tests do work that causes trigger to fire. You need to figure out which unit test class(es) cause your Task trigger to fire, and run those.