+ Start a Discussion
marys pindamarys pinda 

Create Document Using Schedulable

Hello,
I have a code which gets information from a file-URL and creates a Document within the Salesforce.
I wish he created the document on the 10 am and then I used Schedulable.
But he has a problem and does not create the document.
Someone help me please!.

global class Test implements System.Schedulable {     
    global void execute(SchedulableContext sc) {
        getFile();
    }
    
 public void  getFile(){
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    string firstImageURL = 'http://download.finance.yahoo.com/d/quotes.csv?s=AAPL,IBM&f=sl1d1t1c1ohgv&e=.csv';
    firstImageURL = firstImageURL.replace(' ', '%20');
    req.setEndpoint(firstImageURL);
    req.setMethod('GET');
    req.setHeader('Content-Type', 'text/csv');
    req.setCompressed(true);
    req.setTimeout(60000);
                  
    HttpResponse res = null;
    res = h.send(req);
    string responseValue = '';
    responseValue = res.getBody();
    system.debug('Response Body for File: ' + responseValue);
    blob image = res.getBodyAsBlob();
 
    Document mydoc = new Document();
    mydoc.Name = 'update today';
    mydoc.Type = 'csv';
    mydoc.folderid = UserInfo.getUserId();
    mydoc.Body = image;
    insert mydoc;       
    }
}
Thank you,
Vinit_KumarVinit_Kumar
Can you check what's coming the response,I mean the debug value of resonse ??
marys pindamarys pinda
NOTHING!
Nothing happens ...
Vinit_KumarVinit_Kumar
so it seems you are not getting any callout response.Can you try making callout using Dev console and see if it results the response or not ??
marys pindamarys pinda
I do not know how I can do this.

The code is correct, right?
Also do not understand where is the error ...
Vinit_KumarVinit_Kumar
Go to your name on the top and open the Developer console window.Then,execute the below code there:-
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint('http://download.finance.yahoo.com/d/quotes.csv?s=AAPL,IBM&f=sl1d1t1c1ohgv&e=.csv');
        req.setHeader('Content-Type', 'text/csv');
        req.setTimeout(60*1000);
        req.setMethod('GET');
        Http h = new Http();
        String resp;
        HttpResponse res = h.send(req);
        resp = res.getBody();
		
		system.debug('##########' + resp);

marys pindamarys pinda
I tried, but:

30.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
Execute Anonymous: public class testeReturn{
Execute Anonymous:  public void  getFile(){
Execute Anonymous:     Http h = new Http();
Execute Anonymous:     HttpRequest req = new HttpRequest();
Execute Anonymous:     string firstImageURL = 'http://www.unifae.br/publicacoes/pdf/gestao/marketing.pdf';
Execute Anonymous:     firstImageURL = firstImageURL.replace(' ', '%20');
Execute Anonymous:     req.setEndpoint(firstImageURL);
Execute Anonymous:     req.setMethod('GET');
Execute Anonymous:     req.setHeader('Content-Type', 'text/csv');
Execute Anonymous:     req.setCompressed(true);
Execute Anonymous:     req.setTimeout(60000);
Execute Anonymous:                   
Execute Anonymous:     HttpResponse res = null;
Execute Anonymous:     res = h.send(req);
Execute Anonymous:     string responseValue = '';
Execute Anonymous:     responseValue = res.getBody();
Execute Anonymous:     system.debug('Response Body for File: ' + responseValue);
Execute Anonymous:     blob image = res.getBodyAsBlob();
Execute Anonymous:  
Execute Anonymous:      system.debug('##########' + image);
Execute Anonymous:          
Execute Anonymous:     }
Execute Anonymous: }
14:09:06.029 (29657257)|EXECUTION_STARTED
14:09:06.029 (29672516)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
14:09:06.030 (30007867)|HEAP_ALLOCATE|[71]|Bytes:3
14:09:06.030 (30068842)|HEAP_ALLOCATE|[76]|Bytes:152
14:09:06.030 (30103946)|HEAP_ALLOCATE|[272]|Bytes:408
14:09:06.030 (30143913)|HEAP_ALLOCATE|[285]|Bytes:408
14:09:06.030 (30181816)|HEAP_ALLOCATE|[379]|Bytes:48
14:09:06.030 (30228499)|HEAP_ALLOCATE|[131]|Bytes:6
14:09:06.030 (30362488)|STATEMENT_EXECUTE|[1]
14:09:06.030 (30371772)|STATEMENT_EXECUTE|[1]
14:09:06.750 (30425057)|CUMULATIVE_LIMIT_USAGE
14:09:06.750|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

14:09:06.750|CUMULATIVE_LIMIT_USAGE_END

14:09:06.030 (30496278)|CODE_UNIT_FINISHED|execute_anonymous_apex
14:09:06.030 (30509388)|EXECUTION_FINISHED

Vinit_KumarVinit_Kumar
Did u run this class in Dev console ??

I don't see this being exected..
marys pindamarys pinda
I used the link from a CSV file and it looks like he takes yes and why not create the file?

TESTED:
public class testeReturn{
 public void  getFile(){
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    string firstImageURL = 'http://download.finance.yahoo.com/d/quotes.csv?s=AAPL,IBM&f=sl1d1t1c1ohgv&e=.csv';
    firstImageURL = firstImageURL.replace(' ', '%20');
    req.setEndpoint(firstImageURL);
    req.setMethod('GET');
    req.setHeader('Content-Type', 'text/csv');
    req.setCompressed(true);
    req.setTimeout(60000);
                  
    HttpResponse res = null;
    res = h.send(req);
    string responseValue = '';
    responseValue = res.getBody();
    system.debug('Response Body for File: ' + responseValue);
    blob image = res.getBodyAsBlob();
 
     system.debug('##########' + image);
         
    }
}
RESULT:

30.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
Execute Anonymous: public class testeReturn{
Execute Anonymous:  public void  getFile(){
Execute Anonymous:     Http h = new Http();
Execute Anonymous:     HttpRequest req = new HttpRequest();
Execute Anonymous:     string firstImageURL = 'http://download.finance.yahoo.com/d/quotes.csv?s=AAPL,IBM&f=sl1d1t1c1ohgv&e=.csv';
Execute Anonymous:     firstImageURL = firstImageURL.replace(' ', '%20');
Execute Anonymous:     req.setEndpoint(firstImageURL);
Execute Anonymous:     req.setMethod('GET');
Execute Anonymous:     req.setHeader('Content-Type', 'text/csv');
Execute Anonymous:     req.setCompressed(true);
Execute Anonymous:     req.setTimeout(60000);
Execute Anonymous:                  
Execute Anonymous:     HttpResponse res = null;
Execute Anonymous:     res = h.send(req);
Execute Anonymous:     string responseValue = '';
Execute Anonymous:     responseValue = res.getBody();
Execute Anonymous:     system.debug('Response Body for File: ' + responseValue);
Execute Anonymous:     blob image = res.getBodyAsBlob();
Execute Anonymous: 
Execute Anonymous:      system.debug('##########' + image);
Execute Anonymous:         
Execute Anonymous:     }
Execute Anonymous: }
14:18:40.027 (27200899)|EXECUTION_STARTED
14:18:40.027 (27211962)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
14:18:40.027 (27523181)|HEAP_ALLOCATE|[71]|Bytes:3
14:18:40.027 (27570380)|HEAP_ALLOCATE|[76]|Bytes:152
14:18:40.027 (27606215)|HEAP_ALLOCATE|[272]|Bytes:408
14:18:40.027 (27651122)|HEAP_ALLOCATE|[285]|Bytes:408
14:18:40.027 (27694341)|HEAP_ALLOCATE|[379]|Bytes:48
14:18:40.027 (27750211)|HEAP_ALLOCATE|[131]|Bytes:6
14:18:40.027 (27901232)|STATEMENT_EXECUTE|[1]
14:18:40.027 (27910863)|STATEMENT_EXECUTE|[1]
14:18:40.139 (27962974)|CUMULATIVE_LIMIT_USAGE
14:18:40.139|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

14:18:40.139|CUMULATIVE_LIMIT_USAGE_END

14:18:40.028 (28017586)|CODE_UNIT_FINISHED|execute_anonymous_apex
14:18:40.028 (28034274)|EXECUTION_FINISHED


What do I do? Thank you
Vinit_KumarVinit_Kumar
It seems that you are receiving the request.Now,try insert Document record as you were doing earlier in the same code.
marys pindamarys pinda
OK... I tried (Open Execute Enonymous Window):

TESTED:

public class Test {     

    
 public void  getFile(){
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    string firstImageURL = 'http://download.finance.yahoo.com/d/quotes.csv?s=AAPL,IBM&f=sl1d1t1c1ohgv&e=.csv';
    firstImageURL = firstImageURL.replace(' ', '%20');
    req.setEndpoint(firstImageURL);
    req.setMethod('GET');
    req.setHeader('Content-Type', 'text/csv');
    req.setCompressed(true);
    req.setTimeout(60000);
                  
    HttpResponse res = null;
    res = h.send(req);
    string responseValue = '';
    responseValue = res.getBody();
    system.debug('Response Body for File: ' + responseValue);
    blob image = res.getBodyAsBlob();
 
    Document mydoc = new Document();
    mydoc.Name = 'update today';
    mydoc.Type = 'csv';
    mydoc.folderid = UserInfo.getUserId();
    mydoc.Body = image;
    insert mydoc;       
    }
}

RESULT:
0.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
Execute Anonymous: public class testeReturn {    
Execute Anonymous:
Execute Anonymous:    
Execute Anonymous:  public void  getFile(){
Execute Anonymous:     Http h = new Http();
Execute Anonymous:     HttpRequest req = new HttpRequest();
Execute Anonymous:     string firstImageURL = 'http://download.finance.yahoo.com/d/quotes.csv?s=AAPL,IBM&f=sl1d1t1c1ohgv&e=.csv';
Execute Anonymous:     firstImageURL = firstImageURL.replace(' ', '%20');
Execute Anonymous:     req.setEndpoint(firstImageURL);
Execute Anonymous:     req.setMethod('GET');
Execute Anonymous:     req.setHeader('Content-Type', 'text/csv');
Execute Anonymous:     req.setCompressed(true);
Execute Anonymous:     req.setTimeout(60000);
Execute Anonymous:                  
Execute Anonymous:     HttpResponse res = null;
Execute Anonymous:     res = h.send(req);
Execute Anonymous:     string responseValue = '';
Execute Anonymous:     responseValue = res.getBody();
Execute Anonymous:     system.debug('Response Body for File: ' + responseValue);
Execute Anonymous:     blob image = res.getBodyAsBlob();
Execute Anonymous: 
Execute Anonymous:     Document mydoc = new Document();
Execute Anonymous:     mydoc.Name = 'update today';
Execute Anonymous:     mydoc.Type = 'csv';
Execute Anonymous:     mydoc.folderid = UserInfo.getUserId();
Execute Anonymous:     mydoc.Body = image;
Execute Anonymous:     insert mydoc;      
Execute Anonymous:     }
Execute Anonymous: }
15:16:38.034 (34918563)|EXECUTION_STARTED
15:16:38.034 (34928206)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
15:16:38.035 (35180319)|HEAP_ALLOCATE|[71]|Bytes:3
15:16:38.035 (35216635)|HEAP_ALLOCATE|[76]|Bytes:152
15:16:38.035 (35238502)|HEAP_ALLOCATE|[272]|Bytes:408
15:16:38.035 (35265174)|HEAP_ALLOCATE|[285]|Bytes:408
15:16:38.035 (35290284)|HEAP_ALLOCATE|[379]|Bytes:48
15:16:38.035 (35328947)|HEAP_ALLOCATE|[131]|Bytes:6
15:16:38.035 (35359684)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:2
15:16:38.035 (35433118)|STATEMENT_EXECUTE|[1]
15:16:38.035 (35437668)|STATEMENT_EXECUTE|[1]
15:16:38.097 (35481512)|CUMULATIVE_LIMIT_USAGE
15:16:38.097|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

15:16:38.097|CUMULATIVE_LIMIT_USAGE_END

15:16:38.035 (35525801)|CODE_UNIT_FINISHED|execute_anonymous_apex
15:16:38.035 (35534566)|EXECUTION_FINISHED

What do I do? Thank you