You need to sign in to do that
Don't have an account?
Apex Metadata API for Field History Tracking
Hi,
We have and automated code for updating the page layout through Apex Metadata API for example in like in below link.
https://trailhead.salesforce.com/modules/apex_metadata_api/units/apex_metadata_api_updates
Can we have some similar code for enabling the field history tracking of some fields.
Thanks
We have and automated code for updating the page layout through Apex Metadata API for example in like in below link.
https://trailhead.salesforce.com/modules/apex_metadata_api/units/apex_metadata_api_updates
Can we have some similar code for enabling the field history tracking of some fields.
Thanks
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
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;
}
}
Thanks,
Saroja
SweetPotatoTec