Salesforce Java wsdl malformed query exception in QueryResult

Viewed 21

I'm trying to get the user record from salesforce based on Email. I'm providing SQL query in request string.

Problem comes with email containing apostrophe like Prash.O'Brian@testdomain.com

That apostrophe making query as malformed, I tried replacing apostrophe in email in query string.

  1. Query string: "select Id from Contact where Email = " + "\"" + email + "\""
  2. replacing it to double apostrophe "''".
  3. replacing it to slash and apostrophe "\'".

None of these worked!

public String contactSearch(String email){
     String request = String.format(
                  "select Id from Contact where Email = '%s'",
                  email
          );

final QueryResult queryResult = withTry(r -> connection().query(r), request, errMsg);

    if (!queryResult.isDone()) {
      throw new IllegalStateException(err);
    }

    return queryResult.getRecords().length == 0 ? null : queryResult.getRecords()[0].getId();
  }

Any suggestions please! Why salesforce giving me Malformed Query, how to handle emails with apostrophe in salesforce SQL query calls?

0 Answers
Related