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
CRNRCRNR 

Test Method Coverage

I have created a Trigger on our sandbox that works great when we upsert from the Data Loader:

 

trigger ProductTrigger on NRProducts__c (before update, before insert) {

for(NRProducts__c r : Trigger.new){

if(r.Name == null){

r.Name = 'NEWPRODUCT';

}

}

}

 

I have written the following test method:

 

public class ProductTrigger {

    static testMethod void myTest() {

 

     // Create that new product record.

     NRProducts__c r = new NRProducts__c(name='NEWPRODUCT');

     insert r;

     // Verify that the name field was set as "NEWPRODUCT".

     NRProducts__c insertNRProducts = [SELECT name FROM NRProducts__c WHERE Id = :r.Id];

     System.assertEquals('NEWPRODUCT', insertNRProducts.name);

}

}

 

It compiles, however when I ran test coverage it comes back as 66% with the following

 

trigger ProductTrigger on NRProducts__c (before update, before insert) {

for(NRProducts__c r : Trigger.new){

if(r.Name == null){

r.Name = 'NEWPRODUCT';

}

}

}

 

Any thoughts on how to make the test method complete?

Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber

You're not satisfying the conditional. Try this:

 

 

public class ProductTrigger {
    static testMethod void myTest() {
 
         // Create that new product record.
         NRProducts__c r = new NRProducts__c();
         insert r;

         // Verify that the name field was set as "NEWPRODUCT".
         NRProducts__c insertNRProducts = [SELECT name FROM NRProducts__c WHERE Id = :r.Id];
         System.assertEquals('NEWPRODUCT', insertNRProducts.name);
    }
}

 

 

All Answers

mtbclimbermtbclimber

You're not satisfying the conditional. Try this:

 

 

public class ProductTrigger {
    static testMethod void myTest() {
 
         // Create that new product record.
         NRProducts__c r = new NRProducts__c();
         insert r;

         // Verify that the name field was set as "NEWPRODUCT".
         NRProducts__c insertNRProducts = [SELECT name FROM NRProducts__c WHERE Id = :r.Id];
         System.assertEquals('NEWPRODUCT', insertNRProducts.name);
    }
}

 

 

This was selected as the best answer
CRNRCRNR

This was the solution! Thank you very much for your help! Much appreciated!