I have working code below that initiates 1 HTTPRequest and returns 1 File then emails that one file to multiple users. However, I am now looking to (if possible) initiate 1 HTTPRequest and return multiple files.
I could loop over the HTTPRequest changing the file location however that does not get me to 1 HTTPRequest to multiple files.
I have provided the email portion of my code for completion.
My SFDC version is Enterprise.
Any help would be greatly appreciated.
String location = 'Reports/'.$fileName.'.xls'
// Instantiate a new http object
Http h = new Http();
String url = 'http://www.mysite.com/'+location;
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
req.setEndpoint(url);
req.setMethod('GET');
// Send the request, and return a response
Boolean error = false;
String errorMessage = '';
Blob theData = Blob.valueof(errorMessage);
try{
res = h.send(req);
}catch(system.CalloutException e){
system.debug('Callout Error: '+e);
system.debug(res.toString());
error = true;
errorMessage = res.toString();
}
if(error != true){
theData = res.getBodyAsBlob();
for(Contact theUser : emailRecipients){
Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
m.setTargetObjectId(theUser.id);
Messaging.EmailFileAttachment a = new Messaging.EmailFileAttachment();
a.setBody(theData);
a.setFileName(fileName);
a.setInline(false);
m.setFileAttachments(new Messaging.EmailFileAttachment[] { a } );
m.setSubject('Updated List - '+fileName);
m.setHtmlBody(emailBodyPrePend+staticEmailBody);
m.setPlainTextBody(emailBodyPrePend+staticEmailBody);
m.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { m } );
}
//send to useres
for(User theInternalUser : emailUserRecipients){
Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
m.setTargetObjectId(theInternalUser.id);
Messaging.EmailFileAttachment a = new Messaging.EmailFileAttachment();
a.setBody(theData);
a.setFileName(fileName);
a.setInline(false);
m.setFileAttachments(new Messaging.EmailFileAttachment[] { a } );
m.setSubject('Updated List - '+fileName);
m.setHtmlBody(emailBodyPrePend+staticEmailBody);
m.setPlainTextBody(emailBodyPrePend+staticEmailBody);
m.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { m } );
}
}
else{
Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
m.setTargetObjectId(errorUser.id);
Messaging.EmailFileAttachment a = new Messaging.EmailFileAttachment();
a.setBody(theData);
a.setFileName(fileName);
a.setInline(false);
m.setFileAttachments(new Messaging.EmailFileAttachment[] { a } );
m.setSubject('ERROR OCCURED WHEN SENDING NOTICES: '+fileName);
m.setPlainTextBody('ERROR MESSAGE: '+errorMessage);
m.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { m } );
}