1

I have used Rest Api Code , i want to query from two different objects in single Http request,For that I am using composite Query in rest Api code

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Namedcredentialname/services/data/v39.0/query/composite');
req.setMethod('GET');
req.setbody({ 
        "compositeRequest" :[{
        "method" : "GET",
        "url" : "/services/data/v39.0/query/?q=SELECT+Id,MasterLabel,TotalLicenses,UsedLicenses,status+FROM+UserLicense",
        "referenceId" : "ref"                                
         },{
        "method" : "GET",
        "url" : "/services/data/v39.0/query/?q=SELECT+Id,name+FROM+organization",
        "referenceId" : "ref"}]});

        //Get SessionId
        string autho = 'Bearer '+userInfo.getSessionId();
        req.setHeader('Authorization', autho);

        //Get Response
        Http http = new Http();
        HTTPresponse res= http.send(req);
        string response = res.getBody();

How to construct req.body in composite Query of Rest api code? Getting error

Unrecognized symbol '"', which is not a valid Apex identifier I have modified with single quotes

req.setbody({
        'compositeRequest' :[{
        'method' : 'GET',
        'url' : '/services/data/v39.0/query/?q=SELECT+Id,MasterLabel,TotalLicenses,UsedLicenses,status+FROM+UserLicense',
        'referenceId' : 'refContact'                                
         },{
        'method' : 'GET',
        'url' : '/services/data/v39.0/query/?q=SELECT+Id,name+FROM+organization',
        'referenceId' : 'refContact'}]})

Compile Error: Expecting ')' but was: '{

req.setbody('{\"compositeRequest\" :[{
        \"method\" : \"GET\",
        \"url\" : \"/services/data/v39.0/query/?q=SELECT+Id+,MasterLabel+,TotalLicenses+,UsedLicenses+,+status+FROM+UserLicense\",
        \"referenceId\" : \"refContact\"                               
         },{
        \"method\" : \"GET\",
        \"url\" : \"/services/data/v39.0/query/?q=SELECT+Id+,name+FROM+organization\",
        \"referenceId\" : \"refContact\"}]}')

Illegal string literal: Line breaks are not allowed in string literals

4

Let's take a look at your request body:

req.setbody({ 
    "compositeRequest" :[{
    "method" : "GET",
    "url" : "/services/data/v39.0/query/?q=SELECT+Id,MasterLabel,TotalLicenses,UsedLicenses,status+FROM+UserLicense",
    "referenceId" : "ref"                                
     },{
    "method" : "GET",
    "url" : "/services/data/v39.0/query/?q=SELECT+Id,name+FROM+organization",
    "referenceId" : "ref"}]});

The documentation for HttpRequest.setBody tells us the following:

setBody(body)

Sets the contents of the body for this request.

Signature

public Void setBody(String body)

Parameters

body Type: String

Return Value

Type: Void

Usage

Limit: 6 MB for synchronous Apex or 12 MB for asynchronous Apex.

The HTTP request and response sizes count towards the total heap size.

Your request body is missing single quotes at the start and end (so it's not a string as-is). Not entirely sure how Salesforce is treating the open curly brace, but your error is being raised when Salesforce tries to parse the first double quote in "compositeRequest".

N.B. Strings cannot span multiple lines, so you'll need to put your entire JSON string on a single line, or concatenate each line using +.

+edit:

Your new error is because you haven't applied my answer correctly. The issue wasn't that you had double quotes, the issue was (and remains) that you aren't passing a string to the setBody() method (which expects a string).

// This is not a string
{"compositeRequest" :[{ ... }]}

// This is a string
// Notice how the entire thing is enclosed by single quotes (i.e. ')
'{"compositeRequest" :[{ ... }]}'

// String literals cannot span multiple lines
// The following is invalid and will result in an error
String s = 'one line,
two line,
red line,
blue line';

// You either need to put everything on a single line
String s = 'one line, two line, red line, blue line';

// Or concatenate with the + operator
String s = 'one line,'
+ ' two line,'
+ ' red line,'
+ ' blue line';
  • can you construct json srting for my req.setbody – Vinoth Balasubramaniyan Sep 27 '18 at 12:41
  • 2
    @VinothBalasubramaniyan I could, but the task is simple enough that you should be able to do it yourself. – Derek F Sep 27 '18 at 12:45
  • I have already tried with single quote,but the error is expecting ) not { – Vinoth Balasubramaniyan Sep 27 '18 at 12:46
  • 2
    @VinothBalasubramaniyan Please edit your question to add your modified code and the new error (do not overwrite your existing code or error though, as it provides important context for your question). – Derek F Sep 27 '18 at 12:48
  • 1
    Fantastic Got it,awesome explanation – Vinoth Balasubramaniyan Sep 27 '18 at 13:20
0

On top of what Derek has mentioned that you need to enclose your JSON in the request body within single quotes (that's how String is declared in Apex), you also need to fix your query. You will need to perform two things here:

  • Fix your request body enclosing in single quotes. To provide a pointer, you need to construct your request body as below (notice that it starts with a single quote, ends with a single quote and that you use the forward slash to allow quotes in the body):

    req.setbody('{\"compositeRequest\" ...')';

  • Fix your query as below. Notice the plus between the field names separated by comma. You need to put a plus sign to separate every item in your query

    SELECT+Id+,+MasterLabel+,+TotalLicenses+,+UsedLicenses+,+status+FROM+UserLicense

  • ,please see my updated error – Vinoth Balasubramaniyan Sep 27 '18 at 13:07
  • Please do not keep on editing your question with new issues. You need to first fix as what has been already recommended. The way the forum works is that you limit your question to one issue. If you have any other issue, its always recommended to open a new question. You are constructing your JSON request incorrectly, so take a look around to see how you construct such string in Apex. – Jayant Das Sep 27 '18 at 13:09
  • Jayant Das, I was the one that recommended OP edit his question. I felt that keeping it in the same question here was appropriate because my answer should have worked (aside from the query error you found, that is) and having OP include his attempt was the clearest way to demonstrate what additional information my answer needed. – Derek F Sep 27 '18 at 13:14
  • @DerekF - I think edits to an extent definitely helps. But I think OP is missing the basics of constructing a string with characters like double quotes in Apex. And editing errors around every time OP attempts anything around that just keeps on extending the original question being asked. Thoughts? – Jayant Das Sep 27 '18 at 13:18
  • 1
    At this point, I agree with you. I think that more than one edit to a question is either an indication that an answer needs to be re-thought, or that OP is missing something else (like basic string construction in this case). I'm not sure that asking a new question about how to construct a string would be well-received though (it is a specific question, but research and documentation should cover that one, I'd think). Maybe this is worth discussing on meta. – Derek F Sep 27 '18 at 13:30

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.