Search in sources :

Example 1 with QueryResult

use of com.salesforce.soap.partner.QueryResult in project tdi-studio-se by Talend.

the class PartnerSamples method upsertSample.

private void upsertSample() {
    // call the login function to do so
    if (!loggedIn) {
        if (!login()) {
            return;
        }
    }
    try {
        DescribeSObjectResult dsr = binding.describeSObject("Account");
        HashMap fieldMap = makeFieldMap(dsr.getFields());
        if (!fieldMap.containsKey("External_Id__c")) {
            System.out.println("\n\nATTENTION: To run this sample you need to \ncreate a custom text field on the Account object \nnamed External_Id with a length of 8 characters \nand with the 'external id' checkbox checked.");
        } else {
            // First, we need to make sure the test accounts do not exist.
            QueryResult qr = binding.query("Select Id From Account Where External_Id__c = '11111111' or External_Id__c = '22222222'");
            if (qr.getSize() > 0) {
                SObject[] accounts = qr.getRecords();
                // Get the ids
                String[] ids = new String[accounts.length];
                for (int i = 0; i < ids.length; i++) {
                    ids[i] = accounts[i].getId();
                }
                // Delete the accounts
                binding.delete(ids);
            }
            // Create a new account using create, we wil use this to update via upsert
            // We will set the external id to be ones so that we can use that value for the upsert
            SObject newAccount = new SObject();
            newAccount.setType("Account");
            MessageElement acctName = newMessageElement("Name", "Account to update");
            MessageElement extId = newMessageElement("External_Id__c", "11111111");
            newAccount.set_any(new MessageElement[] { acctName, extId });
            binding.create(new SObject[] { newAccount });
            // Now we will create an account that should be updated on insert based
            // on the external id field.
            SObject updateAccount = new SObject();
            updateAccount.setType("Account");
            MessageElement webSite = newMessageElement("Website", "http://www.website.com");
            MessageElement extId1 = newMessageElement("External_Id__c", "11111111");
            updateAccount.set_any(new MessageElement[] { webSite, extId1 });
            // This account is meant to be new
            SObject createAccount = new SObject();
            createAccount.setType("Account");
            MessageElement cacctName = newMessageElement("Name", "My Company, Inc");
            MessageElement extId3 = newMessageElement("External_Id__c", "22222222");
            createAccount.set_any(new MessageElement[] { cacctName, extId3 });
            // We have our two accounts, one should be new, the other should be updated.
            try {
                // Invoke the upsert call and save the results.
                // Use External_Id custom field for matching records
                UpsertResult[] upsertResults = binding.upsert("External_Id__c", new SObject[] { createAccount, updateAccount });
                for (UpsertResult result : upsertResults) {
                    if (result.isSuccess()) {
                        System.out.println("\nUpsert succeeded.");
                        System.out.println((result.isCreated() ? "Inserted" : "Updated") + " account, id is " + result.getId().toString());
                    } else {
                        System.out.println("The Upsert failed because: " + result.getErrors(0).getMessage());
                    }
                }
            } catch (RemoteException ex) {
                System.out.println("An unexpected error has occurred." + ex.getMessage());
            }
        }
        getUserInput("\nPress the RETURN key to continue...");
    } catch (ApiFault e) {
        System.out.println("Error merging account: " + e.getExceptionMessage());
        getUserInput("\nHit return to continue...");
    } catch (RemoteException e) {
        System.out.println("Error from the server on the merge sample: " + e.getMessage());
        getUserInput("\nHit return to continue...");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) DescribeSObjectResult(com.salesforce.soap.partner.DescribeSObjectResult) MessageElement(org.apache.axis.message.MessageElement) RemoteException(java.rmi.RemoteException) ServiceException(javax.xml.rpc.ServiceException) IOException(java.io.IOException) QueryResult(com.salesforce.soap.partner.QueryResult) ApiFault(com.salesforce.soap.partner.fault.ApiFault) SObject(com.salesforce.soap.partner.sobject.SObject) RemoteException(java.rmi.RemoteException) UpsertResult(com.salesforce.soap.partner.UpsertResult)

Example 2 with QueryResult

use of com.salesforce.soap.partner.QueryResult in project tdi-studio-se by Talend.

the class PartnerSamples method getLeadStatus.

private String getLeadStatus() throws UnexpectedErrorFault, RemoteException {
    QueryResult qr = binding.query("Select Id, MasterLabel from LeadStatus Where IsConverted = true");
    if (qr.getSize() > 0) {
        System.out.println("\n");
        for (int i = 0; i < qr.getRecords().length; i++) {
            System.out.println(new Integer(i + 1).toString() + ": " + getFieldValue(qr.getRecords(i).get_any(), "MasterLabel"));
        }
        String stat = getUserInput("\nEnter the number of the status to use. ");
        if (stat != null)
            return getFieldValue(qr.getRecords(new Integer(stat).intValue() - 1).get_any(), "MasterLabel");
        else
            return getFieldValue(qr.getRecords(0).get_any(), "MasterLabel");
    } else {
        return null;
    }
}
Also used : QueryResult(com.salesforce.soap.partner.QueryResult)

Example 3 with QueryResult

use of com.salesforce.soap.partner.QueryResult in project tdi-studio-se by Talend.

the class Test4Feature8540 method doQuery.

public void doQuery() throws Exception {
    binding = (SoapBindingStub) new SforceServiceLocator().getSoap(new URL("https://www.salesforce.com/services/Soap/u/16.0"));
    binding.setTimeout(60000);
    LoginResult loginResult = binding.login("musicatcher@gmail.com", "1234qwer");
    binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());
    SessionHeader sh = new SessionHeader();
    sh.setSessionId(loginResult.getSessionId());
    binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh);
    // com.sforce.soap.partner.QueryOptions qOptions = new com.sforce.soap.partner.QueryOptions();
    // qOptions.setBatchSize(new Integer(2));
    // binding.setHeader(
    // new com.sforce.soap.partner.SforceServiceLocator()
    // .getServiceName().getNamespaceURI(),
    // "QueryOptions", qOptions);
    QueryResult qr = binding.query("SELECT Name, Type, Phone, Account.CreatedBy.CreatedBy.CreatedBy.Email, Account.Owner.city, (SELECT Contact.LastName,  Contact.FirstName " + "FROM Account.Contacts Order By Contact.LastName), (SELECT Note.Title FROM Account.Notes), Account.Owner.Country  " + "FROM Account WHERE Name !='United Oil & Gas Corp.'");
    // QueryResult qr = binding
    // .query("select Name,Id,Type from Account");
    TopQueryResult topqr = new TopQueryResult();
    topqr.processTopQueryResult(qr);
    topqr.printResult();
    List<TopRecord> allTopRecords = topqr.getAllTopRecords();
}
Also used : QueryResult(com.salesforce.soap.partner.QueryResult) SforceServiceLocator(com.salesforce.soap.partner.SforceServiceLocator) SessionHeader(com.salesforce.soap.partner.SessionHeader) LoginResult(com.salesforce.soap.partner.LoginResult) URL(java.net.URL)

