+ Start a Discussion
Kelsey BaustianKelsey Baustian 

code for tracking opportunity product history

Hello! I'm using an Apex trigger I found online to track Opportunity Product History. Here's the original code: 
trigger trackOppProduct on OpportunityLineItem (after insert, after update) {
List<Opportunity_Product_History__c> hisTrackList = new List<Opportunity_Product_History__c>();
//
// Iterate through the OpportunityLineItems and create the History Tracking Record.
//
for (Integer i=0; i < Trigger.new.size(); i++) {

OpportunityLineItem newCM = Trigger.new[i];
if(Trigger.isInsert) {
hisTrackList.add(new Opportunity_Product_History__c(OpportunityLineItem_Record_Id__c = newCM.Id,
Object_Name__c = 'OpportunityLineItem',
Field_Name__c = 'Product Status', 
Previous_Value__c = '',
Current_Value__c = newCM.Product_Status__c,                                                 
Modified_Date_Time__c = System.now(),
Modified_By__c = UserInfo.getUserId()
));
} else if(Trigger.old[i].Product_Status__c != newCM.Product_Status__c){
OpportunityLineItem oldCM = Trigger.old[i];
hisTrackList.add(new Opportunity_Product_History__c(OpportunityLineItem_Record_Id__c = newCM.Id,
Object_Name__c = 'OpportunityLineItem',
Field_Name__c = 'Product Status',                                                   
Previous_Value__c = oldCM.Product_Status__c,
Current_Value__c = newCM.Product_Status__c,
Modified_Date_Time__c = System.now(),
Modified_By__c = UserInfo.getUserId()
));
}
}
if(!hisTrackList.isEmpty()) {
insert hisTrackList;
}
}

The problem is that this code only tracks one field. So, what I'm trying to do is add in the other fields I'd like tracked. I thought I could copy and paste chunks of the Apex to make it work. I've gotten to the below code after a few hours and it doesn't show any errors or problems. I was thinking it would track a second field that I indicated. However, it's still only tracking the first field, Product Status. 
 
trigger trackOppProduct on OpportunityLineItem (after insert, after update) {
List<Opportunity_Product_History__c> hisTrackList = new List<Opportunity_Product_History__c>();
//
// Iterate through the OpportunityLineItems and create the History Tracking Record.
//
for (Integer i=0; i < Trigger.new.size(); i++) {

OpportunityLineItem newCM = Trigger.new[i];
if(Trigger.isInsert) {
hisTrackList.add(new Opportunity_Product_History__c(OpportunityLineItem_Record_Id__c = newCM.Id,
Object_Name__c = 'OpportunityLineItem',
Field_Name__c = 'Product Status', 
Previous_Value__c = '',
Current_Value__c = newCM.Product_Status__c,                                                 
Modified_Date_Time__c = System.now(),
Modified_By__c = UserInfo.getUserId()
));
hisTrackList.add(new Opportunity_Product_History__c(OpportunityLineItem_Record_Id__c = newCM.Id,
Object_Name__c = 'OpportunityLineItem',
Field_Name__c = 'Annual Minimum', 
Previous_Value__c = '',
Current_Value__c = newCM.Annual_Minimum__c,                                                 
Modified_Date_Time__c = System.now(),
Modified_By__c = UserInfo.getUserId()                                                                                                    
));
} else if(Trigger.old[i].Product_Status__c != newCM.Product_Status__c){
OpportunityLineItem oldCM = Trigger.old[i];
hisTrackList.add(new Opportunity_Product_History__c(OpportunityLineItem_Record_Id__c = newCM.Id,
Object_Name__c = 'OpportunityLineItem',
Field_Name__c = 'Product Status',                                                   
Previous_Value__c = oldCM.Product_Status__c,
Current_Value__c = newCM.Product_Status__c,
Modified_Date_Time__c = System.now(),
Modified_By__c = UserInfo.getUserId()
));
    } else if(Trigger.old[i].Annual_Minimum__c != newCM.Annual_Minimum__c){
OpportunityLineItem oldCM = Trigger.old[i];
hisTrackList.add(new Opportunity_Product_History__c(OpportunityLineItem_Record_Id__c = newCM.Id,
Object_Name__c = 'OpportunityLineItem',
Field_Name__c = 'Annual Minimum',                                                   
Previous_Value__c = oldCM.Annual_Minimum__c,
Current_Value__c = newCM.Annual_Minimum__c,
Modified_Date_Time__c = System.now(),
Modified_By__c = UserInfo.getUserId()
));
}
}                          
if(!hisTrackList.isEmpty()) {
insert hisTrackList;
}
}

I am not a developer by any means, just an accidental admin trying to make this work. Any insight you could provide would be greatly appreciated. 
PriyaPriya (Salesforce Developers) 

Hi Kelsey,

This is still in idea. For your reference, visit this :- https://trailblazer.salesforce.com/ideaView?id=08730000000j08LAAQ

You can also upvote for it.

Regards,

Priya Ranjan