tscizzle
tscizzle

Reputation: 12231

SOAP - Must specify a type attribute value for the element

Using Python zeep, I'm interacting with Salesforce's SOAP (specifically, Metadata) API.

Trying to createMetadata I get this error:

Fault: Must specify a {http://www.w3.org/2001/XMLSchema-instance}type attribute value for the {http://soap.sforce.com/2006/04/metadata}metadata element

I've gathered that this is not about parameters passed to the method (the way createMetadata requires a metadata argument, which itself is an object with a fullName field), but rather about a missing xsi:type attribute somewhere.

This is my zeep call:

resp = service['createMetadata'](_soapheaders=soap_headers,
                                 metadata=[{'fullName': 'SomeCustomObject'}])

This is the generated XML:

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
  <soap-env:Body>
    <ns0:createMetadata xmlns:ns0="http://soap.sforce.com/2006/04/metadata">
      <ns0:metadata>
        <ns0:fullName>SomeCustomObject</ns0:fullName>
      </ns0:metadata>
    </ns0:createMetadata>
  </soap-env:Body>
</soap-env:Envelope>

My question is: how can I set that xsi:type on whatever it needs to be set on (that ns0:metadata guy?) using zeep?

UPDATE:
Instead of using a dictionary to represent the metadata object, I replaced it with this:

metadata_type = client.get_type('{http://soap.sforce.com/2006/04/metadata}Metadata')
metadata = metadata_type(fullName='SomeCustomObject')
resp = service['createMetadata'](_soapheaders=soap_headers, metadata=[metadata])

The new generated XML is:

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
  <soap-env:Body>
    <ns0:createMetadata xmlns:ns0="http://soap.sforce.com/2006/04/metadata">
      <ns0:metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns0:Metadata">
        <ns0:fullName>SomeCustomObject</ns0:fullName>
      </ns0:metadata>
    </ns0:createMetadata>
  </soap-env:Body>
</soap-env:Envelope>

which has the xsi:type attribute on the ns0:metadata tag, but I get the same error as before. So I guess it wasn't about a missing xsi:type. Any ideas on what it is?

Upvotes: 0

Views: 1795

Answers (2)

tscizzle
tscizzle

Reputation: 12231

The answer is that xsi:type should use 'CustomObject' (or another appropriate type) instead of 'Metadata', which is the "parent" type, I believe. Which also then requires passing more than just fullName.

In zeep that means instead of

metadata_type = client.get_type('{http://soap.sforce.com/2006/04/metadata}Metadata')
metadata = metadata_type(fullName='SomeCustomObject')

I used

custom_object_type = client.get_type('{http://soap.sforce.com/2006/04/metadata}CustomObject')
custom_object = custom_object_type(fullName='SomeCustomObject__c',
                                   label='SomeCustomObject',
                                   pluralLabel='SomeCustomObjects',
                                   nameField={'label': 'name', 'type': 'Text'},
                                   deploymentStatus='Deployed',
                                   sharingModel='ReadWrite')

and then finally:

resp = service['createMetadata'](_soapheaders=soap_headers,
                                 metadata=[custom_object])

Upvotes: 1

Jignesh Jarsaniya
Jignesh Jarsaniya

Reputation: 589

Here you can see that xsi is defined in metadata.

<metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CustomField">
</metadata>

Upvotes: 1

Related Questions