Example 4 with QueryResult

use of com.salesforce.soap.partner.QueryResult in project tdi-studio-se by Talend.

the class PartnerSamples method querySample.

private void querySample() {
    // call the login function to do so
    if (!loggedIn) {
        if (!login()) {
            return;
        }
    }
    QueryResult qr = null;
    QueryOptions qo = new QueryOptions();
    qo.setBatchSize(new Integer(3));
    binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(), "QueryOptions", qo);
    try {
        qr = binding.query("select id, Website, Name from Account where Name = 'Golden Straw'");
        ArrayList records = this.loadQueryResults(qr);
        if (records.size() != 0) {
            HashMap record = (HashMap) records.get(0);
            System.out.println("Retrieved " + new Integer(records.size()).toString() + " account(s) using Name = 'Golden Straw', String = " + (record.containsKey("id") ? record.get("id").toString() : " ").toString() + ", website = " + (record.containsKey("website") ? record.get("website").toString() : " ").toString());
        }
        // if (qr.getSize() != 0) {
        // SObject account = qr.getRecords()[0];
        //
        // System.out.println("Retrieved "
        // + new Integer(qr.getSize()).toString()
        // + " account(s) using Name = 'Golden Straw', String = "
        // + account.getId() + ", website = "
        // + account.get_any()[1]);
        // }
        qr = binding.query("select FirstName, LastName from Contact");
        int loopCount = 0;
        boolean continueLoop = true;
        while (continueLoop) {
            System.out.println("Results set " + new Integer(loopCount++).toString() + " - ");
            // process the query results
            for (int i = 0; i < qr.getRecords().length; i++) {
                SObject con = qr.getRecords()[i];
                String fName = "";
                String lName = "";
                if (con.get_any()[0].getName().toLowerCase().equals("firstname")) {
                    fName = con.get_any()[0].getValue();
                    lName = con.get_any()[1].getValue();
                } else
                    lName = con.get_any()[0].getValue();
                if (fName == null) {
                    System.out.println("Contact " + (i + 1) + ": " + lName);
                } else {
                    System.out.println("Contact " + (i + 1) + ": " + fName + " " + lName);
                }
            }
            // recent queryResult
            if (qr.isDone())
                continueLoop = false;
            else
                qr = binding.queryMore(qr.getQueryLocator());
        }
        System.out.println("\nQuery succesfully executed.");
        getUserInput("\nHit return to continue...");
    } catch (ApiFault af) {
        System.out.println("\nFailed to execute query succesfully, error message was: \n" + af.getExceptionMessage());
        getUserInput("\nHit return to continue...");
    } catch (Exception ex) {
        System.out.println("\nFailed to execute query succesfully, error message was: \n" + ex.getMessage());
        getUserInput("\nHit return to continue...");
    }
}
Also used : QueryResult(com.salesforce.soap.partner.QueryResult) ApiFault(com.salesforce.soap.partner.fault.ApiFault) SforceServiceLocator(com.salesforce.soap.partner.SforceServiceLocator) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SObject(com.salesforce.soap.partner.sobject.SObject) QueryOptions(com.salesforce.soap.partner.QueryOptions) RemoteException(java.rmi.RemoteException) ServiceException(javax.xml.rpc.ServiceException) IOException(java.io.IOException)

