+ Start a Discussion
Jacob Friedman 11Jacob Friedman 11 

SOAP API Create and Search in same call

Howdy.  I'm using a Java program to connect to the SF SOAP API v41.  My end goal is to use the API to:
1. Create a record via create().
2. Search for the record in step 1 via search().
3. Cull list down from number 2 so that I can
4. Update that record.

Below is the code I'm using based on the samples in the documentation for the SOAP API.
I'm calling the create record first.  Then calling the search method, which uses a variable from create to get results.  For some reason the record created in step 1 is not found when I search for it in the API.  If I go to my org, I clearly see it.  If I run the code a second time, I see the record.

Any thoughts or ideas on how to get the record to show up in initial run of the code?

This is tough to articulate, so please let me know if you need any clarifcations to my question.
package resources;
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.soap.partner.sobject.*;
import com.sforce.soap.partner.*;
import com.sforce.ws.ConnectorConfig;
import com.sforce.ws.ConnectionException;
import com.sforce.soap.partner.Error;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.*;

public class PartnerSamples {
    PartnerConnection partnerConnection = null;
    private static BufferedReader reader =
        new BufferedReader(new InputStreamReader(System.in));
    
    public static void main(String[] args) {
        PartnerSamples samples = new PartnerSamples();
        if (samples.login()) {
            // Add calls to the methods in this class.
            // For example:
            // samples.querySample();
        	samples.createSample();
        	String searchVariable = "Application Gathering";
        	samples.searchSample(searchVariable);
        	
        }
    } 
    
    private String getUserInput(String prompt) {
        String result = "";
        try {
          System.out.print(prompt);
          result = reader.readLine();
        } catch (IOException ioe) {
          ioe.printStackTrace();
        }
        return result;
    }
    
    private boolean login() {
        boolean success = false;
        String username = getUserInput("Enter username: ");
        String password = getUserInput("Enter password: ");
        String authEndPoint = getUserInput("Enter auth end point: ");
        

        try {
          ConnectorConfig config = new ConnectorConfig();
          config.setUsername(username);
          config.setPassword(password);
          
          config.setAuthEndpoint(authEndPoint);
          config.setTraceFile("traceLogs.txt");
          config.setTraceMessage(true);
          config.setPrettyPrintXml(true);

          partnerConnection = new PartnerConnection(config);          

          success = true;
        } catch (ConnectionException ce) {
          ce.printStackTrace();
        } catch (FileNotFoundException fnfe) {
          fnfe.printStackTrace();
        }

        return success;
      }

    // 
    // Add your methods here.
    //
    
    public String createSample() {
        String result = null;
        try {
            // Create a new sObject of type Contact
               // and fill out its fields.
        	////figure out how to make this block a variable within a test case and get inserted here////
//            SObject contact = new SObject();
//            contact.setType("Contact");
//            contact.setField("FirstName", "Jester");
//            contact.setField("LastName", "Jespersen");
//            contact.setField("Salutation", "Professor");
//            contact.setField("Phone", "(999) 555-1234");
//            contact.setField("Title", "Philologist");
            
//            SObject enrollment = new SObject();
//            enrollment.setType("optm_cld_inr__Enrollment__c");
//            enrollment.setField("optm_cld_inr__Enrollment_Application__c", "JFSOAPEn4");
//            enrollment.setField("optm_cld_inr__Provider__c", "0034600000TadcdAAB");
//            enrollment.setField("optm_cld_inr__Enrollment_Type__c", "New Enrollment");
//            enrollment.setField("optm_cld_inr__Enrollment_Status__c", "Initiated");
//            enrollment.setField("optm_cld_inr__Source__c", "Sales");
//            enrollment.setField("optm_cld_inr__Enrollment_Format__c", "Paper");
            
            SObject credentialing = new SObject();
        	credentialing.setType("optm_cld_inr__Credential__c");
        	credentialing.setField("Name", "JFSOAPCred1");
        	credentialing.setField("optm_cld_inr__Provider__c", "0034600000TadcdAAB");
        	credentialing.setField("optm_cld_inr__Type__c", "Initial Credentialing");
        	credentialing.setField("optm_cld_inr__Credentialing_Status__c", "Initiated");

            
            
            
        
            // Add this sObject to an array 
            SObject[] credentialings = new SObject[1];
            credentialings[0] = credentialing;
            // Make a create call and pass it the array of sObjects
            SaveResult[] results = partnerConnection.create(credentialings);              
            ///////////////end block///
            
            
            // Iterate through the results list
            // and write the ID of the new sObject
            // or the errors if the object creation failed.
            // In this case, we only have one result
            // since we created one contact.
            for (int j = 0; j < results.length; j++) {
                if (results[j].isSuccess()) {
                    result = results[j].getId();
                    System.out.println(
                        "\nA contact was created with an ID of: " + result
                    );
                 } else {
                    // There were errors during the create call,
                    // go through the errors array and write
                    // them to the console
                    for (int i = 0; i < results[j].getErrors().length; i++) {
                        Error err = results[j].getErrors()[i];
                        System.out.println("Errors were found on item " + j);
                        System.out.println("Error code: " + 
                            err.getStatusCode().toString());
                        System.out.println("Error message: " + err.getMessage());
                    }
                 }
            }
        } catch (ConnectionException ce) {
            ce.printStackTrace();
        }
        return result;
        
        
    }
    
    public void searchSample(String searchVariable) {
        try {    
            // Example of phoneNumber format: 4155551212
            String soslQuery = 
                "FIND {" + searchVariable + "}  " +
                "RETURNING " +
                "Contact(Id, Phone, FirstName, LastName), " +
                "Lead(Id, Phone, FirstName, LastName)," +
                "Account(Id, Phone, Name)," +
                "optm_cld_inr__Credential_Tracking__c(Id, Name, optm_cld_inr__Credentialing_Step__c, optm_cld_inr__Credentialing__c)";
            	
            
            
            
            
            System.out.println(soslQuery);
            // 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>();
            List<SObject> credTracking = 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);
                } else if (credTracking.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")
                  );
                }
              }
           //Cred Tracking   
              System.out.println("Found " + credTracking.size() + " record(s):" );
              for (SObject optm_cld_inr__Credential_Tracking__c  : credTracking ){
              	System.out.println(optm_cld_inr__Credential_Tracking__c.getId() + "-" +
              			optm_cld_inr__Credential_Tracking__c.getField("Name") + "-" +
              			optm_cld_inr__Credential_Tracking__c.getField("optm_cld_inr__Credentialing_Step__c") + "-" +
              			optm_cld_inr__Credential_Tracking__c.getField("optm_cld_inr__Credentialing__c")
              	);}
              // 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();
        }      
    }
}