2

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 ?

1
  • Can I ask a favour? Wondering if you would mind changing the title of your quesiton to something like 'How to delete a custom field with the Metadata API?'. This will make the question more accessible in the future should others have this problem. Commented Aug 30, 2013 at 23:10

2 Answers 2

3

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!

8
  • System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: '' is not a valid value for the enum 'FieldType' faultcode=soapenv:Client faultactor= ---> This is the exception that is throws. I wonder why does it needs populated custom Field to delete the metadata component as you mention.
    – Soberano
    Commented Aug 26, 2013 at 15:02
  • And I am using deleteMetaData( /*Custom field*/ ) call to delete and I dont fine direct delete method in the apex class generated from WSDL. Am I doing something wrong ? Please suggest. Thanks in advance.
    – Soberano
    Commented Aug 26, 2013 at 15:12
  • Ah ha, I see your using the Apex Metadata API, let me check this again for you. Commented Aug 26, 2013 at 15:47
  • Yes,and currently I am using a map of values to do so . Like built a map of this kind. KEYS ['boolean', 'currency','date','datetime','double','email','encryptedstring','integer','multipicklist','percent','phone','picklist','string','textarea','url'], VALUES ['Checkbox','Currency','Date','DateTime','Number','Email','EncryptedText','Number','MultiselectPicklist','Percent','Phone','Picklist','Text','TextArea','Url']. Mapping this way i am getting type_x values dynamically but I do know there must me some way to do it.
    – Soberano
    Commented Aug 27, 2013 at 14:35
  • I don't think you should have to pass a fully populated CustomField personally, i'm a little slammed at present, if your OK I'll try and revist this question for you. Commented Aug 27, 2013 at 14:52
0

I've never tried myself but maybe check with the Type object, that might help you : here ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .