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
A S 31A S 31 

How to Track field History on Opportunity Line Items?

Is there any way to track the field history in Opportunity line item through a custom code? since there is no standard feature enabled for this object to track field history.

Thanks
 
Best Answer chosen by A S 31
Mahesh DMahesh D
Hi,

We can do it using Custom Object and Trigger not with the Standard History Tracking.

Step 1:
       Create an object with the name History Tracking


User-added image
Step 2:
       Create a Trigger on object where you want to track fields.

Below is the sample code:

 
trigger CampaignMemberAfterTrigger on CampaignMember (after insert, after update) {
    List<History_Tracking__c> hisTrackList = new List<History_Tracking__c>();
    //
    // Iterate through the CampaignMembers and create the History Tracking Record.
    //
    for (Integer i=0; i < Trigger.new.size(); i++) {
        
        CampaignMember newCM = Trigger.new[i];
        if(Trigger.isInsert) {
            hisTrackList.add(new History_Tracking__c(CampaignMember_Record_Id__c = newCM.Id,
                                                    Object_Name__c = 'CampaignMember', 
                                                    Field_Name__c = 'Status',
                                                    Previous_Value__c = '',
                                                    Current_Value__c = newCM.Status,
                                                    Modified_Date_Time__c = System.now(),
                                                    Modified_By__c = UserInfo.getUserId()
                                                    ));  
        } else if(Trigger.old[i].Status != newCM.Status){
            CampaignMember oldCM = Trigger.old[i];            
            hisTrackList.add(new History_Tracking__c(CampaignMember_Record_Id__c = newCM.Id,
                                                    Object_Name__c = 'CampaignMember', 
                                                    Field_Name__c = 'Status',
                                                    Previous_Value__c = oldCM.Status,
                                                    Current_Value__c = newCM.Status,
                                                    Modified_Date_Time__c = System.now(),
                                                    Modified_By__c = UserInfo.getUserId()
                                                    ));
        }
    }
    if(!hisTrackList.isEmpty()) {
        insert hisTrackList;
    }
}
Please do let me know if it helps you.

Regards,
Mahesh

 

All Answers

Mahesh DMahesh D
Hi,

We can do it using Custom Object and Trigger not with the Standard History Tracking.

Step 1:
       Create an object with the name History Tracking


User-added image
Step 2:
       Create a Trigger on object where you want to track fields.

Below is the sample code:

 
trigger CampaignMemberAfterTrigger on CampaignMember (after insert, after update) {
    List<History_Tracking__c> hisTrackList = new List<History_Tracking__c>();
    //
    // Iterate through the CampaignMembers and create the History Tracking Record.
    //
    for (Integer i=0; i < Trigger.new.size(); i++) {
        
        CampaignMember newCM = Trigger.new[i];
        if(Trigger.isInsert) {
            hisTrackList.add(new History_Tracking__c(CampaignMember_Record_Id__c = newCM.Id,
                                                    Object_Name__c = 'CampaignMember', 
                                                    Field_Name__c = 'Status',
                                                    Previous_Value__c = '',
                                                    Current_Value__c = newCM.Status,
                                                    Modified_Date_Time__c = System.now(),
                                                    Modified_By__c = UserInfo.getUserId()
                                                    ));  
        } else if(Trigger.old[i].Status != newCM.Status){
            CampaignMember oldCM = Trigger.old[i];            
            hisTrackList.add(new History_Tracking__c(CampaignMember_Record_Id__c = newCM.Id,
                                                    Object_Name__c = 'CampaignMember', 
                                                    Field_Name__c = 'Status',
                                                    Previous_Value__c = oldCM.Status,
                                                    Current_Value__c = newCM.Status,
                                                    Modified_Date_Time__c = System.now(),
                                                    Modified_By__c = UserInfo.getUserId()
                                                    ));
        }
    }
    if(!hisTrackList.isEmpty()) {
        insert hisTrackList;
    }
}
Please do let me know if it helps you.

Regards,
Mahesh

 
This was selected as the best answer
Yury BondarauYury Bondarau
Yes, it looks like the most efficiend way to track changing in Opportunity Line Items is to crate a custom apex trigger. You can do it under Opportunity Product menu (see the screenshot)
User-added image
Moe ZounMoe Zoun
This is exactly what I need. Is there a workaround with professional edition? 
Roxana ShuppRoxana Shupp
I came across this post in researching how to track history for some other objects - including Opportunity Products, Quotes. I know this is an older post, but was hoping someone could help me. 
These instructions are great and were easy to follow. I created the Histroy Tracking object, the custom triggers on my Quote object (replacing Opportunity Product with Quote info) and then put History Tracking on the Quote page layout. However, I am not a developer and when I make a change to the data, it is not recording the history. I am not sure if there is a step I am missing in order to get this to work. 

This is one of the triggers. It should be putting a row in History Tracking correct?

trigger QuoteQuoteNameHistoryTracking on Quote (after insert, after update) {
    List<History_Tracking__c> hisTrackList = new List<History_Tracking__c>();
    //
    // Iterate through the Quote Name and create the History Tracking Record.
    //
    for (Integer i=0; i < Trigger.new.size(); i++) {
        
        Quote newCM = Trigger.new[i];
        if(Trigger.isInsert) {
            hisTrackList.add(new History_Tracking__c(QuoteID__c = newCM.Id,
                                                    Object_Name__c = 'Quote', 
                                                    Field_Name__c = 'Quote Name',
                                                    Original_Value__c = '',
                                                    New_Value__c = newCM.Name,
                                                    Modified_Date_Time__c = System.now(),
                                                    Modified_By__c = UserInfo.getUserId()
                                                    ));  
        } else if(Trigger.old[i].Name != newCM.Name){
            Quote oldCM = Trigger.old[i];            
            hisTrackList.add(new History_Tracking__c(QuoteID__c = newCM.Id,
                                                    Object_Name__c = 'Quote', 
                                                    Field_Name__c = 'Quote Name',
                                                    Original_Value__c = oldCM.Name,
                                                    New_Value__c = newCM.Name,
                                                    Modified_Date_Time__c = System.now(),
                                                    Modified_By__c = UserInfo.getUserId()
                                                    ));
        }
    }
    if(!hisTrackList.isEmpty()) {
        insert hisTrackList;
    }
}