Search code examples
apache-axissalesforcewebservice-clientforce.com

SForceAPI : unable to find classes listed on API? (Account, Contact, etc)


API referred : http://www.salesforce.com/us/developer/docs/api/index.htm
subsection: reference->standard objects

Client side details : partner.wsdl, Axis2 1.5, generated stubs using unpacked option (-u).

I was hoping to find some basic objects like Account, Contact, etc (which were listed on above url) so that I can do something like

-> SObject[] sObjArray = queryResult.getRecords(); 

   for(SObject sObj : sObjArray){
     Account acc = [Account] sObj; 
   }

[used above approach successfully in another webservice - 'Zuora']

However, I could not find Account class in the generated classes. I guess I am into wrong approach, but atleast I should be finding the classes listed in the reference API.

Please help.


Solution

  • The partner WSDL has a loosely-typed data model that allows interaction with any organization's data without its schema being known in advance - you just get SObjects. In contrast, the enterprise WSDL is strongly typed, and has the Account, Contact etc types you are looking for - see http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_partner.htm

    Also, since you generate the enterprise WSDL on demand for your org, it includes your custom types (or objects, in Salesforce parlance).

    [Updated to answer comment...]

    I generated stubs with

    wsdl2java.sh -uri ~/soapclient/partner.wsdl.xml -p com.superpat.partner -d adb -u -s
    

    I'm not an Axis2 expert, but I hacked the following together and it seems to work:

    package axis2partner;
    
    import com.sforce.soap.partner.Login;
    import com.sforce.soap.partner.LoginResult;
    import com.sforce.soap.partner.Query;
    import com.sforce.soap.partner.QueryResult;
    import com.sforce.soap.partner.SessionHeader;
    import com.sforce.soap.partner.sobject.SObject;
    import com.superpat.partner.SforceServiceStub;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.apache.axiom.om.OMElement;
    
    public class Main {
        private static String username = "[email protected]";
        private static String password = "password";
        private static String securityToken = "SECURITY_TOKEN";
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            try {
                // First, login to get a session ID and server URL
                SforceServiceStub loginStub = new SforceServiceStub();
    
                Login login = new Login();
    
                login.setUsername(username);
                login.setPassword(password + securityToken);
    
                LoginResult loginResult
                        = loginStub.login(login, null, null).getResult();
    
                // Now make a stub for the correct service instance
                SforceServiceStub serviceStub
                        = new SforceServiceStub(loginResult.getServerUrl());
    
                SessionHeader sessionHeader = new SessionHeader();
                sessionHeader.setSessionId(loginResult.getSessionId());
    
                // Now we can execute the actual query
                Query query = new Query();
                query.setQueryString("SELECT Id, Name, AccountNumber, BillingCity,"
                        + " BillingState, Description FROM Account");
    
                QueryResult queryResult = serviceStub.query(query, sessionHeader,
                        null, null, null, null).getResult();
    
                SObject[] sObjArray = queryResult.getRecords();
    
                for ( SObject sObj : sObjArray ) {
                    System.out.println(sObj.getId());
                    for ( OMElement omElement : sObj.getExtraElement() ) {
                        System.out.println("\t" + omElement.getLocalName() + ": "
                                + omElement.getText());
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    

    In my dev org, this produces output of the form:

    0015000000VALE3AAP
        Id: 0015000000VALE3AAP
        Name: United Oil & Gas Corp.
        AccountNumber: CD355118
        BillingCity: New York
        BillingState: NY
        Description: World's third largest oil and gas company.
    

    NOTE - the raw SOAP interface is pretty generic, and not the easiest way to work with the Force.com API. You might want to take a look at the Force.com Web Services Connector. There is also a REST API, but it is currently (Jan 2011) in developer preview, and not for production deployment.