1

Here is my Apex Trigger

trigger ProductNameTrigger on QuoteLineItem (before insert) {
    list<QuoteLineItem> plist = new list<QuoteLineItem>();
    for(quotelineitem q : [select id, Product2.name, product2id, Product2_Name__c from QuoteLineItem where id =:trigger.new]){
        q.Product2_Name__c = q.Product2.name;
        plist.add(q);
    }
    update plist;
}

and here is my test class

private static testMethod void testTrig(){
    Opportunity o = new Opportunity(name='testOp', StageName='Closed', CloseDate=date.today());
    insert o;
    Product2 prodTest = new Product2(Name = 'testProduct', StandardPrice=20);
    insert prodTest;
    Quote quoteTest = new Quote(OpportunityId = o.id);
    quoteTest.name = 'testQuote';
    insert quoteTest;
    PriceBook2 pbook = new PriceBook2(name='testBook');
    insert pbook;
    PriceBookEntry entry = new PriceBookEntry( Product2Id=prodTese.id, UnitPrice=0, PriceBook2Id=pbook.id, UseStandardPrice=true);
    insert entry;

    QuoteLineItem q = new QuoteLineItem(Product2Id = prodTest.id, PriceBookEntryId=entry.id, Quantity=0, UnitPrice=entry.UnitPrice);
    insert q;

    System.assertEquals(prodTest.name, q.Product2_Name__c);
}

and I am getting an Error that states

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

Any Idea how to fix this?

1

The reason for this error is that when using custom pricebooks (pBook), before you can add a PBE (PricebookEntry) for that product | pricebook combination, you must first insert a PBE for the same product | standard Pricebook combination

The standard pricebook's id can be found from Test.getStandardPricebookId(). You do not create a standard pricebook in the testmethod

You'll need something like:

PriceBookEntry[] entryList = new List<PriceBookEntry> {
    new PricebookEntry( Product2Id=prodTese.id, UnitPrice=0,
                        PriceBook2Id=Test.getStandardPricebookId(), // **
                        UseStandardPrice=false,isActive=true),
    new PricebookEntry( Product2Id=prodTese.id, UnitPrice=0, 
                        PriceBook2Id=pbook.id, 
                        UseStandardPrice=true,isActive=true)
    };
insert entryList;
  • Thank you so much! haven't checked to see if this has worked yet I'll get back to you – Mike Stomp Jun 26 '15 at 14:15
  • The test class works now, but the PriceBook2_Name__c is null instead of 'testProd' so i assume something is incorrect with my trigger – Mike Stomp Jun 26 '15 at 14:35
  • Cool; there's not enough posted to diagnose pricebook2_name__c issue; hopefully, you'll figure this out but you can file a new question if still stuck – cropredy Jun 26 '15 at 18:35
0

Few issues to mention

Your trigger is for before insert of QuoteLineItem records. So you can't really query out QuoteLintItems of Trigger.new in line

for(quotelineitem q : [select id, Product2.name, product2id, Product2_Name__c from QuoteLineItem where id =:trigger.new])

because records in Trigger.new don't exist in the database yet.

And in a before insert trigger, you don't have to specifically update the record, you just have to code the changes you want to happen, and they will be reflected in the inserted records.

So if all you want to do in the trigger is to change the records' Product2_Name__c field, your trigger should change as

trigger ProductNameTrigger on QuoteLineItem (before insert) {
    List<Id> product2Ids = new List<Id>();
    for(quotelineitem q : Trigger.new){
        product2Ids.add(q.Product2Id);
    }
    Map<Id, Product2> product2Map = new Map<Id, Product2>([SELECT Id, Name FROM Product2 WHERE Id IN :product2Ids]);

    for(quotelineitem q : Trigger.new) {
         if(product2Map.containsKey(q.Product2Id)) {
             q.Product2_Name__c = product2Map.get(q.Product2Id).Name;
         }
    }
}

So back to your error, check if there is StandardPrice is a valid field name for Product2 record.

  • Thank you for the help with the Trigger! also there is no Standard Price field for the Product2 object – Mike Stomp Jun 25 '15 at 18:02
  • That's why you are getting the error. So all your issues are solved now? – navD87 Jun 25 '15 at 18:03
  • No i took it out but I am still getting the error – Mike Stomp Jun 25 '15 at 18:07
  • Heres the full error if it helps 'Class.testProdNameTrigger.testTrig: line 14, column 1 13:07:08.525 (4525387670)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []' – Mike Stomp Jun 25 '15 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.