Search in sources :

Example 1 with SearchResult

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

the class PartnerSamples method searchSample.

private void searchSample() {
    // call the login function to do so
    if (!loggedIn) {
        if (!login()) {
            return;
        }
    }
    SearchResult sr = null;
    QueryResult qr = null;
    try {
        sr = binding.search("find {4159017000} in phone fields returning contact(id, phone, firstname, lastname), lead(id, phone, firstname, lastname), account(id, phone, name)");
        SearchRecord[] records = sr.getSearchRecords();
        Vector contacts = new Vector();
        Vector leads = new Vector();
        Vector accounts = new Vector();
        if (records != null && records.length > 0) {
            for (int i = 0; i < records.length; i++) {
                SObject record = records[i].getRecord();
                if (record.getType().toLowerCase().equals("contact")) {
                    contacts.add(record);
                } else if (record.getType().toLowerCase().equals("lead")) {
                    leads.add(record);
                } else if (record.getType().toLowerCase().equals("account")) {
                    accounts.add(record);
                }
            }
            if (contacts.size() > 0) {
                System.out.println("Found " + new Integer(contacts.size()).toString() + " contacts:");
                for (int i = 0; i < contacts.size(); i++) {
                    SObject c = (SObject) contacts.get(i);
                    System.out.println(c.getId() + " - " + c.get_any()[1] + " " + c.get_any()[2] + " - " + c.get_any()[0]);
                }
            }
            if (leads.size() > 0) {
                System.out.println("Found " + new Integer(leads.size()).toString() + " leads:");
                for (int i = 0; i < leads.size(); i++) {
                    SObject l = (SObject) leads.get(i);
                    System.out.println(l.getId() + " - " + l.get_any()[1] + " " + l.get_any()[2] + " - " + l.get_any()[0]);
                }
            }
            if (accounts.size() > 0) {
                System.out.println("Found " + new Integer(accounts.size()).toString() + " accounts:");
                for (int i = 0; i < accounts.size(); i++) {
                    SObject a = (SObject) accounts.get(i);
                    System.out.println(a.getId() + " - " + a.get_any()[1] + " - " + a.get_any()[0]);
                }
            }
        } else {
            System.out.println("No records were found for the search.");
        }
        System.out.println("\nSearch succesfully executed.");
        getUserInput("\nHit return to continue...");
    } catch (ApiFault af) {
        System.out.println("\nFailed to execute search succesfully, error message was: \n" + af.getExceptionMessage());
        getUserInput("\nHit return to continue...");
    } catch (Exception ex) {
        System.out.println("\nFailed to execute search succesfully, error message was: \n" + ex.getMessage());
        getUserInput("\nHit return to continue...");
    }
}
Also used : SearchRecord(com.salesforce.soap.partner.SearchRecord) QueryResult(com.salesforce.soap.partner.QueryResult) ApiFault(com.salesforce.soap.partner.fault.ApiFault) SObject(com.salesforce.soap.partner.sobject.SObject) SearchResult(com.salesforce.soap.partner.SearchResult) Vector(java.util.Vector) RemoteException(java.rmi.RemoteException) ServiceException(javax.xml.rpc.ServiceException) IOException(java.io.IOException)

Aggregations

QueryResult (com.salesforce.soap.partner.QueryResult)1 SearchRecord (com.salesforce.soap.partner.SearchRecord)1 SearchResult (com.salesforce.soap.partner.SearchResult)1 ApiFault (com.salesforce.soap.partner.fault.ApiFault)1 SObject (com.salesforce.soap.partner.sobject.SObject)1 IOException (java.io.IOException)1 RemoteException (java.rmi.RemoteException)1 Vector (java.util.Vector)1 ServiceException (javax.xml.rpc.ServiceException)1