I'm trying to deploy a managed package that will create some new fields on the Opportunity object on install using the Metadata API.
- The entire code in the callout method below works in my dev org (i.e. it makes the HTTP call to my test endpoint and also creates the field).
- The HTTP call works fine by itself when I install the package in a different org (because I've added my test endpoint URL to Remote Site Settings).
- When I add in the Metadata API part, the package successfully installs, I get no error notifications, but it doesn't call my HTTP endpoint or create the field.
I'm using https://github.com/financialforcedev/apex-mdapi.
global class ReadMetadata implements InstallHandler {
global void onInstall(InstallContext context) {
createField(UserInfo.getSessionId());
}
@future(callout=true)
public static void createField(String sessionId){
// For logging debug statements.
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('<TEST_ENDPOINT>');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = sessionId;
MetadataService.Metadata[] newFields = new MetadataService.Metadata[]{};
MetadataService.SaveResult[] results;
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'Opportunity.foo__c';
customField.label = 'Foo';
customField.type_x = 'DateTime';
newFields.add(customField);
results = service.createMetadata(newFields);
request.setBody('{\n"result":"' + String.valueOf(results) + '"\n}');
HttpResponse response = http.send(request);
System.debug(response.getStatusCode());
}
}
Any ideas on what the issue might be?