+ Start a Discussion
elossoelosso 

Get Opportunity Name from OpportunityLineItem

I'm writing a trigger and need to get the Opportunity Name from from OpportunityLineItem. Any help would be appreciated!

Best Answer chosen by Admin (Salesforce Developers) 
ForcepowerForcepower

elosso,

 

ok - that snapshot helps - you have a lookup to Opportunity which expects an opportunity id rather than the name of the Opportunity.

 

so change your assignment to Opportunity_Name__c like so:

 

        if (impr != null) impr.Opportunity_Name__c = oppLineItem.OpportunityId;

All Answers

ForcepowerForcepower
elosso,

It should be available as Opportunity.Name. I don't know offhand whether you'll get that as part of the trigger record. But, if not, you can query it this way:

Select o.Opportunity.Name, o.OpportunityId From OpportunityLineItem o
where Id in :oppLineItemList

best,
Ram
elossoelosso

Ram,

could you look at the following link? It's more detailed explanation of what I'm experiencing. 

 

Link to additional Force.com post

 

Thank you so much for all of your help!!

ForcepowerForcepower
elosso,
Try this:

trigger createNewImpression on OpportunityLineItem (after insert) { List <Impressions__c> impToInsert = new List <Impressions__c> (); List <Id> oppLineItemIds = new List<Id>();
Map <Id, Impressions__c> oppLineItemIdToImpressionsMap = new Map <Id, Impressions__c>(); for (OpportunityLineItem o : Trigger.new) { if (o.Product_Family__c == 'Impression') { oppLineItemIds.add(o.Id);
Impressions__c i = new Impressions__c (); //i.Opportunity_Name__c = o.Opportunity.Name; i.Bill_Date__c = o.ServiceDate; i.Expected_Impressions__c = o.Quantity; i.Predicted_Revenue__c = o.TotalPrice; oppLineItemIdToImpressionsMap.put(o.Id, i); impToInsert.add(i); } } for (OpportunityLineItem oppLineItem : [Select Opportunity.Name, OpportunityId, Id From OpportunityLineItem where Id in :oppLineItemIds]) {
Impressions__c impr = oppLineItemIdToImpressionsMap.get(oppLineItem.Id);
if (impr != null) impr.Opportunity_Name__c = oppLineItem.Opportunity.Name;
} insert impToInsert; }
 
elossoelosso

Ram,

 

First of all, thank you so much for helping me out. I cannot stress how grateful I am. Unfortunately the code you provided came back with an error:

 

Apex script unhandled trigger exception by user/organization: 00550/00D5000
 
NewImpression: execution of AfterInsert
 
caused by: System.StringException: Invalid id: This is an Impression
 
Trigger.NewImpression: line 24, column 1

 

Note "This is an Impression" is the name of the Opportunity which is exactly what I want the field to update to; however, instead of updating it's coming back with an error. Here is a photo of my Impressions Object fields. Is it possible I'm messing up the relationship. IE: should it be lookup or master-detail?

 

Link to Image

 

Thank you again for all of your help. Please let me know if you have any thoughts.

 

 

ForcepowerForcepower

elosso,

 

ok - that snapshot helps - you have a lookup to Opportunity which expects an opportunity id rather than the name of the Opportunity.

 

so change your assignment to Opportunity_Name__c like so:

 

        if (impr != null) impr.Opportunity_Name__c = oppLineItem.OpportunityId;
This was selected as the best answer
elossoelosso

Ram,

 

Words do not express my gratidude! I'm a so so thankful for all of you help! You are brilliant :) Many thanks!

ForcepowerForcepower
You're very welcome, elosso.
best!
Ram