Activity Forums Salesforce® Discussions Is there any Standard feature to track Opportunity Line Item in Salesforce?

  • Saurabh

    Member
    April 20, 2017 at 6:57 am

    Hi Suraj

    Salesforce does not provide any standard feature for history tracking on opportunity line items but by using custom object and trigger we can get to this solution

    First you need to create custom object as i created opportunity_Tracking__c

    trigger opportunitytracking on OpportunityLineItem(After insert, After update) {
    List<opportunity_Tracking__c> hisTrackList = new List<opportunity_Tracking__c>();
    //
    // Iterate through the OpportunityLineItems and create the History Tracking Record.
    //
    for (Integer i=0; i < Trigger.new.size(); i++) {

    OpportunityLineItems newCM = Trigger.new[i];
    if(Trigger.isInsert) {
    hisTrackList.add(new opportunity_Tracking__c(OpportunityLineItem_Record_Id__c = newCM.Id,
    Object_Name__c = 'OpportunityLineItem',
    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){
    OpportunityLineItem oldCM = Trigger.old[i];
    hisTrackList.add(new opportunity_Tracking__c(OpportunityLineItem_Record_Id__c = newCM.Id,
    Object_Name__c = 'OpportunityLineItem',
    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;
    }
    }

    Hope it may help you:

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos