I have created the HTTP method using this post. But I'm unsure of how to test it out. Say if I run this in the Developer Console for testing how Can I link it to taking the image out of the Salesforce Dashboard and uploading it into the Document Folder so that the Image can be send upload else where?

I already have the URL to those Dashboard images that I want to upload

How do I link this to getting a image from Salesforce Dashboard and uploading it into the Document Folder?


public class testFBInt {

public void exInt() {

  HttpRequest req = new HttpRequest();
    req.setBody(buildHttpQuery(new Map<String, String> {
        'grant_type' => 'password',
        'username' => 'username',
        'password' => 'password',
        'client_id' => 'Consumer_id',
        'client_secret' => 'Consumer_key'


  static String buildHttpQuery(Map<String, String> queryParams) {
    if (queryParams.isEmpty()) {
        return '';

    String[] params = new String[] {};
    for (String k : queryParams.keySet()) {
        String v = EncodingUtil.urlEncode(queryParams.get(k), 'UTF-8');

        params.add(String.format('{0}={1}', new String[] { k, v }));

    return String.join(params, '&');

  • Why are you making a callout to upload it to Document? Is it in different org? Apr 20 '17 at 9:49
  • I need to upload the image file that is from the Dashboard and then send the image file to Facebook that is why I'm trying to upload it as a Document unless here is another way to send a specific image of the graph from the Dashboard to Facebook?
    – Luke Au
    Apr 20 '17 at 9:51
  • There is 2 things here: 1. To save the image as Document, you dont need callout for this. 2. Sending the image to facebook, it needs a callout. Your question doesn't talk about callout to facebook. Apr 20 '17 at 9:53
  • But i need a call out for it in the scheduler don't I?
    – Luke Au
    Apr 20 '17 at 9:54
  • If its in the same org, you can insert document record without a callout even from a scheduler. But to send the image to facebook, you need a callout. Apr 20 '17 at 9:58

Based on your statement

I already have the URL to those Dashboard images that I want to upload

From here, there are 3 task you talked about. All these can be done in a scheduler job.

One: Fetching the image body as base64data.

Get the access token first. Reference: Alan's answer here

Lets say your URL is in variable - dashboardImageURL.

String base64Data;
httprequest req = new httprequest();
req.setHeader('Authorization', 'OAuth ' + access_token);
http http = new http();
httpresponse res = http.send(req);
Blob image = res.getBodyAsBlob();
base64Data =  EncodingUtil.base64Encode(image);

Two: Saving the image as Document in current org.

Document docatt = new Document();
docatt.Name = 'Chart 1.jpg';
docatt.Body = EncodingUtil.base64Decode(base64Data);
docatt.FOLDERID = '00l6F231005tqGi'; //Your folder id
docatt.IsPublic = true;

insert docatt;

You can have a scheduler job to run at any specific time with these 2 snippets to save the image from the URL.

Three: Sending the image to Facebook.

For this you will need to make another callout to facebook with the image body set in the httprequest body. This is not a part of the question, so I am just over-lining it. You can write it in a separate scheduler if you want to send it at a different time or in the same scheduler job.

  • 1
    But where do I get that Session Id from my code in the post? For the scheduler
    – Luke Au
    Apr 20 '17 at 10:20
  • That's a good point. For that you have the code with you in your question. Get the access token using that and then continue. I have updated my answer. Apr 20 '17 at 10:33

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.