4

I'm trying to create an object of SObject class and then setting its type to Lead and setting one of the fields for Email.

Eventually, I want to pass this object in findDuplicates() method. But the compiler says that it cannot resolve methods setType and setField for given object. It's failing at the compilation. Following is the code:

    SObject[] inputSObjectArray = new SObject[1];
    // Instantiate an empty Java SObject
    SObject searchCriteria = new SObject();
    // Set its type to Lead.
    searchCriteria.setType("Lead");
    // Set Email field.
    searchCriteria.setField("Email", "[email protected]");
    // Add the sObject to the input array
    inputSObjectArray[0] = searchCriteria;

    Connection.getInstance().getConnection().setDuplicateRuleHeader(false, true, false);

    FindDuplicatesResult[] callResults = null;

    try {
        callResults = Connection.getInstance().getConnection().findDuplicates(inputSObjectArray);
    } catch (ConnectionException e) {
        e.printStackTrace();
    }

    if (callResults != null && callResults.length > 0) {
        // print results
    }

Following is the compilation error:

Error:(223, 23) java: cannot find symbol
symbol: method setType(java.lang.String)
location: variable searchCriteria of type com.sforce.soap.enterprise.sobject.SObject

Error:(225, 23) java: cannot find symbol
symbol: method setField(java.lang.String,java.lang.String)
location: variable searchCriteria of type com.sforce.soap.enterprise.sobject.SObject

I'm referring to the sample code given for Java in SalesForce documentation. Ref: https://resources.docs.salesforce.com/206/latest/en-us/sfdc/pdf/apex_api.pdf

Update: I've got the enterprise.wsdl from my SalesForce Admin. I generated the enterprise.jar using the guidelines from here. And I've included this external jar in my Java project. So I'm assuming all the Core classes / methods should be available for use to me, which is not happening.

5
  • Hi Adrian, I've updated the question with code that I'm trying. Thanks.
    – Udit Desai
    Feb 6, 2017 at 19:44
  • 1
    Please also include the error message verbatim. In general that makes it much easier to identify what went wrong. Welcome to SFSE!
    – Adrian Larson
    Feb 6, 2017 at 19:45
  • It's not even compiling successfully. I've added the error message in the question.
    – Udit Desai
    Feb 6, 2017 at 19:48
  • 1
    Is this in apex or in java? If its in java, you may have an issue with one or more of the WSDL's you need to import into your project to access the SOAP API Objects. Try checking the schema for the SObject class, and see if all the functions on the object are available. Feb 6, 2017 at 19:56
  • Both are not same? I'm sorry, but I'm working on this for the first time. I've included the enterprise.jar in my Java project. I've updated my questions with the information how I got it working. Please refer to it. Thanks.
    – Udit Desai
    Feb 6, 2017 at 20:01

1 Answer 1

3

The sample code appears to be using the Partner API.

Your example error messages indicate you are using the Enterprise WSDL (com.sforce.soap.enterprise.sobject.SObject).

The Partner API is loosely typed versus the Enterprise WSDL which is strongly typed. Because the Partner API is loosely typed you need to set the sObject type (setType) and use a generic method to set the field values (setField). The Enterprise WSDL in comparison would has a strongly typed Lead class with a dedicated property to set the Email field.

The Partner API is more flexible as it isn't tied to a fixed schema in Salesforce. The Enterprise API is easier up front as everything is strongly typed. However, the Enterprise API makes handling changes much harder.

Try using the Partner API WSDL instead if you want to keep the current code you have. Then the sObject class should be from the com.sforce.soap.partner.sobject.wsc namespace.

3
  • Well, unfortunately, I'm provided with only the Enterprise WSDL. So I don't think I'll be able to use the Partner API. Is there any sample code that I can refer to, for Enterprise API ?
    – Udit Desai
    Feb 6, 2017 at 20:09
  • Could you just ask your Salesforce Admin for the Partner API? Or if you have credentials to login to Salesforce you could probably get it yourself (unless they really want you to use the Enterprise API for some reason). There is very basic code for establishing a Session in Verify the WSDL Environment Feb 6, 2017 at 20:20
  • @UditDesai There is some better Enterprise API sample code in Walk Through the Sample Code. If you can, do see if you can use the Partner API. It's a bit more work up front, but will save you a lot of pain in the long run when things start changing. Feb 6, 2017 at 20:22

You must log in to answer this question.

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