I don't know if my question fits to ask here, but let me ask it anyway. I'm building a several features using Apex Wrapper Salesforce Metadata API: https://github.com/financialforcedev/apex-mdapi
Simply put, this is just an Apex SOAP callout that invokes Metadata API (thus, the issue might not depend on this module). Thanks to this module, I was able to operate metadata via Apex and verified the change of metadata from the Setting screen in the org. However, it turns out that if I try to create a record that is defined by the metadata that is created by the metadata api operation, then I realized the change actually isn't reflected.
The weird workaround of this issues is to just wait for like 20 min. If I retry after 20- min-long wait, then I successfully created a record.
But the 20-min-long wait is not something acceptable for real use-cases. Do you have any idea to resolve this issue? Thanks.
FYI, this is my sample code:
public static void createObject(String label, String api_name){
MetadataService.MetadataPort service = createService();
MetadataService.CustomObject obj = new MetadataService.CustomObject();
obj.fullName = api_name+'__c';
obj.label = label;
obj.pluralLabel = label;
obj.nameField = new MetadataService.CustomField();
obj.nameField.type_x = 'AutoNumber';
obj.nameField.label = 'Record Number';
obj.nameField.displayFormat = 'SHN-{00000000}';
obj.deploymentStatus = 'Deployed';
obj.sharingModel = 'ReadWrite';
obj.enableReports = true;
obj.enableSharing = true;
obj.enableBulkApi = true;
obj.enableStreamingApi = true;
obj.enableSearch = true;
List<MetadataService.SaveResult> results =
service.createMetadata(
new MetadataService.Metadata[] { obj });
handleSaveResults(results[0]);
}