1

We are in need of creating History Tracking for the Quote object. I am trying to make this work like the History Tracking that Salesforce already has coded in the system for Opportunities (create a history row when a piece of data is changed). I am not a developer, so I am piecing all this together from code I have found online.

When my trigger runs, I get the following error:

QuoteDescriptionHistoryTracking: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Quote__c]: [Quote__c] Trigger.QuoteDescriptionHistoryTracking: line 30, column 1

On my Quote History Tracking object, I have a field called Quote__c that is a Master-Detail (Lookup) to the Quote. I set this up in order to have a link back to the Quote from the Quote History Tracking row.

This is the code in my Trigger:

      List<Quote_History_Tracking__c> hisTrackList = new List<Quote_History_Tracking__c>();
    //
    // Iterate through the Description 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 Quote_History_Tracking__c(QuoteID__c = newCM.Id,
                                                    Field_Name__c = 'Description',
                                                    Original_Value__c = '',
                                                    New_Value__c = newCM.Description,
                                                    Date__c = System.now(),
                                                    Modified_By__c = UserInfo.getUserId()                                                    
                                                    ));  
        } else if(Trigger.old[i].Description != newCM.Description){
            Quote oldCM = Trigger.old[i];            
            hisTrackList.add(new Quote_History_Tracking__c(QuoteID__c = newCM.Id,
                                                    Field_Name__c = 'Description',
                                                    Original_Value__c = oldCM.Description,
                                                    New_Value__c = newCM.Description,
                                                    Date__c = System.now(),
                                                    Modified_By__c = UserInfo.getUserId()
                                                    ));
        }
    }
     
   if(!hisTrackList.isEmpty()) {
       insert hisTrackList;
    }
}

I am sure I somehow have to relate/map the Quote__c field in this code, but am unsure how to do it.

I appreciate any help I can get. Thank you.

1 Answer 1

0

I think you are just missing the field that it is telling you about: Quote__c. This is likely an Id field and is probably the Master-Detail field from Quote to QuoteHistoryTracking.

Add it like this:

hisTrackList.add(
  new Quote_History_Tracking__c( 
   Quote__c = newCM.Id,
   QuoteID__c = newCM.Id,
   Field_Name__c = 'Description'
   //...etc
   )
);
1
  • Caspar - you are a life saver!!!! It worked like a charm and exactly like I needed it too. A BIG THANK YOU!!!!
    – roxxi
    Apr 1, 2021 at 21:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .