0

My current solution is working with following logic:

  • system is calling remote action from vf page
  • in the method there is a HttpRequest, system is calling external system, is sending file which will be processed
  • in the response system is getting processed file - response.getBodyAsBlob()
  • system is inserting file in Salesforce (custom object, ContentVersion, ContentDocumentLink)

and it is working as expected.

Now I am trying to use continuation pattern for the same logic: https://developer.salesforce.com/blogs/developer-relations/2015/02/apex-continuations-asynchronous-callouts-visualforce-pages.html

Below, I am sharing my code, there is:

  • remote action = processDocument with intance of Continuation class
  • callback method with response.getBodyAsBlob() and logic with inserting objects required by File - with contentVersion.VersionData = fileBody;

I am not getting any errors but inserted file, in my scenario it is PDF is corrupted. Am I missing something related to response.getBodyAsBlob() and asynchronous calls/continuation pattern? Please note that the same logic but without continuation pattern is working correctly and the system is getting the valid PDF file.

public with sharing class Continuation_Endpoint {

@RemoteAction
public static Object processDocument(String objectId) {
    HttpRequest req = createRequestForMyDocumentProcessing(new List<String>{objectId});     
    Continuation con = new Continuation(120);
    con.state = con.addHttpRequest(req);
    con.continuationMethod = 'callback';        

return con;  
}

public static Object callback(Object state) {
    HttpResponse response = Continuation.getResponse((String)state);

    Integer statusCode = response.getStatusCode();
    if (statusCode >= 2000) {
    return 'Continuation error: ' + statusCode;
    }
    Blob fileBody = response.getBodyAsBlob();
    if (fileBody != null) { 
        ContentVersion contentVersion = new ContentVersion();
        //custom code here
        contentVersion.VersionData = fileBody;
        contentVersion.Origin = 'H';
        insert contentVersion;

        ContentDocumentLink contentDocumentLink = new ContentDocumentLink();
        //custom code here
        insert contentDocumentLink;                        
}

return custom_info_here;
}

UPDATE: I made several tests and here are my scenarios:

  1. Calling external server with body request (req.setBodyAsBlob) which is properly combined pdf file and json - without using continuation pattern - PASS
  2. Scenario 1 with continuation pattern - FAIL with response error: not valid pdf file

  3. Calling external server with param as json ('url...?fields={"name":"john"}' and pdf file in the body - without using continuation pattern - PASS

  4. Scenario 3 with continuation pattern - FAIL - without response from external server because there wasn't successful call. In Salesforce debug logs don't see an entry to the callback method, don't see any errors either.

Please note that using EncodingUtil.urlEncode doesn't help.

Any clues?

0

the scenario 2 has been solved, calling external server with body request (req.setBodyAsBlob)with combined file and json is working as expected with continuation pattern.

I had a file with size > 1MB but we have limitations: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_continuation_limits.htm

Scenario 3 - is not working, even with files <1MB

Thanks!

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.