[SalesForce] Soap WebService set mandatory fields on WSDL

Creating a custom Webservice, can I set the parameters of the webservice in such way that when I export the WSDL from SFDC such parameters result mandatory.

That mean that when someone try to call my webservice, the system automatically verify if all the mandatory fields are present and different from null in the soap call without the necessity to add apex validation logic.

thnks in advance.
Klodjan

Best Answer

Salesforce Convention in WSDL for Required Elements. It has been Salesforce's convention when exposing its own Web Services and those built via Apex to expose each child element of a given XML structure, be that derived from an Object or an Apex class, without consideration for it being required (typically denoted in XML Schema via minOccurs="1", the default if not specified) or optional.

Salesforce use of minOccurs in XML Schema and why?. Salesforce explicitly outputs minOccurs="0" for every child element, regardless if it maps to a required field on an Object for example. Meaning that web service clients cannot enforce client side the need for required information, which is a shame, as this would be a good client side optimisation and make your web service API's more self describing. However the reasoning i imagine behind this, is to allow records to be updated and/or created with only specific fields to be specified. Thus if they made the XML Schema enforce required fields they would not be able to support this convention, which can be quite useful.

<xsd:complexType name="Test__c">
    <xsd:complexContent>
        <xsd:extension base="tns:sObject">
            <xsd:sequence>
                <xsd:element name="A_Number__c" minOccurs="0" type="xsd:double" nillable="true"/>

enter image description here

Implementing required field validation without Apex code. If your using any of the Salesforce standard API's (Enterprise, Partner or REST) eventually the platform will enforce the 'required' field attribute on the field definition giving you an error, all be it only after the database operation has been attempted. Likewise if you write an Apex Web Service and it receives an SObject structure, it is only after you have attempted to perform DML (Database Manipulation Language) will an exception occur. In all but, Lookup Field types, you can configure this on a field. If you need something more custom before the database is hit, you will need to implement this yourself in your Apex code.

Related Topic