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
edward scott 10edward 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?
 
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
Best Answer chosen by edward scott 10
PabitraPabitra
Hi  edward scott 10,

The same code is working for me without any error.Please can you share the screenshot of your error.
 
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)
            
            try{
              update oppsToUpdate;
            }
            catch(Exception e){
             System.debug(e);
            }
        }
Thank you.
 

All Answers

PabitraPabitra
Hi  edward scott 10,

The same code is working for me without any error.Please can you share the screenshot of your error.
 
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)
            
            try{
              update oppsToUpdate;
            }
            catch(Exception e){
             System.debug(e);
            }
        }
Thank you.
 
This was selected as the best answer
edward scott 10edward scott 10
User-added image

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.
 
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, Product.Name 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.Product.Name;
                }//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 again.
edward scott 10edward scott 10
Error: Compile Error:
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.
PabitraPabitra
Dear Edward Scott 10,

Please replace Product.Name with Product2.Name in the SOQL query.

Thanks!
Pabitra
edward scott 10edward scott 10
Thanks, Pabirta the code did compile that time but my text product field on the opportunity is still blank. Since it is a lookup field do I need to do anything different to get it to convert to text?
PabitraPabitra
Dear Edward Scott 10,

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
edward scott 10edward scott 10
Thanks for your help pabitra. Can I ask you one more question. if i wanted to keep a record of this field meaning that for every product that gets added i add the product name: next product name how would I do that so all products would live in that field?
PabitraPabitra
Dear, Edward,
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
if(oppUpdate.Product__c!=null && oppUpdate.Product__c!=''){
  oppUpdate.Product__c = oppUpdate.Product__c+';'+Item;
}
else{
  oppUpdate.Product__c=team;
}
Kindly check this code and let me know if you are not meeting your requirements.

Many Thanks!
Pabitra
 
edward scott 10edward scott 10
Hey 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