Example 5 with QueryResult

use of com.salesforce.soap.partner.QueryResult in project tdi-studio-se by Talend.

the class PartnerSamples method getContact.

private SObject getContact() throws Exception {
    SObject contactToReturn = null;
    // get a list of contacts so the user can select one
    QueryResult qr = binding.query("Select Id, FirstName, LastName, AccountId from Contact Where not AccountId = null");
    if (qr.getSize() == 0) {
        // No leads where found that have not been
        // converted, so....
        // we will create a lead and then run the query again
        System.out.println("No contacts found, will create one for you...");
        this.createContactSample();
        qr = binding.query("Select Id, FirstName, LastName, AccountId from Contact");
    }
    if (qr.getSize() > 0) {
        for (int i = 0; i < qr.getRecords().length; i++) {
            SObject contact = qr.getRecords(i);
            System.out.println(new Integer(i + 1).toString() + ": " + getFieldValue(contact.get_any(), "FirstName") + " " + getFieldValue(contact.get_any(), "LastName"));
        }
        String selectedContact = getUserInput("/nSelect the number of the contact to use. ");
        if (selectedContact != null) {
            try {
                int selContact = new Integer(selectedContact).intValue();
                contactToReturn = qr.getRecords(selContact - 1);
            } catch (Exception ex) {
                System.out.println("The number you selected is not valid, exiting...");
            }
        }
    }
    if (contactToReturn == null) {
        throw new Exception("No contact was selected, conversion failed.");
    } else {
        return contactToReturn;
    }
}
Also used : QueryResult(com.salesforce.soap.partner.QueryResult) SObject(com.salesforce.soap.partner.sobject.SObject) RemoteException(java.rmi.RemoteException) ServiceException(javax.xml.rpc.ServiceException) IOException(java.io.IOException)

Aggregations

QueryResult (com.salesforce.soap.partner.QueryResult)15 SObject (com.salesforce.soap.partner.sobject.SObject)10 RemoteException (java.rmi.RemoteException)9 IOException (java.io.IOException)7 ServiceException (javax.xml.rpc.ServiceException)7 ApiFault (com.salesforce.soap.partner.fault.ApiFault)6 QueryOptions (com.salesforce.soap.partner.QueryOptions)4 SforceServiceLocator (com.salesforce.soap.partner.SforceServiceLocator)2 HashMap (java.util.HashMap)2 DescribeSObjectResult (com.salesforce.soap.partner.DescribeSObjectResult)1 LoginResult (com.salesforce.soap.partner.LoginResult)1 Query (com.salesforce.soap.partner.Query)1 QueryAll (com.salesforce.soap.partner.QueryAll)1 QueryMore (com.salesforce.soap.partner.QueryMore)1 ResetPasswordResult (com.salesforce.soap.partner.ResetPasswordResult)1 SearchRecord (com.salesforce.soap.partner.SearchRecord)1 SearchResult (com.salesforce.soap.partner.SearchResult)1 SessionHeader (com.salesforce.soap.partner.SessionHeader)1 SetPasswordResult (com.salesforce.soap.partner.SetPasswordResult)1 UpsertResult (com.salesforce.soap.partner.UpsertResult)1