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
Kon DeleKon Dele 

Test Coverage only 50%

Trying to get at least 75% coverage on this trigger. Here's what i have so far

My trigger:

trigger UpdateOpptyPicklists on Opportunity(Before Insert, Before Update) {
    for(Opportunity o: Trigger.new){
     Account a = [select id,LC_Region__c,LC_Area__c, LC_Division__c from Account where Id=:o.AccountId];
       
        if(o.LC_Region__c == NULL || o.LC_Area__c == NULL || o.LC_Division__c == NULL){
         
          o.LC_Region__c=a.LC_Region__c;
          o.LC_Area__c = a.LC_Area__c;
          o.LC_Division__c=a.LC_Division__c;
       }
  }
 }
  
The test class:

@isTest
private class UnitTests_UpdateOpptyPicklists {

    static testMethod void myUnitTest() {
      Account a = new Account();
      a.Name='TEST ACCOUNT';
      a.Type='Competitor';
      a.LC_Region__c='Americas';
      a.LC_Area__c='West';
      a.LC_Division__c='MA';
      insert a;
     
      Opportunity o=new Opportunity();
      o.Name='TEST';
      o.AccountId=a.Id;
      o.Type='New Customer';
      o.closeDate=date.today();
      o.StageName='Prospecting';
      o.Amount=10000;
      o.LC_Region__c='Americas';
      o.LC_Area__c='East';
      o.LC_Division__c='SE';
      insert o;
     
      RecordType rt=new RecordType();
      try{
       rt=[SELECT Id from RecordType where sObjectType='Opportunity' and isActive=true limit 1];
      }
      catch(Exception e){
       rt=null;
      } 

      if(o.LC_Region__c == NULL || o.LC_Area__c == NULL || o.LC_Division__c == NULL){
      o.LC_Region__c=a.LC_Region__c;
      o.LC_Area__c=a.LC_Region__c;
      o.LC_Division__c=a.LC_Region__c;
      update o;
      }
}    
}

Could someone enlighten me why I'm I getting only 50% coverage
Best Answer chosen by Kon Dele
Vinit_KumarVinit_Kumar
1.) First thing,you are running your SOQL inside for loop which is against the best practices of salesforce.You should move the SOQL outside it.

2.) As why 50% code is covered,the reason is that you are not satisfying your if condition.Try below code :
@isTest
private class UnitTests_UpdateOpptyPicklists {

    static testMethod void myUnitTest() {
      Account a = new Account();
      a.Name='TEST ACCOUNT';
      a.Type='Competitor';
      a.LC_Region__c='Americas';
      a.LC_Area__c='West';
      a.LC_Division__c='MA';
      insert a;
     
      Opportunity o=new Opportunity();
      o.Name='TEST';
      o.AccountId=a.Id;
      o.Type='New Customer';
      o.closeDate=date.today();
      o.StageName='Prospecting';
      o.Amount=10000;
      o.LC_Region__c='Americas';
      o.LC_Division__c='SE';
      insert o;
	  o.LC_Division__c=null;
	  update o
     }
	 }
If this helps,please mark it as best answer to help others :)

All Answers

Vinit_KumarVinit_Kumar
1.) First thing,you are running your SOQL inside for loop which is against the best practices of salesforce.You should move the SOQL outside it.

2.) As why 50% code is covered,the reason is that you are not satisfying your if condition.Try below code :
@isTest
private class UnitTests_UpdateOpptyPicklists {

    static testMethod void myUnitTest() {
      Account a = new Account();
      a.Name='TEST ACCOUNT';
      a.Type='Competitor';
      a.LC_Region__c='Americas';
      a.LC_Area__c='West';
      a.LC_Division__c='MA';
      insert a;
     
      Opportunity o=new Opportunity();
      o.Name='TEST';
      o.AccountId=a.Id;
      o.Type='New Customer';
      o.closeDate=date.today();
      o.StageName='Prospecting';
      o.Amount=10000;
      o.LC_Region__c='Americas';
      o.LC_Division__c='SE';
      insert o;
	  o.LC_Division__c=null;
	  update o
     }
	 }
If this helps,please mark it as best answer to help others :)
This was selected as the best answer
Kon DeleKon Dele
Thank you so much Vinit! I moved the SOQL query outside the FOR loop and the test coverage is 100%!!!

Thanks Again!