1

I'm trying to retrieve my VisualForce Page that I have already Render as a PDF using my Apex Class so I can store the page as a PDF inside the document folder.

But, I keep on having an Error when testing the Code. Can someone help me out with this?


Debug Log Error

enter image description here


Developer Console Error

enter image description here


Apex Class Code

public class testFBInt {

     public void exInt() {
        //converting to base64
        String base64Data = '';
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://c.ap4.visual.force.com/apex/Dashboard_PDF'); 
        req.setMethod('GET');
        Http http = new Http();
        HttpResponse res = http.send(req);
        Blob image = res.getBodyAsBlob();
        base64Data =  EncodingUtil.base64Encode(image);
        System.debug('@@ base64 @@' + base64Data );

    //inserting into documents
    Document doc = new Document();
    doc.Name = 'SF_Dashboard.pdf';
    doc.body = EncodingUtil.base64Decode(base64Data);
    doc.folderId = '00l900000027wOx'; //your folder id
    doc.IsPublic = true;
    doc.Description = 'Salesforce Dashboard Report -' + String.valueOf(date.today().format());

    insert doc;
    System.debug('@@ doc @@' +doc);

    //retrieving the file details
    List<Document> lstfbdoc = [Select Id, Name, Description, Type FROM Document Where Name =: 'SF_Dashboard.pdf'];
    }
}

VisualForce Page Code

<apex:page name="Custom_Dash_Board" renderAs="PDF" showHeader="true" sidebar="false" title="Custom DashBoard">
<html>

<h2 >Sales</h2> 
<img src="https://ap4.salesforce.com/servlet/servlet.ChartServer?rsid=0FL6F000002dBX3&ruid=0056F00000746E0&did=01Z6F000001DwLb&s=7&fs=10&tfg=12&tfs=-16777063&explode=0&c=gauge&cs=0&title=Closed+Sales+To+Date&eh=no&compo=yes&fg=-16777216&bg1=-1&bg2=-3355393&bgdir=2&dl1=Account+Name&dl2=&l=2&sax=yes&Yman=no&nc=0&actUrl=%2F00O6F00000Aug7k%3Fdbw%3D1&sd=1&scv=no&sct=yes&spt=no&bd=yes&cu=SGD&ab=X&u=0&vt=0&ab2=Y&u2=1&vt2=2&vl0=Sum+of+Amount&spoc=no&topn=no&gm=0.0&gc0=-6750208&gm0=500000.0&gc1=-13210&gm1=1000000.0&gc2=-16738048&gm2=1500000.0&sona=0&refreshts=1491383581000" />

<h2 >Historical Snapshot</h2>
<img src="https://ap4.salesforce.com/servlet/servlet.ChartServer?rsid=0FL6F000002dBXF&ruid=0056F00000746E0&did=01Z6F000001DwLb&s=6&fs=10&tfg=12&tfs=-16777063&explode=0&c=column&cs=2&title=&eh=no&compo=yes&fg=-16777216&bg1=-1&bg2=-3355393&bgdir=2&dl1=As+of+Date&dl2=Historical+Stage&l=2&sax=yes&Yman=no&nc=0&actUrl=%2F00O6F00000Aug7w%3Fdbw%3D1&sd=1&scv=no&sct=no&spt=no&bd=yes&cu=SGD&ab=X&u=0&vt=0&ab2=Y&u2=0&vt2=2&vl0=Sum+of+Historical+Amount&spoc=no&topn=no&gc0=-4041644&gc1=-4013484&gc2=-11222444&sona=0&refreshts=1491458466000" />

</html>
</apex:page>
2

The error's coming from here:

base64Data =  EncodingUtil.base64Encode(image);

In your HttpCalloutMock implementation, you need to remember to call setBodyAsBlob, since you're calling getBodyAsBlob.

class FBIntCalloutMock implements HttpCalloutMock {
  public HttpResponse respond(HttpRequest req) {
    HttpResponse res = new HttpResponse();
    res.setBodyAsBlob(Blob.valueOf('sample image content'));
    return res;
  }
}

You need to remember that HttpCalloutMock is not a real callout, and so is only as explicitly loaded with information as you specify in the implementation.

  • I tried editing it to this String base64Data = ''; HttpRequest req = new HttpRequest(); req.setBodyAsBlob(Blob.valueOf('https://c.ap4.visual.force.com/apex/Dashboard_PDF')); //req.setEndpoint('https://c.ap4.visual.force.com/apex/Dashboard_PDF'); //req.setMethod('GET'); Http http = new Http(); HttpResponse res = http.send(req); Blob image = res.getBodyAsBlob(); base64Data = EncodingUtil.base64Encode(image); But it still doesn't work – Luke Au Apr 10 '17 at 6:50
  • @LukeAu Your code was fine--your unit test is what needs to be updated. – sfdcfox Apr 10 '17 at 6:53
  • Thanks the error is solve but now the file is not being upload to the folder – Luke Au Apr 10 '17 at 7:06

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.