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
LaurenP6777LaurenP6777 

Trigger.Old compared to Trigger.new Help

I have been working with this trigger for some time and just can't seem to get it to stop adding mutliple records so I am resorting to the boards!

 

I am just trying to create a simple trigger on OpportunityLineItem that creates a custom object record when certain fields are updated. I am sure that I am using Trigger.old & Trigger.new in the wrong way. When I make certain changes- two records are inserted instead of one.

 

Could someone tell me what I am doing wrong?

 

trigger Itemupdated on OpportunityLineItem (before update) {

 

//List to hold LineItemHistory
List <Oppty_Line_Item_History__c> insertHIST = new List <Oppty_Line_Item_History__c>();
Oppty_Line_Item_History__c IH;

for (OpportunityLineItem o: Trigger.new) {

OpportunityLineItem oldoli = Trigger.oldMap.get(o.ID);
if (o.site__c != oldoli.site__c||o.fixed_Variable__c != oldoli.fixed_Variable__c||o.UnitPrice != oldoli.UnitPrice||
o.quantity != oldoli.quantity||o.Modification_Type__c != oldoli.Modification_Type__c)
{



IH= new Oppty_Line_Item_History__c ();
IH.Action_Initiated_by__c=o.lastmodifiedbyId;
IH.Action_Date__c=o.lastmodifieddate;
IH.Action__c = 'Product Updated';
IH.Oppty_Line_Item_id__c = o.id;
IH.Opportunity__c = o.OpportunityId;
IH.Product_Name__c=o.Product_Name_in_TEXT__c;
IH.Oppty__c=o.opptytrigger__c;
IH.Line_Item__c=o.Line_Item_Id__c;
IH.Product_Family__c = o.Product_Family__c;
IH.Site__c=o.Site__c;
IH.fixed_Variable__c=o.fixed_Variable__c;
IH.Discount__c=o.Discount__c;
IH.Modification__c=o.Modification__c;
IH.Modification_Type__c= o.Modification_Type__c;
IH.Quantity__c=o.Quantity;
IH.Sales_Price__c = o.UnitPrice;
IH.Total_Price__c = o.Total__c;

insertHIST.add(IH);

}



//once loop is done, you need to insert new records in SF
//DML operations might cause an error, so you need to catch it with try/catch block.
try {
insert insertHIST;
}
catch (system.Dmlexception e) {
system.debug(e);
}

 

 

}}

 

Best Answer chosen by Admin (Salesforce Developers) 
Kiran  KurellaKiran Kurella

It is not a best practice to catch an error and ignoring it. I recommend changing your try catch statement as follows:

try {
       insert insertHIST;
} catch (DmlException ex) {
        //trigger.new[0].addError(ex.getDmlMessage(0));
        throw ex;
}

All Answers

Avidev9Avidev9
Do you have any workflow field update in OpportunityLineItem object ?
Avidev9Avidev9

ahh got the problem. You mis placed the brackets

 

It should be something like 

rigger Itemupdated on OpportunityLineItem(before update) {

    //List to hold LineItemHistory
    List < Oppty_Line_Item_History__c > insertHIST = new List < Oppty_Line_Item_History__c > ();
    Oppty_Line_Item_History__c IH;

    for (OpportunityLineItem o: Trigger.new) {

        OpportunityLineItem oldoli = Trigger.oldMap.get(o.ID);
        if (o.site__c != oldoli.site__c || o.fixed_Variable__c != oldoli.fixed_Variable__c || o.UnitPrice != oldoli.UnitPrice ||
            o.quantity != oldoli.quantity || o.Modification_Type__c != oldoli.Modification_Type__c) {



            IH = new Oppty_Line_Item_History__c();
            IH.Action_Initiated_by__c = o.lastmodifiedbyId;
            IH.Action_Date__c = o.lastmodifieddate;
            IH.Action__c = 'Product Updated';
            IH.Oppty_Line_Item_id__c = o.id;
            IH.Opportunity__c = o.OpportunityId;
            IH.Product_Name__c = o.Product_Name_in_TEXT__c;
            IH.Oppty__c = o.opptytrigger__c;
            IH.Line_Item__c = o.Line_Item_Id__c;
            IH.Product_Family__c = o.Product_Family__c;
            IH.Site__c = o.Site__c;
            IH.fixed_Variable__c = o.fixed_Variable__c;
            IH.Discount__c = o.Discount__c;
            IH.Modification__c = o.Modification__c;
            IH.Modification_Type__c = o.Modification_Type__c;
            IH.Quantity__c = o.Quantity;
            IH.Sales_Price__c = o.UnitPrice;
            IH.Total_Price__c = o.Total__c;

            insertHIST.add(IH);

        }


        //once loop is done, you need to insert new records in SF
        //DML operations might cause an error, so you need to catch it with try/catch block.


    }

    try {
        insert insertHIST;
    } catch (system.Dmlexception e) {
        system.debug(e);
    }
}

 

Kiran  KurellaKiran Kurella

It is not a best practice to catch an error and ignoring it. I recommend changing your try catch statement as follows:

try {
       insert insertHIST;
} catch (DmlException ex) {
        //trigger.new[0].addError(ex.getDmlMessage(0));
        throw ex;
}

This was selected as the best answer
LaurenP6777LaurenP6777
Thank you both! I should have posted this days ago:-)