0

Kindly help me in resolving the issue. I am not able to parse the response from the json data. I need to get the Body part of the attachment. It's giving the error 'Required field missing' a.Body.
I am getting the reponse from server thru hRes.getBody(),which is fine.
Apex :-

    Http h2 = new Http();
   final PageReference urlPg= new PageReference('https://xxxxxx-dev-ed.my.salesforce.com/services/data/v39.0/query/');
         urlPg.getParameters().put('q','Select Body,ParentId from Attachment where ParentId=\'0017F000003o4gJ\''); 
           String uri  = urlPg.getUrl();

        HttpRequest req2= new HttpRequest();
           req2.setEndpoint(uri);
           req2.setMethod('GET');
           req2.setHeader('Authorization', 'OAuth ' + newaccessToken);

        HttpResponse hRes = h2.send(req2);
    system.debug('1111 '+hRes.getBody());       
showreshttp =hRes.getBody();

            JSONParser parser = JSON.createParser(hRes.getBody());
            while (parser.nextToken() != null) {
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
                    String fieldName = parser.getText();
                    parser.nextToken();
                    if(fieldName == 'Body') {
                    string    data= parser.getText();
                    }
                }
              }

                    Attachment a= new Attachment();
                     a.Name='xyz';
                     a.Body=  EncodingUtil.base64Decode(data);
                     a.ParentId='0010I00001odnie';
                      insert a;
0

The REST API doesn't return the Body (a Blob field) directly back through the API for performance reasons. Instead, you need to call the Blob Retrieve endpoint from the value derived from Body, which will look something like /services/data/v43.0/sobjects/Attachment/OOPXXXXXXXXXXXXXXX/Body. The reason why you're getting the error is because the body couldn't be decoded, because it's not a base64 string, just a normal URL fragment.

For your body, you'll need to do something like this:

HttpRequest bodyRequest = new HttpRequest();
bodyRequest.setEndpoint(new Url(new url(urlPg.getUrl()), data).toExternalForm());
bodyRequest.setMethod('GET');
bodyRequest.setHeader('Authorization', 'OAuth '+newAccessToken);
HttpResponse bodyResponse = new http().send(bodyRequest);
                Attachment a= new Attachment();
                 a.Name='xyz.png';
                a.Body=bodyResponse.getBodyAsBlob();//bodyResponse.getBody()
                 a.ParentId='001xxxx0001odnie';
                  insert a;
| improve this answer | |
  • thnx a lot ......i tried this earlier. In that case what should i use in a.Body? – vineet Sep 21 '18 at 10:17
  • is a.body=hRes.getBody() correct? – vineet Sep 21 '18 at 10:18
  • @vineet No, you have to make another callout. – sfdcfox Sep 21 '18 at 10:21
  • See i tried like this a.Body=Blob.ValueOf(hRes.getBody()); Some garbge value is getting saved in attachments now,but not the exact image file. Neways will make another callout to check. Btw, the URL in another callout ,I have to use yours' or mine url. – vineet Sep 21 '18 at 10:28
  • currently i used ur url. – vineet Sep 21 '18 at 10:29

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.