I am trying to automatically enable pages for profiles (profileApexPageAccess doc) via Metadata API. Here is my method:
public static void updateProfileAccesses(String profileName, List<String> pageNames) {
if(!String.isBlank(profileName)) {
MetadataService.IMetadataPort service = createService();
MetadataService.IReadResult readResult =
service.readMetadata('Profile', new String[] { profileName });
MetadataService.Metadata[] records = readResult.getRecords();
MetadataService.Profile p = (MetadataService.Profile)records[0];
for(String pageName : pageNames) {
Boolean isPageEnabled = false;
for(MetadataService.ProfileApexPageAccess pa : p.pageAccesses) {
if(pa.apexPage == pageName) {
pa.enabled = true;
isPageEnabled = true;
break;
}
}
if(!isPageEnabled) {
System.debug('page is NOT in the list'); // this message never appears in the logs
MetadataService.ProfileApexPageAccess pap = new MetadataService.ProfileApexPageAccess();
pap.apexPage = pageName;
pap.enabled = true;
p.pageAccesses.add(pap);
}
}
MetadataService.SaveResult[] sr = service.updateMetadata(new MetadataService.Metadata[] {p});
if(sr!=null) {
System.debug('sr error ' + sr[0].errors);
}
}
}
SaveResult always has an error with the following details:
FIELD_INTEGRITY_EXCEPTION
You can't edit tab settings for AddOnDefinition, as it's not a valid tab.
I haven't found any info on AddOnDefinition and what it has to do with ProfileApexPageAccess. Does anyone know what that might mean?
UPD I've tried the same code in another dev org and got
You can't edit tab settings for Calibration, as it's not a valid tab.
AND I found AddOnDefinition in ProfileTabVisibility list (tabVisibilities doc) but I still don't know how to get pass this error because I am not changing tabVisibilities at all.
Has anyone successfully updated page accesses automatically via Apex?