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
selva kumar 14selva kumar 14 

Issue to Deploy into production

Hi Everyone,

I have a trigger that was properly ran in sandbox. When i try to deploy it into production getting deployment failed. The details shown below.

Apex Test Failures:
TestMultipleDLsTrigger validateMDLs System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, BulkPricebookOpp: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.BulkPricebookOpp: line 7, column 1: []
Stack Trace: Class.TestMultipleDLsTrigger.validateMDLs: line 5, column 1


Trigger :

trigger BulkPricebookOpp on Opportunity (before insert,before update)
{   
    Id pbID = null;
    Id recordtypId = null;   
    pbID = [Select Name, Description From Pricebook2 where Name =: 'Bulk Price Book'].Id;
    recordtypId =  [Select SobjectType, Name, Id, DeveloperName From RecordType  Where SobjectType = 'Opportunity' And  Name = 'k) Bulk Services'].Id;   

   for(Opportunity obj : trigger.new)
   {
        if(obj.RecordTypeId  == recordtypId)
        {
            obj.Pricebook2Id = pbID;
        }
        }
        }

Is it the reason that which i directly called "Bulk Price Book" name.

Is need to modify trigger in sandbox??

Please any one help on this.
SathyaTrekbin TechSathyaTrekbin Tech
Hi Selva,

Reason for the Error
You dont have a pricebook2 record named as BULK PRICE BOOK in Production org.

Solution can be implemented
Create a pricebook2 record in the test class. For example,
 
PriceBook2 objPB = new PriceBook2 (Name='Bulk Price Book',Description = 'test', isActive = true);
insert objPB;

Then you create a  PriceBookEntry record for the above PriceBook2, For Example
Product2 objProduct2 = new Product2(Name = 'Test Product', isActive = true);
insert objProduct2;

PricebookEntry objPBE1 = new PricebookEntry(Pricebook2Id = objPB.Id, Product2Id = objProduct2.Id, UnitPrice = 2,   UseStandardPrice = false, isActive = true);
insert objPBE1;


 
SathyaTrekbin TechSathyaTrekbin Tech
Hi Selva,

Do above code changes in sandbox and then try to validate against production org.
Hopefully, this will help you. 
selva kumar 14selva kumar 14
Hi  SathyaTrekbin,

Thanks for your help.

When i try to execute the code in sandbox am getting the follwoing error.

System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []

My Test class code is given below.

@isTest
public class TestBulkPB {
     Public static testmethod void validatepric(){
         PriceBook2 objPB = new PriceBook2 (Name='Bulk Price Book',Description = 'Bulk Pricing (non-standard)', isActive = true);
         insert objPB;
         
         Product2 objProduct2 = new Product2(Name = 'Pro 75 Bundle', isActive = true);
         insert objProduct2;

         PricebookEntry objPBE1 = new PricebookEntry(Pricebook2Id = objPB.Id, Product2Id = objProduct2.Id, UnitPrice = 189.99,   UseStandardPrice = false, isActive = true);
         insert objPBE1;

}
}

Please solve this issue. Thanks in advance.
SathyaTrekbin TechSathyaTrekbin Tech
Hi Selva,

Try with adding 
 
@isTest(SeeAllData=true)
public class TestBulkPB 
{

}
Thanks,
selva kumar 14selva kumar 14
Hi  Sathya,

Even though am getting the same error. As u said i added my code.

@isTest(SeeAllData=true)
public class TestBulkPB
{
Public static testmethod void TestBulkPB(){
         PriceBook2 objPB = new PriceBook2 (Name='Bulk Price Book',Description = 'Bulk Pricing (non-standard)', isActive = true);
         insert objPB;
         
         Product2 objProduct2 = new Product2(Name = 'Pro 75 Bundle', isActive = true);
         insert objProduct2;

         PricebookEntry objPBE1 = new PricebookEntry(Pricebook2Id = objPB.Id, Product2Id = objProduct2.Id, UnitPrice = 189.99,   UseStandardPrice = false, isActive = true);
         insert objPBE1;

}
}


Is we need to assign as standard pricebook. Since mentioned priebook not related to standard.
SathyaTrekbin TechSathyaTrekbin Tech
Hi Selva,

You can use Test.getStandardPricebookId() method to get the standard pricebook Id in test class. In this way, you don't need to perform any soql or write seealldata=true.