I am trying to delete fields using metadata API . But how to set the type_x vale for the field i chose ?
The describeFieldresult getType() method just gets me DisplayName for the field i chose. Or is there any way to set it dynamically ?
I am trying to delete fields using metadata API . But how to set the type_x vale for the field i chose ?
The describeFieldresult getType() method just gets me DisplayName for the field i chose. Or is there any way to set it dynamically ?
You can use the delete operation to delete any Metadata type that extends the Metadata base type (from the WSDL). The CustomField Metadata type is such a type.
Represents the metadata associated with a custom field. Use this metadata type to create, update, or delete custom field definitions. It extends the Metadata metadata type and inherits its fullName field.
It does appear odd that a full populated CustomField structure needs to be passed to the delete operation (hence I assume why you want to know the type?). Have you tried using only the Metadata type and populating only the fullName?
Use the following process to delete metadata components: Determine the fullName of each component that you want to delete. See Metadata for more details on the fullName field. You must delete only components of the same type in a single call. Invoke this call, passing in the array of metadata components with fullName specified
The Java example for the 'delete' operation is wrong btw, it shows a 'create' example.
Since your using the Apex Metadata API, here is the correct example.
MetadataService.MetadataPort service = createService();
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'Test__c.TestField__c';
MetadataService.AsyncResult[] results = service.deleteMetadata(new List<MetadataService.Metadata> { customField });
NOTE: Update your MetadataSerivce.cls here also.
Background to the above: It turns out there is what I think is a bug in the Metadata WSDL for the 'type' element in the CustomField type, it is the only one not described as optional, hence the generated code describes it as such. Hence, even though you don't assign a value (in a delete use case) the Apex to XML serialiser is sending this...
<env:Body>
<delete xmlns="http://soap.sforce.com/2006/04/metadata">
<metadata xsi:type="CustomField">
<fullName>Test__c.TestField__c</fullName>
<type />
</metadata>
</delete>
</env:Body>
When it should be sending this...
<env:Body>
<delete xmlns="http://soap.sforce.com/2006/04/metadata">
<metadata xsi:type="CustomField">
<fullName>Test__c.TestField__c</fullName>
</metadata>
</delete>
</env:Body>
There is also slight difference in the way XML serialisers work in the underlying Java implementations at large and the one in the platform, some don't send XML elements if the bound member variable in the class is null, some (correctly) send an empty element if no minOccurs="0" element is applied in the WSDL used to generate the code. Some don't, hence why I suspect other platforms consuming this API with less strict adherence to the WSDL semantics have not come across this.
Fortunately this is something we can fix in the WSDL and/or in the generated code (if its already been generated as in this case). Which is what I've done and updated the Github repository for the API.
That was fun!
I've never tried myself but maybe check with the Type object, that might help you : here ;-)