3

My requirement is to update a record in one Salesforce org (org A) from another Salesforce org's (org B) Apex code. I have taken the Partner WSDL of org A and imported it into org B. Using the Partner WSDL code, I am able to login successfully. But I am not able to update a field on a custom object using Partner WSDL. Here is the code that I have so far written.

    partnerSoapSforceCom.LoginResult loginResult = sp.login(username, password);
    sp.endpoint_x = loginResult.serverUrl;
    SessionHeader.sessionId = loginResult.sessionId;

    sobjectPartnerSoapSforceCom.sObject_x[] lstObjects = new  sobjectPartnerSoapSforceCom.sObject_x[]{};
    sobjectPartnerSoapSforceCom.sObject_x singleObj = new sobjectPartnerSoapSforceCom.sObject_x();

    singleObj.type_x = 'Position__c';
    singleObj.Id = idPositionRecord;

    /**** HOW DO I set the value of a custom field here eg: Stage__c to New ****/

    sObjects.add(singleObj);

    sp.SessionHeader = SessionHeader;
    sp.update_x(sObjects);

Thanks in adv.

3

The Partner WSDL contains XML Schema features that are not supported by the WSDL2Apex tool, specifically in the case of the sObject type, it uses the any attribute.

        <complexType name="sObject">
            <sequence>
                <element name="type"               type="xsd:string"/>
                <element name="fieldsToNull"       type="xsd:string" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
                <element name="Id"                 type="tns:ID" nillable="true" />
                <any namespace="##targetNamespace" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
            </sequence>
        </complexType>

As per the documentation here, any is an element not listed as supported, unfortunately the WSDL2Apex tool does not output an error any warnings, and just skips this fact, hence you get the Apex sObject class that looks like the following and is not very useful.

public class sobjectPartnerSoapSforceCom {
    public class sObject_x {
        public String type_x;
        public String[] fieldsToNull;
        public String Id;
        private String[] type_x_type_info = new String[]{'type','urn:sobject.partner.soap.sforce.com',null,'1','1','false'};
        private String[] fieldsToNull_type_info = new String[]{'fieldsToNull','urn:sobject.partner.soap.sforce.com',null,'0','-1','true'};
        private String[] Id_type_info = new String[]{'Id','urn:sobject.partner.soap.sforce.com',null,'1','1','true'};
        private String[] apex_schema_type_info = new String[]{'urn:sobject.partner.soap.sforce.com','true','false'};
        private String[] field_order_type_info = new String[]{'type_x','fieldsToNull','Id'};
    }
}

So there is no class member you can set to define your fields, in theory you could use the Enterprise WSDL to generate explicit types that represent the custom objects in your target org, though i suspect you will also want the same when querying your source org. The WSDL for Enterprise is also very very large so I would not recommend attempting to perform WSDL2Apex on it for this reason alone.

Possible Alternative Route Forward

I would propose you look at using the Salesforce REST API from Apex, there has been a number of posts on this on StackExchange. Here for example is some code that allows you to update the currency objects via the REST API via Apex.

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v28.0/sobjects/DatedConversionRate/04wb0000000KzHlAAK?_HttpMethod=PATCH');
req.setBody('{ "ConversionRate" : 2.5 }');
req.setHeader('Authorization', 'OAuth ' + sessionId);
req.setHeader('Content-Type', 'application/json');
req.setMethod('POST');

NOTE: A login example from Apex without using Partner WSDL can be found here. HttpResponse res = h.send(req);

You basically encode the fields you want to set in the JSON string set in the body of the request. Note that this is not a bulk API, so depending on the volumes this may not be ideal. I've never used the Salesforce Bulk API from Apex, but in theory if your determined enough its possible.

Finally you may also be interested in External Data Sources, a feature upcoming in the platform, you can read more here.

| improve this answer | |
  • Thanks for the detailed reply, EDS seems very interesting. – DCBoy Feb 3 '14 at 6:16
  • Has anyone worked around the fact of not having any type in the partner wsdl. It almost seems like Partner WSDL is not useful when you want to update data? – DCBoy Feb 3 '14 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.