Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CustomField.trackHistory : disable/enable fields under Field History Tracking using the apex code (Metadata service) is not working correctly. It is showing ambiguous behaviour. This happens for the standard objects that do not have enable history tracking button, but we need to track the fields separately (i.e case,asset). #276

Open
pradeepvenkat28 opened this issue Sep 15, 2021 · 0 comments

Comments

@pradeepvenkat28
Copy link

Issue : Metadata API using apex is not able to disable/enable fields under Field History Tracking using the apex code. It is showing ambiguous behaviour.

Steps to reproduce the issue :

Use the sample apex code in the anonymous window :


List objList = new List{'Case' };
List<MetadataService.Metadata> deployList = new List<MetadataService.Metadata>();
MetadataService.MetadataPort service = DM_SandboxHelper.createService();
List<MetadataService.CustomObject> customLabelMetaList = (List<MetadataService.CustomObject>) service.readMetadata('CustomObject', objList).getRecords();
MetadataService.CustomObject cusObj = customLabelMetaList.get(0);
system.debug('cusObj.enableHistory '+cusObj.enableHistory);
for(MetadataService.CustomField cf :cusObj.fields){

if(cf.fullName == 'Description' || cf.fullName == 'BusinessHoursId' || cf.fullName == 'Priority' || cf.fullName == 'Type '){
System.debug('fullName -- '+cf.fullName);
cf.trackHistory = true ;
}
}
deployList.add(cusObj);

List<MetadataService.SaveResult> results = service.updateMetadata(new List<MetadataService.CustomObject>{cusObj});


Issue affects only the fields of the standard objects like case and assets etc that does not have a single point of enabling the history tracking ex : "Enable Account History" button on account.

1] Use this line in code to change the value to true or false.
cf.trackHistory = true ;
2] Here i am trying with the fields : 'Description' , 'BusinessHoursId', 'Priority', 'Type' on case object.
3] Check the field tracking page of the case object :
https://summer21v52-dev-ed.my.salesforce.com/p/setup/layout/LayoutFieldList?type=Case&setupid=CaseFields&retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DCase
4] Turn off all the history tracking of the above fields.
5] Now execute the above code in the anonymous window.

Result : You will see the history tracking is enabled for only fields : Description, Priority but for other fields this is not enabled.

Need to understand why is the behaviour not consistent across the fields.

I have checked the same using the Metadata deployment using the sfdx and the deployment works correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant