Пример #1
0
  public void querySample() {
    try {
      // Set query batch size
      partnerConnection.setQueryOptions(250);

      // SOQL query to use
      String soqlQuery = "SELECT FirstName, LastName FROM Contact";
      // Make the query call and get the query results
      QueryResult qr = partnerConnection.query(soqlQuery);

      boolean done = false;
      int loopCount = 0;
      // Loop through the batches of returned results
      while (!done) {
        System.out.println("Records in results set " + loopCount++ + " - ");
        SObject[] records = qr.getRecords();
        // Process the query results
        for (int i = 0; i < records.length; i++) {
          SObject contact = records[i];
          Object firstName = contact.getField("FirstName");
          Object lastName = contact.getField("LastName");
          if (firstName == null) {
            System.out.println("Contact " + (i + 1) + ": " + lastName);
          } else {
            System.out.println("Contact " + (i + 1) + ": " + firstName + " " + lastName);
          }
        }
        if (qr.isDone()) {
          done = true;
        } else {
          qr = partnerConnection.queryMore(qr.getQueryLocator());
        }
      }
    } catch (ConnectionException ce) {
      ce.printStackTrace();
    }
    System.out.println("\nQuery execution completed.");
  }
Пример #2
0
  public void searchSample(String phoneNumber) {
    try {
      // Example of phoneNumber format: 4155551212
      String soslQuery =
          "FIND {"
              + phoneNumber
              + "} IN Phone FIELDS "
              + "RETURNING "
              + "Contact(Id, Phone, FirstName, LastName), "
              + "Lead(Id, Phone, FirstName, LastName),"
              + "Account(Id, Phone, Name)";
      // Perform SOSL query
      SearchResult sResult = partnerConnection.search(soslQuery);
      // Get the records returned by the search result
      SearchRecord[] records = sResult.getSearchRecords();
      // Create lists of objects to hold search result records
      List<SObject> contacts = new ArrayList<SObject>();
      List<SObject> leads = new ArrayList<SObject>();
      List<SObject> accounts = new ArrayList<SObject>();

      // Iterate through the search result records
      // and store the records in their corresponding lists
      // based on record type.
      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);
          }
        }
        // Display the contacts that the search returned
        if (contacts.size() > 0) {
          System.out.println("Found " + contacts.size() + " contact(s):");
          for (SObject contact : contacts) {
            System.out.println(
                contact.getId()
                    + " - "
                    + contact.getField("FirstName")
                    + " "
                    + contact.getField("LastName")
                    + " - "
                    + contact.getField("Phone"));
          }
        }
        // Display the leads that the search returned
        if (leads.size() > 0) {
          System.out.println("Found " + leads.size() + " lead(s):");
          for (SObject lead : leads) {
            System.out.println(
                lead.getId()
                    + " - "
                    + lead.getField("FirstName")
                    + " "
                    + lead.getField("LastName")
                    + " - "
                    + lead.getField("Phone"));
          }
        }
        // Display the accounts that the search returned
        if (accounts.size() > 0) {
          System.out.println("Found " + accounts.size() + " account(s):");
          for (SObject account : accounts) {
            System.out.println(
                account.getId()
                    + " - "
                    + account.getField("Name")
                    + " - "
                    + account.getField("Phone"));
          }
        }
      } else {
        // The search returned no records
        System.out.println("No records were found for the search.");
      }
    } catch (ConnectionException ce) {
      ce.printStackTrace();
    }
  }