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
Jes@NZJes@NZ 

Test coverage help please, thanks!

Hi everyone

 

My first trigger in salesforce, is working in sandbox but I'm having a problem with the coverage.

 

I was already reading about it and couldn't find a solution, can anyone help?

 

I'm a new at salesforce so any help will be more than welcome.

 

The code is:

 

trigger AccountTrigger on Account (before insert) {
    
    List<Account> lst_in = trigger.new;
      for(Account acc:lst_in){
      
         
         acc.ShippingCity = acc.BillingCity;
         acc.PersonMailingCity = acc.BillingCity;
         acc.PersonOtherCity = acc.BillingCity;
    
         acc.ShippingStreet = acc.BillingStreet;
         acc.PersonMailingStreet = acc.BillingStreet;
         acc.PersonOtherStreet = acc.BillingStreet;
   
         acc.ShippingState = acc.BillingStreet;
         acc.PersonMailingState = acc.BillingState;
         acc.PersonOtherState = acc.BillingState;
       
         acc.ShippingPostalcode = acc.BillingPostalcode;
         acc.PersonMailingPostalcode = acc.BillingPostalcode;
         acc.PersonOtherPostalcode = acc.BillingPostalcode;
 
         acc.ShippingCountry = acc.BillingCountry;
         acc.PersonMailingCountry = acc.BillingCountry;
         acc.PersonOtherCountry = acc.BillingCountry;
    
   }
}

 

The errors are:

 

AccountTrigger

      Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required Deploy Error      

      Average test coverage across all Apex Classes and Triggers is 66%, at least 75% test coverage is required.

 

I can understand that I will have to tweak the code to have more than the minimum expected, but how?

 

Thank you

Best Answer chosen by Admin (Salesforce Developers) 
testrest97testrest97
trigger AccountTrigger on Account (before insert) {
    
    List<Account> lst_in = trigger.new;
      for(Account acc:lst_in){
      
         
         acc.ShippingCity = acc.BillingCity;
         acc.PersonMailingCity = acc.BillingCity;
         acc.PersonOtherCity = acc.BillingCity;
    
         acc.ShippingStreet = acc.BillingStreet;
         acc.PersonMailingStreet = acc.BillingStreet;
         acc.PersonOtherStreet = acc.BillingStreet;
   
         acc.ShippingState = acc.BillingStreet;
         acc.PersonMailingState = acc.BillingState;
         acc.PersonOtherState = acc.BillingState;
       
         acc.ShippingPostalcode = acc.BillingPostalcode;
         acc.PersonMailingPostalcode = acc.BillingPostalcode;
         acc.PersonOtherPostalcode = acc.BillingPostalcode;
 
         acc.ShippingCountry = acc.BillingCountry;
         acc.PersonMailingCountry = acc.BillingCountry;
         acc.PersonOtherCountry = acc.BillingCountry;
    
   }
}


the above trigger is the right way, the class and trigger you wrote is incorrect. What you are trying to do is update a record before its inserted. Just go with the abovce trigger and create a test class mentioned above

All Answers

testrest97testrest97

@isTest(SeeAllData=true)
private class TestAccountTrigger{
public static testMethod void TestAccountTrigger(){
Account a=new Account(name='Test Account');
insert a;
}
}

 

The above test class will help you to cover test code. You cannot deploy a trigger with 0% code coverage.

Jes@NZJes@NZ

Hi 

 

Thanks.

 

Sorry if I'm looking to be very basic (because I am), but can you give me a step by step indication.

 

Thank you.

Jes@NZJes@NZ
 

My class

public class AccountTrigger {
 public static void updateAccount(String a) {
  Account acc;
  acc = [SELECT  ShippingCity  FROM Account where Name = :a];
 
   //  shipping address
         acc.ShippingCity = acc.BillingCity ;
         acc.ShippingStreet = acc.BillingStreet;
         acc.ShippingState = acc.BillingState;
         acc.ShippingPostalcode = acc.BillingPostalcode;
         acc.ShippingCountry = acc.BillingCountry;
        
         // Person Mailing address
         acc.PersonMailingCity = acc.BillingCity ;
         acc.PersonMailingStreet = acc.BillingStreet;
         acc.PersonMailingState = acc.BillingState;
         acc.PersonMailingPostalcode = acc.BillingPostalcode;
         acc.PersonMailingCountry = acc.BillingCountry;
        
         //  PersonOther address
         acc.PersonOtherCity = acc.BillingCity ;
         acc.PersonOtherStreet = acc.BillingStreet;
         acc.PersonOtherState = acc.BillingState;
         acc.PersonOtherPostalcode = acc.BillingPostalcode;
         acc.PersonOtherCountry = acc.BillingCountry;
 
 
 
  update acc;
}
}

 

 

 

and the trigger

 

trigger AccountTrigger on Account (before insert) {
    List<Account> lst_in = trigger.new;
       for(Account acc:lst_in){
         AccountTrigger.updateAccount(acc.Name);
  }
     
}

 

It was looking good, at least for me... but, when cretaing a customer I get this error:

 

"Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTrigger: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject Class.AccountTrigger.updateAccount: line 8, column 1 Trigger.AccountTrigger: line 6, column 1: [] (System Code)"

 

Any help would be more than welcome.

 

Thank you

Satish_SFDCSatish_SFDC
You will have to create a new class with the code as mentioned by 'testrest97' above.
This is a test class and will help you achieve the code coverage.

Regards,
Satish Kumar
testrest97testrest97
trigger AccountTrigger on Account (before insert) {
    
    List<Account> lst_in = trigger.new;
      for(Account acc:lst_in){
      
         
         acc.ShippingCity = acc.BillingCity;
         acc.PersonMailingCity = acc.BillingCity;
         acc.PersonOtherCity = acc.BillingCity;
    
         acc.ShippingStreet = acc.BillingStreet;
         acc.PersonMailingStreet = acc.BillingStreet;
         acc.PersonOtherStreet = acc.BillingStreet;
   
         acc.ShippingState = acc.BillingStreet;
         acc.PersonMailingState = acc.BillingState;
         acc.PersonOtherState = acc.BillingState;
       
         acc.ShippingPostalcode = acc.BillingPostalcode;
         acc.PersonMailingPostalcode = acc.BillingPostalcode;
         acc.PersonOtherPostalcode = acc.BillingPostalcode;
 
         acc.ShippingCountry = acc.BillingCountry;
         acc.PersonMailingCountry = acc.BillingCountry;
         acc.PersonOtherCountry = acc.BillingCountry;
    
   }
}


the above trigger is the right way, the class and trigger you wrote is incorrect. What you are trying to do is update a record before its inserted. Just go with the abovce trigger and create a test class mentioned above
This was selected as the best answer
Jes@NZJes@NZ

Thank you it's solved.