You need to sign in to do that
Don't have an account?
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 :)
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
Hi,
Did you get to the bottom of this? I am facing the same issue, not sure what the problem is. Thanks!
J
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.
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
Can you please share the SELECT statement? I've just started with Apex and I need to access the ProductCode while in OpportunityLineItem.
Thanks!
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');
}
}
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.