You need to sign in to do that
Don't have an account?
edward scott 10
how to write info from the opportunity line item to the opportunity
Hi all,
I am trying to write information from the opportunitylineitem to the opportunity. On the LineItem there is a field called Product which is a standard lookup field to the product object. I am trying to get that name to go from the LineItem to the Opportunity to a custom field called product. When I was trying to reference the Standard lookup product I kept getting an error message saying it wasn't a field so changed it to the description to see if i could get the description to go from the LineItem to the Opportunity. The trigger did save but it didn't work so I wanted to post my code here to see if anyone could help.
So my first question is how do I reference a standard lookup field in apex and the second is this the best way to get information from the LineItem to the opportunity?
Thanks for your help,
Ed
I am trying to write information from the opportunitylineitem to the opportunity. On the LineItem there is a field called Product which is a standard lookup field to the product object. I am trying to get that name to go from the LineItem to the Opportunity to a custom field called product. When I was trying to reference the Standard lookup product I kept getting an error message saying it wasn't a field so changed it to the description to see if i could get the description to go from the LineItem to the Opportunity. The trigger did save but it didn't work so I wanted to post my code here to see if anyone could help.
So my first question is how do I reference a standard lookup field in apex and the second is this the best way to get information from the LineItem to the opportunity?
trigger OpportunityProductTrigger on OpportunityLineItem (after update) { List<Id> oppIds = new List<Id>(); String Item; if(trigger.isAfter) { for (OpportunityLineItem oli: trigger.new){ oppIds.add(oli.opportunityID); } List<OpportunityLineItem> allOLI = [SELECT id, ProductCode FROM OpportunityLineItem WHERE OpportunityId in: oppids]; List<Opportunity> oppsToUpdate = [SELECT id, Product__c FROM Opportunity WHERE id in: oppids]; if(allOLI.size() > 0){ for(OpportunityLineItem allOLI2: allOLI){ Item = allOLI2.ProductCode; }//END if(allOLI2.Number_of_Months__c > contractLengthMonths) } //END for(OpportunityLineItem allOLI2: allOLI) for(Opportunity oppUpdate: oppsToUpdate){ oppUpdate.Product__c = Item; }// END for(Opportunity oppUpdate: oppsToUpdate) } }
Thanks for your help,
Ed
The same code is working for me without any error.Please can you share the screenshot of your error.
Thank you.
All Answers
The same code is working for me without any error.Please can you share the screenshot of your error.
Thank you.
I inserted the error above. This was when I was trying to reference the lookup field Product on the lint item. I will paste that code below.
Thanks again.
SELECT id, Product.Name FROM OpportunityLineItem
^
ERROR at Row:1:Column:12
Didn't understand relationship 'Product' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 10 column 44
sorry this is the error code above that picture is too small to read above.
Please replace Product.Name with Product2.Name in the SOQL query.
Thanks!
Pabitra
In your above code I didn't get update statement.If you didn't placed update statement please go through my above code how I am updating the list in last.If you still facing issue please let me know.
Thanks!
Pabitra
It's my pleasure to answer to you,
First of all it will be good to change the Opportunity Product__c field type text field to text area or long text area.So that large number of characters you can store in this.In addition of all product name in this field I am using ';' to separate all product name here.
Please replace 18th line in your above code with my below code Kindly check this code and let me know if you are not meeting your requirements.
Many Thanks!
Pabitra
Thanks again for all of your help. I have one more question as well. I turned the Poduct__c field that is receiving the information from the opportunity product into a rich area text field. I was wondering how would I update the code to create a line break every time a new product is added so that every product will be on it's own line?
Thanks,
Ed