-1

I have to create a RestResource apex class which should let the user to download a pdf from salesforce.

I have written the following code, but I am not sure how to proceed further to let file downloaded in local storage.

Basically what I want is that, I want an external application call this method using Rest. I tried to return VersionData, but salesforce would accept the return type to be blob.

@RestResource(urlMapping='/AccountPdf/*')
Global class restSendPDF {
    @HttpGet 
    global static blob getAccountPdf()  
    {                                                                                              
        RestRequest req = RestContext.request;       
        RestResponse res = RestContext.response;                                             
        String accId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);                                                                                      
        Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accId ];  
        ContentDocumentLink cdl = [Select Id,ContentDocumentId from ContentDocumentLink where LinkedEntityId = :accId ];

       ContentVersion cv = [Select id, VersionData from ContentVersion where ContentDocumentId = :cdl.ContentDocumentId and IsLatest = true];
        //not sure what to do 
        Http h = new Http(); 
        HttpRequest request = new HttpRequest(); 
       request.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v42.0/sobjects/ContentVersion/'+cv.VersionData+'/VersionData'); 
        request.setMethod('GET');  
        request.setHeader('Content-Type', 'application/json'); 
        HttpResponse response = h.send(request); 
        //not sure what to further                                                                                         
    }
}

When I put the following url in workbench I get the PDF file downloaded /services/data/v58.0/sobjects/ContentVersion/0681wxxxxxxx/VersionData

4
  • Welcome to SFSE! Please take the tour and read How to Ask. To clarify, are you expecting to call this from outside of Salesforce in another application? If so you should simply be able to return the VersionData and have that application handle downloading it, not have Salesforce send a request as you've included in your code. Could you please edit your question to add more details. Aug 16, 2023 at 21:29
  • @CallumMacErlich, Yes, I want an External application to all this method using rest. I tried returning the VersionData, but salesforce would not accept the return type of Blob. Aug 17, 2023 at 7:24
  • You can return if a Blob by setting it as the responseBody of the RestReponse. Dana's answer to this question should help. Aug 17, 2023 at 21:22
  • @CallumMacErlich It worked thanks. Aug 22, 2023 at 14:21

1 Answer 1

0

Adding an answer to this question for clarity when other view it.

To return the content of the file we can simply add it to the responseBody of the RestResponse. This is of type Blob which is what is required for this content.

An example of how this would work is:

@RestResource(urlMapping='/AccountPdf/*')
Global class restSendPDF {
    @HttpGet 
    global static void getAccountPdf()  
    {                                                                                              
        RestRequest req = RestContext.request;       
        RestResponse res = RestContext.response;                                             
        String accId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);                                                                                      
        Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accId ];  
        ContentDocumentLink cdl = [Select Id,ContentDocumentId from ContentDocumentLink where LinkedEntityId = :accId ];

        ContentVersion cv = [Select id, VersionData from ContentVersion where ContentDocumentId = :cdl.ContentDocumentId and IsLatest = true];
        res.responseBody = Blob.valueOf(cv.VersionData);
        res.statusCode = 200;
    }
}

Note: I haven't included any error handling that should be included in this sample code.

You must log in to answer this question.

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