0

I am working on an integration where I am provided a URL + document ID, which should send back a .pdf, I retrieve it using a GET, and then I take the blob of the pdf and create a ContentDocument and then a RelatedContent record so that it can be attached to our Quote in CPQ.

The file itself gets created and linked but it is a corrupt 1kb file that can't be previewed so I assume the issue is how I am fetching it.

Code:

        HTTP http = new HTTP();
        HTTPRequest request = new HTTPRequest();
        //request.setEndpoint(Our endpoint here)

        request.setHeader('contentType', 'application/pdf');
        request.setMethod('GET');           
                
        HTTPResponse resp = http.send(request); 
        
        if(resp.getStatusCode() == 200 && resp.getbody() != null)
        {             
            Blob body = resp.getBodyAsBlob();
            
            //Create Document
            ContentVersion cv = new ContentVersion();
            cv.Title = 'testDocument ' + id; 
            cv.PathOnClient =  id + '_test_Document.pdf';
            cv.VersionData = body;
            cv.IsMajorVersion = true;
            insert cv;
            
            //Get Content Document ID so we can link it 
            Id conDocId = [SELECT ContentDocumentId FROM 
                           ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
            
            
            //Create ContentDocumentLink 
            ContentDocumentLink cdl = New ContentDocumentLink();
            cdl.LinkedEntityId = id;
            cdl.ContentDocumentId = conDocId;
            cdl.shareType = 'V';
            insert cdl;
            
            //finally, add it to the quote page for use
            SBQQ__RelatedContent__c rc = new SBQQ__RelatedContent__c();
            rc.OwnerID = '0055x00000ArBNCAA3';
            rc.Name =  'test Document';
            rc.SBQQ__ExternalID__c = conDocId;
            rc.SBQQ__Quote__c = id;
            rc.SBQQ__Required__c = false;
            
            insert rc;

I debugged the response and I see a very long stream of what looks like encrypted text so I am successfully fetching it. I just am not storing it properly. Can someone assist with how I save this .pdf so that it can be properly previewed and attached in CPQ Quotes?

Thanks

1
  • 1
    Would you be able to provide the endpoint so we can see if we can recreate it ourselves? Aug 2, 2023 at 19:32

1 Answer 1

0

The proper way might depend on how your file is returned by the system you are connecting with. If it has some kind of redirect set up, then you won't be able to do this with Apex (as far as I know).

You should get a direct link to your document and then use the data from that endpoint in a Blob to save to a content file. The rest of your code looks fine, but we don't see how the file is encoded.

It might present itself to you in a cryptic way because it might be a Base64 encoded string when you fetch from your endpoint, we don't know. If that's the case, you'll have to decode it first and then store the blob result in the content file:

String encodedFile = 'the cryptic base64 string from the endpoint...';
Blob decodedBlob = EncodingUtil.base64Decode(encodedFile);

contentVersion.VersionData = decodedBlob;

You must log in to answer this question.

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