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
wilbur07wilbur07 

Newbie needs some help and clarification!

I'm having trouble deploying to a regular organization (not developer or sandbox).
 
Here is the apex code:
 
trigger AutoPopulatePricebookEntry on Product2 (after insert) {
 
 sObject s = [select ID from Pricebook2 where IsStandard = TRUE];
 
 for (Product2 newProduct: Trigger.new) {
  
  PricebookEntry z = new PricebookEntry(Pricebook2Id=s.ID, Product2Id=newProduct.ID, UnitPrice=0.00, IsActive=TRUE, UseStandardPrice=FALSE);
  insert z;
  
 }
}
 
Here is my testcode that I ran all tests on and succeeded:
 
public class myClass {
  static testMethod void testInsertLine() {
   Product2 p = new product2(name='x');
   
   try{
    insert p;
                }
          catch (DmlException e) {
            System.debug(e.getMessage());
                }
   
   System.assertEquals(0.00, [select UnitPrice from PricebookEntry
                               where product2id = :p.id].UnitPrice);
  }
 }
 
When I try to deploy the Eclipse message is that all 4 lines of my code are uncovered.
 
HEEEELLLLLPPPPP!!!!!  Several hours of frustration lead me to beg someone's knowledge and wisdom.  Perhaps some sample code showing me how to cover each line in my trigger?  Thanks in advance!
 
James
wilbur07wilbur07

Well I tried the test method on developer network and the test was a success.

I tried the test method on eclipse IDE and it says all lines of code are tested with 100% coverage.

I go to deploy the code and it gives me an error in my method:

Run Failures:
  myClass.testInsertLine System.QueryException: List has no rows for assignment to SObject [Line 14]
  AutoPopulatePricebookEntry Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
  Test coverage of selected Apex Class and Trigger is 66%, at least 75% test coverage is required

The code is as above and line 14 selects the new pricebookentry on id of the product created and inserted.  Does this mean my trigger code is bad?  But it works in development and the testmethod passes in development, both on salesforce app and eclipse IDE.  It's only when I try to deploy does the error message come up.  Can somebody please help me here?

James

JonPJonP
The key to diagnosing what happened is the following error message:

myClass.testInsertLine System.QueryException: List has no rows for assignment to SObject [Line 14]

This means the query on line 14 of myClass::testInsertLine() returned no results.

Your TestMethod is not guaranteed to be transportable because it relies on certain data already being in the organization, when it executes the query: "select ID from Pricebook2 where IsStandard = TRUE".  If your development organization has a matching pricebook but your deployment target organization does not, the test will work in the former and fail in the latter--exactly as you've seen.

In general, it's preferable to create your entire test data set within your tests, so you don't have to worry about what is already in the database.  This may or may not be appropriate to your particular situation, but the only alternative is to create all the requisite data manually, outside your code deployment process.