+ Start a Discussion
mc1392mc1392 

Query Contact with where clause

All,

Im trying to query tax id's from salesforce using a LinkedList of Strings.
Im am not performing it properly in batch/bulk format, so I am going over
my allotted requests for the day.

Can any help me out with this query?

Code:
private static LinkedList<String> queryContacts(LinkedList<String> ssns,SoapBindingStub binding){
  LinkedList<String> taxid = new LinkedList<String>();
  
  QueryResult qr = null;
   int count = 0; 
   for(Iterator<String> it = ssns.iterator();it.hasNext();){
   String txid = it.next();
   String query = "select TaxID__c from Contact where TaxID__c="; 
   //////////////////////////////////////
   try {

    query+="'"+txid+"'";
    qr = binding.query(query);
    
    if (qr.getSize() != 0) {
                 
              for(int i =0;i<qr.getSize();i++){
              SObject account = qr.getRecords()[i];
              System.out.println("Retrieved account AccountNumber__c: " +getFieldValue(account.get_any(), "TaxID__c"));

              count++;
                 }
             } else {
              System.out.println("Hmm...\nDid not find the account, that's strange.");
              taxid.add(txid);
             }
             System.out.println(count);
             //System.out.println("\nQuery succesfully executed.");
            
         } catch (ApiFault af) {
             System.out
                     .println("\nFailed to execute query succesfully, error message was: \n"
                             + af.getExceptionMessage());
             
         } catch (Exception ex) {
             System.out
                     .println("\nFailed to execute query succesfully, error message was: \n"
                             + ex.getMessage());
             
         }
         ////////////////////////////////////////////////
   }
   return taxid;
 }

 

SuperfellSuperfell
build an in query, e.g. select ... from contact where taxId__c in ( 'taxID1', 'taxID2', 'taxID3', .... )

read the results into a map to work out which ones you got and which ones you didn't.
mc1392mc1392
Just out of curiosity, how do you handle the:
"Query string cannot be more than 1000 characters"?

Since i have so many items to query for.
SuperfellSuperfell
its 10000 not 1000, but keep adding them til your string is just under the limit, run that query, then do it again, and so on until you're done.