Trigger:
Custom Opportunity History Tracking
Create Custom Object : OpportunityHistory
Fields : Field Name(Text), Old Value(Text) and New Value(Text)
Trigger Logic:
• When new opportunity is inserted in the system, It creates history record with Field Name = ‘Record Created’.
• When Record is updated, it first identifies which all fields got updated and then it creates 'OpportunityHistory’ record one for each updated field with Field Name, Old Value and New Value information
I have currently written code just to check the 'NextStep' field. I am not able to proceed further, Unable to fetch and check all fields in Opportunity. Edited the code, just updated trigger is writter in the code. Code:
trigger OpportunityHistory on Opportunity (after update) {
for(Opportunity opp : Trigger.new) {
System.debug('NEW MAP' +opp );
Opportunity oppOld = Trigger.oldMap.get(opp.Id);
System.debug('OLD MAP' +oppOld );
OpportunityHistory__c oppHist = new OpportunityHistory__c();
if(oppOld.NextStep != opp.NextStep) {
oppHist.Opportunity__c = opp.Id;
oppHist.Field_Name__c = 'Record Created 2.0';
oppHist.Old_Value__c = oppOld.NextStep;
System.debug('Old Next Step Value: ' +oppHist.Old_Value__c );
oppHist.New_Value__c = opp.NextStep;
System.debug('New Next Step Value: ' +oppHist.New_Value__c );
insert oppHist;
}
}
}
tour
on how to ask a good question. Right now you seem to know on what needs to be done, Go through trailhead module Apex Triggers to learn, after it please share what you have tried so far to get better support.[tour]
instead.