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
SF Beginner 2019SF Beginner 2019 

Opportunityline item track when deleted

Hi,

I have his code wherein I want to track when an OpportunityLine Item is deleted. already done the create and update but not the delete. can you help me? I have done this as well in process builder

trigger trackOppProduct on OpportunityLineItem (after insert, after update, after delete) {
List<History_Tracking__c> hisTrackList = new List<History_Tracking__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 History_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 History_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;
}
}
Best Answer chosen by SF Beginner 2019
Maharajan CMaharajan C
Hi,

I have assumed your custom field api names should be Opportunity__c, Product__c in History_Tracking__c Object. So please confirm this. If the Field API Names are different then use the correct API Names in below highlighted Field Assignments.

trigger trackOppProduct on OpportunityLineItem (after insert, after update, after delete) {
List<History_Tracking__c> hisTrackList = new List<History_Tracking__c>();
//
// Iterate through the OpportunityLineItems and create the History Tracking Record.
//

if(Trigger.isInsert || Trigger.isUpdate){
for (Integer i=0; i < Trigger.new.size(); i++) {
    OpportunityLineItem newCM = Trigger.new[i];
    if(Trigger.isInsert) {
        hisTrackList.add(new History_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(),
        Opportunity__c = newCM.OpportunityId,
        Product__c = newCM
.Product2Id
        ));
    } else if(Trigger.old[i].Status != newCM.Status){
        OpportunityLineItem oldCM = Trigger.old[i];
        hisTrackList.add(new History_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(),
        Opportunity__c = oldCM.OpportunityId,
        Product__c = oldCM.Product2Id

        ));
    } 
}
}
else if(Trigger.isDelete) {
        for (OpportunityLineItem oldCM : Trigger.old) {
        hisTrackList.add(new History_Tracking__c(OpportunityLineItem_Record_Id__c = oldCM.Id,
        Object_Name__c = ‘OpportunityLineItem’,
        Field_Name__c = ‘Status’,
        Previous_Value__c = '',
        Current_Value__c = oldCM.Status,
        Modified_Date_Time__c = System.now(),
        Modified_By__c = UserInfo.getUserId(),
        Opportunity__c = oldCM.OpportunityId,
        Product__c = oldCM.Product2Id

        ));
        }
    }
if(!hisTrackList.isEmpty()) {
    insert hisTrackList;
}
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

All Answers

SF Beginner 2019SF Beginner 2019
caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.trackOppProduct: line 6, column 1

for (Integer i=0; i < Trigger.new.size(); i++) 
SF Beginner 2019SF Beginner 2019

Error: Compile Error: Variable does not exist: i at line 24 column 57
    OpportunityLineItem oldCM = Trigger.old[i];
Maharajan CMaharajan C
Sorry again small mistake:

trigger trackOppProduct on OpportunityLineItem (after insert, after update, after delete) {
List<History_Tracking__c> hisTrackList = new List<History_Tracking__c>();
//
// Iterate through the OpportunityLineItems and create the History Tracking Record.
//

if(Trigger.isInsert || Trigger.isUpdate){
for (Integer i=0; i < Trigger.new.size(); i++) {
    OpportunityLineItem newCM = Trigger.new[i];
    if(Trigger.isInsert) {
        hisTrackList.add(new History_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 History_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()
        ));
    } 
}
}
else if(Trigger.isDelete) {
        for (OpportunityLineItem oldCM : Trigger.old) {
        hisTrackList.add(new History_Tracking__c(OpportunityLineItem_Record_Id__c = oldCM.Id,
        Object_Name__c = ‘OpportunityLineItem’,
        Field_Name__c = ‘Status’,
        Previous_Value__c = '',
        Current_Value__c = oldCM.Status,
        Modified_Date_Time__c = System.now(),
        Modified_By__c = UserInfo.getUserId()
        ));
        }
    }
if(!hisTrackList.isEmpty()) {
    insert hisTrackList;
}
}
SF Beginner 2019SF Beginner 2019
How do i fill up the a product and an opporunity?
SF Beginner 2019SF Beginner 2019
How do i fill up the a product and an opporunity?
Maharajan CMaharajan C
What is the data type of your  product and opporunity fields in History_Tracking__c Object? 
SF Beginner 2019SF Beginner 2019
it is a lookup on the product and lookup on the opportunity
Maharajan CMaharajan C
Hi,

I have assumed your custom field api names should be Opportunity__c, Product__c in History_Tracking__c Object. So please confirm this. If the Field API Names are different then use the correct API Names in below highlighted Field Assignments.

trigger trackOppProduct on OpportunityLineItem (after insert, after update, after delete) {
List<History_Tracking__c> hisTrackList = new List<History_Tracking__c>();
//
// Iterate through the OpportunityLineItems and create the History Tracking Record.
//

if(Trigger.isInsert || Trigger.isUpdate){
for (Integer i=0; i < Trigger.new.size(); i++) {
    OpportunityLineItem newCM = Trigger.new[i];
    if(Trigger.isInsert) {
        hisTrackList.add(new History_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(),
        Opportunity__c = newCM.OpportunityId,
        Product__c = newCM
.Product2Id
        ));
    } else if(Trigger.old[i].Status != newCM.Status){
        OpportunityLineItem oldCM = Trigger.old[i];
        hisTrackList.add(new History_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(),
        Opportunity__c = oldCM.OpportunityId,
        Product__c = oldCM.Product2Id

        ));
    } 
}
}
else if(Trigger.isDelete) {
        for (OpportunityLineItem oldCM : Trigger.old) {
        hisTrackList.add(new History_Tracking__c(OpportunityLineItem_Record_Id__c = oldCM.Id,
        Object_Name__c = ‘OpportunityLineItem’,
        Field_Name__c = ‘Status’,
        Previous_Value__c = '',
        Current_Value__c = oldCM.Status,
        Modified_Date_Time__c = System.now(),
        Modified_By__c = UserInfo.getUserId(),
        Opportunity__c = oldCM.OpportunityId,
        Product__c = oldCM.Product2Id

        ));
        }
    }
if(!hisTrackList.isEmpty()) {
    insert hisTrackList;
}
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
This was selected as the best answer