+ Start a Discussion
marys pindamarys pinda 

File URL from Document

Hello,
I have a file in a URL,
I would like to get  the content of  this file and create a Document.
I made this code, but it does not work, what I forgot to do?

global class UploadFileTest implements System.Schedulable {
 Public Document mydoc;         
    global void execute(SchedulableContext sc) {
        test();
    }
    
public void test(){

Http h = new Http();
HttpRequest req = new HttpRequest();
string firstImageURL = '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();

mydoc = new Document();
mydoc.Name = 'VOCAZA UPDATE';
mydoc.Type = 'csv';
mydoc.folderid = UserInfo.getUserId();
mydoc.Body = image;
insert mydoc;       
}
}

Thank you
kevin Carotherskevin Carothers
Hi

I didn't use the schedulable context, but I just re-wrote your task a little so I could just run it in the anonymous context, and it ran fine;

public class  UploadFileTest {    
   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 = 'VOCAZA UPDATE';
    mydoc.Type = 'csv';
    mydoc.folderid = UserInfo.getUserId();
    mydoc.Body = image;
    insert mydoc;       
    }
}

...Also, I had to specift the protocol (http:)   and specify the endpoint in the security controls.

marys pindamarys pinda
Thank you.
From what I saw you did not change anything in my code, just took the Schedulable.
I would like to use the Schedulable to automate this action to create documents from files in URL.

I used your code with Schedulable, but the document is not created.
What should I do?
Somebody help me.

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,