I am working on a trigger on a custom object that when a field IncludeWithOrder__c is marked as true a after update trigger is fired to add that product to the Quote as a QuoteLineItem, but I am currently getting saying that the Product2 field doesn't exist on the QuoteLineItem object, I have tried Product2Id and Product2 but neither seem to work, any help would be awesome. here is the trigger that I have currently:

 trigger WearableTrigger on Wearable__c (after update) {

    List<QuoteLineItem> item = new List<QuoteLineItem>();
        for (Wearable__c wear: Trigger.New)
            if (wear.IncludeWithOrder__c == TRUE){
                item.add(new QuoteLineItem(
                    //ProductId = wear.Product__r.Id,
                    QuoteId = wear.Quote__r.Id,
                    UnitPrice = wear.Price__c,
                    Quantity = wear.Quantity__c));
        insert item;

1 Answer 1


Since you want the Id's for Product and Quote, you actually don't need to use the __r.Id versions Just replace __r.Id with __c and it will work...

Furthermore, if you want any non-ID relationship fields, those are not included in the Trigger.new context... You'll need to do as follows:

for (Wearable__c wear: [SELECT Id, Product__c, Quote__c, Price__c, Quantity__c, IncludeWithOrder__c, Product__r.SomeField__c FROM Wearable__c WHERE Id IN :Trigger.New])

Sounds like you also need the pricebookentryid, so create a map before your "for" loop:

Map<Id, Id> mapProductToPBE = new Map<Id, Id>();
for (Product2 p : [SELECT Id, Product2Id FROM PricebookEntry WHERE Pricebook2Id = :someId]) {
    mapProductToPBE.put(p.Product2Id, p.Id);

... then reference that in your loop by replacing //ProductId = wear.Product__r.Id with PricebookEntryId = mapProductToPBE.get(wear.Product__c)

  • After doing what you had suggested, I still get the compile error of "Invalid field Product2 for SObject QuoteLineItem (Line 7), while that is the API name of the field on the QuoteLineItem object. May 16, 2014 at 22:50
  • What is the lookup object for Wearable__c.Product__c? It sounds like they are incompatible... May 16, 2014 at 22:51
  • It is a look up to the Product standard object, same object that the QuoteLineItem look ups to. May 16, 2014 at 22:52
  • I suggest creating a map that maps product id to pricebookentry id that you want... May 16, 2014 at 22:54
  • Ah that is a good point/idea, I will put that to use! thank you! May 16, 2014 at 22:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .