+ Start a Discussion
Alex MerwinAlex Merwin 

Populate custom field on OpportunityLineItem with an array of Custom Field values on the same object

Hi everyone - 

I'm trying to modify some APEX used for a different purpose for this, and I think I'm getting off track. 

There is a custom text field called SpX_Product_ID__c on the OpportunityLineItem object. I need to populate a new field, Export_All_Inventory_Pkg_Reference__c , with all SpX_Product_ID__c values for OLI associated with the same Opportunity. 

I will then use the Export_All_Inventory_Pkg_Reference__c to populate a Custom Link on the OLI Detail Page so users can view a report of all Products associated with this Opportunity with one click. 

Thanks for your help!
trigger ExportPkgRef on OpportunityLineItem (after update, after insert) {
    Set<Id> oppIds = new Set<Id>();

    for (OpportunityLineItem opp : Trigger.new) {
        OpportunityLineItem oldOpp = Trigger.oldMap.get(opp.Id);
        if (
            opp.Opportunity.ID = opp.Opportunity.ID &&
            oldOpp.Opportunity.ID != opp.Opportunity.ID
        ) {

    if (!oppIds.isEmpty()) {
        List<OpportunityLineItem> oliList = [
            select SpX_Product_ID__c
            from OpportunityLineItem
            where OpportunityLineItemId in :oppIds

        for (OpportunityLineItem oli : oliList) {
            oli.SpX_Product_ID__c = SpX_Product_ID__c;

        update oliList;

Sagar PareekSagar Pareek
Consider both SpX_Product_ID__c and Export_All_Inventory_Pkg_Reference__c are on same object (OpportunityLineItem ) and you want to copy the value of SpX_Product_ID__c  into Export_All_Inventory_Pkg_Reference__c. Here is the code to achieve it -
trigger ExportPkgRef on OpportunityLineItem (after update, after insert) {
    List<OpportunityLineItem>oliList = new List<OpportunityLineItem>();

    for (OpportunityLineItem oli : Trigger.new) {
        oli.Export_All_Inventory_Pkg_Reference__c = oli.SpX_Product_ID__c;


        update oliList;
Alex MerwinAlex Merwin
Thanks for your help, Sagar. The code deployed fine, but I'm getting this error when triggering it on the Opportunity Product detail page: 

Error:Apex trigger ExportPkgRef caused an unexpected exception, contact your administrator: ExportPkgRef: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.ExportPkgRef: line 5, column 1

I should note that SpX_Product_ID__c is a read-only formula field that returns text. 

Export_All_Inventory_Pkg_Reference__c is not read only, and is a text field, up to 1500 characters. 

If I'm interpreting your code correctly, it looks like it will set Export_All_Inventory_Pkg_Reference__c to equal SpX_Product_ID__c? I'm actually trying to get Export_All_Inventory_Pkg_Reference__c to contain multiple SpX_Product_ID__c values, corresponding to all Products under the opportunity. 

The end goal is to pass this field as a Custom Link to a report that users can use to export all Products associated with an opportunity. 

For example, say Opportunity X has 5 products associated with it, with SpX_Product_ID__c values 000001, 000002, 000003, 000004 & 000005. I'm hoping that the Export_All_Inventory_Pkg_Reference__c field on each of the Opportunity Product objects equals: 000001,000002,000003,000004,000005