1

I tried make this restapex code, and tried to using SOSL to find value this is my apex code when i used SOQL and it works well.

@RestResource(urlMapping='/Account/*')
global with sharing class UT_MDM_ResponseJSON {
    @HttpGet
    global static Account doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId];
        return result;
    }

enter image description here

But, i want to change from SOQL to SOSL. This is code when i tried using SOSL, but there is no result, what should i do to make it works?

@HttpGet
global static List<List<Account>> getItem(){
    RestRequest req = RestContext.request;
    String searchQuery=req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);

    List<List<Account>> itemList;
    RestResponse res = new RestResponse();
    RestContext.response = res;

    try{
        itemList = [FIND :searchQuery IN ALL FIELDS RETURNING Account(Id, Name, Phone, Website)];
        res.responseBody = Blob.valueOf(JSON.serialize(itemList));
    } catch(Exception e){
        System.debug('Error:  ' + e.getMessage());
    }
    return itemList;
}

in my SOQL just find by ID, but i wanted to change to SOQL so it can find id, phone, name or whatever value in field account

https://utdev.my.salesforce.com//services/apexrest/Account/0019D000008DEcZQAW -> Find by ID

https://utdev.my.salesforce.com//services/apexrest/Account/6281928731 -> Find by Phonename

2
  • Can you edit your question to include the value you are using in your request in the second example? EG is it the Id value or some other string that matches the name?
    – Jeff Kranz
    Commented Apr 14, 2022 at 1:41
  • Halo @JeffKranz, i already edit my question :) Commented Apr 14, 2022 at 2:17

1 Answer 1

6

Don't mix responseBody and a return value. Use one or the other. It's a lot easier to use the return value, and offers more functionality for no extra effort, such as supporting XML and JSON input and output. Note that if you use responseBody, the return type is ignored.

Here's a fixed version of your code:

@HttpGet global static List<List<sObject>> httpGet() {
    String searchQuery=RestContext.request.requestURI.substringAfterLast('/');
    return [FIND :searchQuery IN ALL FIELDS RETURNING Account(Name, Phone, Website)];
}

As you can see, it couldn't be simpler.

Note that the reason why your code was also acting weird is because you replaced the default RestResponse:

RestResponse res = new RestResponse();
RestContext.response = res; // Do not do this in production code

The response object is writable for unit tests (and you should initialize it in those cases), but you should not replace the default response object in production code.

@HttpGet global static void httpGet() {
    String searchQuery=RestContext.request.requestURI.substringAfterLast('/');
    RestContext.response.responseBody = Blob.valueOf(JSON.serialize([FIND :searchQuery IN ALL FIELDS RETURNING Account(Name, Phone, Website)]));
}

If you lose access to the RestResponse object the system gives you, you cannot respond to the request using that object.

Finally, note that your return type was incorrect; it should have been List<List<sObject>>, not List<List<Account>>. You were likely getting an exception on return, since the two types are considered incompatible.

Again, I strongly recommend using the first method, because it allows the client to specify if they want the response in JSON or XML without any extra effort on the part of the Salesforce developer.

Also, one final note: the Id field isn't actually "searchable." If you discover that you've been given an ID, you'll need to use SOQL, instead. Something like this:

@HttpGet global static List<List<sObject>> httpGet() {
    String searchQuery=RestContext.request.requestURI.substringAfterLast('/');
    if(searchQuery instanceOf Id) {
        Account record = [SELECT Name, Phone, Website FROM Account WHERE Id = :searchQuery];
        return new List<List<sObject>> {
          new List<sObject> { record }
        };
    }
    return [FIND :searchQuery IN ALL FIELDS RETURNING Account(Name, Phone, Website)];
}
1
  • Thankyou so muchhh :)), it works Commented Apr 14, 2022 at 8:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .