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
Bard09Bard09 

OpportunityLineItem trigger questions

I'm doing my first major Apex scripting job to automatically create events based on a quantity # specified in Opportunity Products.  I'm not the best coder around, so please bear with me here :)

 

I'm trying to query PricebookEntry.Product2.Name but am finding that the output via Apex is the name of the field instead of the actual name specified for the related object.  This also happens with ProductCode as well.

 

For example:

 

PricebookEntry.Product.Name                             .....results in "Name"

Trigger.new[0].PricebookEntry.Product2.Name    .....results in "null"

 

I've been able to narrow down a SOQL query with other fields in the table, so I know it is possible.  Here's an example:

 

[select Id,OpportunityId,Quantity,PricebookEntry.Product2.Family from OpportunityLineItem where PricebookEntry.Product2.Family = 'Services' AND Opportunity.Id = :eachNewOpportunityLineItem.OpportunityId]

 

 

What am I missing?  Someone fill in the obvious for me :)

Best Answer chosen by Admin (Salesforce Developers) 
dev_jhdev_jh

AsGot to the bottom of this.As it transpires, in a trigger you can´t access related records without issuing a select statement. Thanks,

 

J

All Answers

dev_jhdev_jh

Hi,

 

Did you get to the bottom of this? I am facing the same issue, not sure what the problem is. Thanks!

 

J

Bard09Bard09

I was able to get it to work, yes, but unfortunately my code has changed enough that I don't remember what the specific fix was.  Sorry :(  It might have been that I was trying to call PriceBookEntry.Product2.Name outside of my OpportunityLineItem list loop, but I don't recall.

 

Feel free to send me a PM if you're working on a similar project, though-- I just finished mine and am happy to share if you have questions.

dev_jhdev_jh

AsGot to the bottom of this.As it transpires, in a trigger you can´t access related records without issuing a select statement. Thanks,

 

J

This was selected as the best answer
Bard09Bard09
Yep, that was it.  I remember having to consolidate my related list items into the main SOQL query.  Thanks for confirming, dev_jh :)
RajSharanRajSharan

Can you please share the SELECT statement? I've just started with Apex and I need to access the ProductCode while in OpportunityLineItem.

 

for (OpportunityLineItem updatedLineItem : System.Trigger.new)    
    {   
   
System.debug(PricebookEntry.Product2.ProductCode);   }
 

 

Thanks!

Message Edited by RajSharan on 12-17-2009 02:40 PM
Message Edited by RajSharan on 12-17-2009 02:41 PM
RajSharanRajSharan

Here's the code:

 

trigger AddProductSchedules on OpportunityLineItem (after insert){//, after update

 

Set<Id> OrderLineIds= new Set<Id>();               
    Set<Id> LineItemIds= new Set<Id>();
   
    Set<String> PBEIdSet = new Set<String>();
    Set<String> Prod2IdSet = new Set<String>();
      
    for (OpportunityLineItem updatedLineItem : System.Trigger.new)   
    {  
System.debug('Line 33');
        System.debug(updatedLineItem.Opportunity.name);
System.debug('Line 35');
       

       
System.debug('Line 55');
        if (updatedLineItem.PricebookEntryId  != null) {
System.debug(updatedLineItem.PricebookEntryId );
            PBEIdSet.add(updatedLineItem.PricebookEntryId );
        }
        List<PricebookEntry> ParentPricebookEntry = [SELECT Product2Id          
                                            from PricebookEntry                           
                                            where id in :PBEIdSet];
        for(PricebookEntry pbes: ParentPricebookEntry) {           
            if (pbes.Product2Id != null) {
                System.debug(pbes.Product2Id);
                Prod2IdSet.add(pbes.Product2Id);  
            }
            List<Product2> ParentProd = [SELECT ProductCode
                                                   from Product2
                                                   where id in :Prod2IdSet];                                           
            for(Product2 prods: ParentProd) {
                System.debug(prods.ProductCode);
                if(prods.ProductCode == 'TCV') {
                   bTVC = true;                  
                }
                System.debug(bTVC);
            }
System.debug('77');
        }
                   

}                   

shrishri

HI ,

I am trying to add the new custom field in product section on opportunity tab, It should track the each product status,

On opportunity Tab, we have product section whcih is product related list . I am trying to add new custom field to track status of the each product.

But I am not able to add directly custom field s in related list of product on opportunity tab..

 

Any one have any suggestion to track each product status on opty level.

 

Thanks In advance.