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
BobBob 

Apex Trigger Test Lines Not Covered

I have a trigger with a test method that has lines of code not covered. I was wondering if anyone could help fill in the blanks. Lines, 16,18,20,21,23,24 are not covered. I am also using the test for another trigger, but wanted to add coverage into the test for my new trigger too.


New Trigger

trigger Trigger_PreferredSP on Case (before update) {

List<String> accountList = new List<String>();
    for (Case c : Trigger.new)
        accountList.add(c.AccountId);
  
    // Query Accounts
    Map<Id, Account> accountMap = new Map<id, Account>([SELECT OwnerId,Preferred_SP__c,Shipping_Country__c FROM Account WHERE id =: accountList]);
  
    // Set Service Provider
    for (Case c : Trigger.new){
        Account a = accountMap.get(c.AccountId);
       
        if(a != null){ //Checks to see if the case has an account.
       
        if(a.Shipping_Country__c != 'USA')
       
        if(a.Shipping_Country__c != 'CAN')
       
            if ( c.Service_Provider__c == null)
                c.Service_Provider__c = a.Preferred_SP__c ;
              
           else if(c.AccountId == null & a.Preferred_SP__c == null )
             c.Service_Provider__c = null ;
        }


TEST METHOD

@isTest
private class Test_AssignWebCaseQueue {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
       
        test.startTest();
       
        Account acc = new Account( Name ='Key West Services', Market_Segment__c='Finance Company',Shipping_Country__c= 'USA', Customer_Type__c='Finance Company', Preferred_SP__c ='0017000000WitqP', Registered_for_Seminar__c=true );
      insert acc;
     
      acc.Name='Key West Services';
      acc.Market_Segment__c ='Finance Company';
      acc.Customer_Type__c='Finance Company';
      acc.Preferred_SP__c ='0017000000WitqP';
      acc.Shipping_Country__c= 'USA';
      acc.Shipping_Country__c= 'CAN';
     
      update acc;
     
     

      Case cse = new Case( Status='Open', Origin='Parts Support - Web', Subject='Parts', Web_Case_Type__c='Part Support',Service_Provider__c='0017000000WitqP');
      insert cse;

      cse.Status = 'Open';
      cse.Origin = 'Parts Support - Web';
      cse.OwnerId='00G70000001JGIA';
      cse.Web_Case_Type__c='Part Support';
      cse.Service_Provider__c = '0017000000WitqP';
      update cse;
     
      cse.Status = 'Open';
      cse.Origin = 'Tech Support- Web';
      cse.OwnerId='00G70000001hJGH';
      cse.Web_Case_Type__c='Tech Support';
      update cse;

     

    test.stopTest();
    }
}
MaddySinghMaddySingh
Please check your trigger code at these lines

if(a.Shipping_Country__c != 'USA')
      
if(a.Shipping_Country__c != 'CAN')

You are not doing anything yet you have these conditions. Either they are unneccessary or you forgot the processing blocks if the conditons are met.
BobBob
I was trying to check the Shipping_Country__c for USA AND CAN, if that field has those two values then do not add the a.Preferred_SP__c. I couldn’t figure out how to do a AND operator without receiving an error. My trigger actually does work with what I have but if there is a better way to write that code I would appreciate any help you can give me. Robert A. Poliquin Salesforce.com Administrator CYBEX 10 Trotter Drive Medway, MA 02053 USA t 774-324-8181 f 508.533.5500 e bpoliquin@cybexintl.com w www.cybexintl.com [sf_cert_adm_rgb] Join CYBEX on Facebook & Twitter! Check out our Blog