3

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 } );
        }
4

The "REST" interface in Apex Code doesn't support pipelining, SPDY, or asynchronous callbacks, so there's no way to retrieve more than one file per request natively. If you were feeling bold, you could probably develop a custom service (externally) that you could call out to as a proxy that could retrieve multiple resources, string them together in a single response, and feed that back into salesforce.com. Short of doing this, there's no other way you're going to get more than one file per HttpRequest/HttpResponse pair. You can have up to 10 callouts per transaction, so you can retrieve up to ten files natively (with a total collective size of 3MB). Performing a loop would be the correct approach to this.

| improve this answer | |
  • I've come to the same conclusion. Read the docs and they mentioned the 10 callouts and looping. I've decided to go another way. I'm just going to send the file locations with a hash to the webservice and include the links into one email. So 1 callout will return an array of file locations. – Ricky Hewitt Oct 18 '13 at 20:40
  • Saves bandwidth, too, I'd guess, in case they didn't need to download them to their mobile device, etc. – sfdcfox Oct 18 '13 at 20:45
  • Yea, especially since the files have the potential to grow exponentially as they are archival in nature. I also won't have to worry about the emails being bounced because of an organization's max email attachment size. So all around a better route to go. – Ricky Hewitt Oct 18 '13 at 20:51
  • Also, if your planning to be at DreamForce this year, beers on me. You've helped out on most of the questions I've asked her on sf.Stack. (which isn't that many) And I've used a lot of answers you provided to others. Thanks for all the help! :) – Ricky Hewitt Oct 18 '13 at 20:56
  • I can never seem to get time away from work for that. Eight years I've meant to go, across four different jobs, and each year there's either a lack of funds, vacation time, or both. Ah well. Here's to DF 2014, perhaps. – sfdcfox Oct 18 '13 at 21:02

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.