• CalRam
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
In my project there is a need to download the salesforce attachment body in JAVA and encode the body and send it to another system.
I am using HttpEntity to download the attachment from salesforce
baseurl+/sobject/attachment/attachid/Body
The above call returns byte[]. 
I am encoding the byte array using below code in java.
Encoder encoder = new BASE64Encoder();
String encodedPdf = encoder.encode(response.getBody.toString.getBytes()); System.out.println(encodedPdf);

Now the encodedPDF when conveerted as file retuns blank file.
This is  case for PDF and DOC. But other format works.
Any input will be helpful.
  • April 26, 2021
  • Like
  • 0
global with sharing class GetObjectsAWS{
 
    @AuraEnabled

    public static List<ContentssArray> GetObject(){
   String jsonResponse = XMLParser.xmlToJson(res.getBody());
       // String json ='Contents:{"Key": "my-third-image.jpg", "LastModified": "2009-10-12T17:50:30.000Z", "ETag": "\"1b2cf535f27731c974343645a3985328\"", "Size": "64994", "StorageClass": "STANDARD_IA", "Owner": {"ID": "75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a", "DisplayName": "mtd@amazon.com"}}';
        system.debug('+++++Body is'+jsonResponse);
      
        Map<String,Object> jsonParsed =(Map<String,Object> ) JSON.deserializeUntyped(jsonResponse);
        Map<String,Object> content_collection = ( Map<String,Object>) jsonParsed.get('ListBucketResult');
        List<Object> ContentssArray =( List<Object> ) content_collection.get('Contents');
        
        for(Object inidividualEntries : ContentssArray){
            Map<String,Object> ind = (Map<String,Object> )inidividualEntries;
            System.debug('Key = '+ ind.get('Key'));
            System.debug('Size = '+ ind.get('Size'));
             
            System.debug('---------------------------------------------');
        }

   return ContentssArray;

    }
Trying to dispay the JSON response in the lightnign component. Hence returning LIST in the apex controller. But getting below errors:
Invalid type: ContentssArray
Illegal conversion from List<Object> to List<ContentssArray>
Thanks In Advance
  • April 19, 2020
  • Like
  • 0
In my project there is a need to download the salesforce attachment body in JAVA and encode the body and send it to another system.
I am using HttpEntity to download the attachment from salesforce
baseurl+/sobject/attachment/attachid/Body
The above call returns byte[]. 
I am encoding the byte array using below code in java.
Encoder encoder = new BASE64Encoder();
String encodedPdf = encoder.encode(response.getBody.toString.getBytes()); System.out.println(encodedPdf);

Now the encodedPDF when conveerted as file retuns blank file.
This is  case for PDF and DOC. But other format works.
Any input will be helpful.
  • April 26, 2021
  • Like
  • 0

Hi everyone,

I'm doing a rest call to retrieve attachments out of Salesforce. The first call would get the information of the attachment which has the URL to the body of the attachment. From there, I take the URL of the body and do another call to retrieve the contents of the attachment.

The calls are successful, however, when getting a response back, the response.getBody() return a string that does not translate to anything. When saving to a file or reuploading or encoding it, it always ends up to be gibberish or a broken file.

 

My questions are:

 what kind of content does the GET method return for the URL: https://SOME_ORG.salesforce.com/services/data/v20.0/sobjects/Attachment/SOME_ID/body

Am I missing anything to get the correct format? The headers that are being included in the final call for my attachment body are Content-Type, Authoriatization, and Accept

 

I also forgot to mention that postman is able to retrieve and display the content just fine. The issue is that the rest call returns a binary string and I am unable to translate it back to the actual content itself.