1

while running this code in anonymous window getting the error below

        String useremailaddress = 'test@test.com';
        HttpRequest req = new HttpRequest();
        String query='SELECT+id+,+uuid__c+from+User+WHERE+Username='+useremailaddress;
        req.setEndpoint('callout:salesforceauthnam/services/data/v56.0/query?q='+query);
        req.setMethod('GET');
        Http http = new Http();
        HTTPResponse resp = http.send(req);
        system.debug('Body:'+resp.getBody());

Getting error malformed on where clause

[{"message":"\nuuid__c from User where Username= 'test@test.com'\n ^\nERROR at Row:1:Column:53\nBind variables only allowed in Apex code","errorCode":"MALFORMED_QUERY"}

1 Answer 1

3

As the error says you cannot use a Bind Variable in SOQL via REST API.

Try modifying the code as below

String query= = 'SELECT+id+,+uuid__c+from+User+WHERE+Username=\'' + String.escapeSingleQuotes(useremailaddress)+ '\'');

req.setEndpoint('callout:salesforceauthnam/services/data/v56.0/query?q='+query);

What I have done is instead of using the bind variable there, made sure to pass it as a normal string. I have used '\'' to escape the quotes. Also enclosed your username in escapeSingleQuotes to avoid SOQL Injection errors.

1
  • 1
    thank you @mohith its working good
    – vijay
    Nov 27, 2022 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.