Last active
August 29, 2015 14:23
-
-
Save zaki-yama/79a3c4b665cb9c4d5fd5 to your computer and use it in GitHub Desktop.
[Salesforce]スケジュールApexでVisualforceをpdf送信
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class HttpCallout { | |
| @future(callout=true) | |
| public static void sendVF() { | |
| // 名前空間を指定していると /services/apexrest/[namespace]/sendPDFEmail となる | |
| String serviceUrl = '/services/apexrest/sendPDFEmail'; | |
| String endpoint = URL.getSalesforceBaseUrl().toExternalForm() + serviceUrl; | |
| HttpRequest req = new HttpRequest(); | |
| req.setEndpoint(endpoint); | |
| req.setMethod('POST'); | |
| req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId()); | |
| req.setHeader('Content-Type','application/json'); | |
| // パラメータを渡したい場合こうする | |
| ID oppId = '0061000000Y9L3H'; | |
| Map<String,String> postBody = new Map<String,String>(); | |
| postBody.put('oppId', oppId); | |
| String reqBody = JSON.serialize(postBody); | |
| req.setBody(reqBody); | |
| Http http = new Http(); | |
| HttpResponse response = http.send(req); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| global class ScheduledApex implements Schedulable { | |
| global void execute(SchedulableContext sc) { | |
| // ここに直接 Http コールアウトの処理を書くとエラーになる | |
| HttpCallout.sendVF(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @RestResource(urlMapping='/sendPDFEmail/*') | |
| global class SendPDFEmailRestService { | |
| @HttpPost | |
| global static void sendEmail(ID oppId) { | |
| Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); | |
| mail.setToAddresses(new List<String> { '[email protected]' }); | |
| mail.setSubject('Mail Subject'); | |
| mail.setPlainTextBody('Mail body'); | |
| PageReference pr = Page.CustomVfPage; | |
| pr.getParameters().put('id', oppId); | |
| pr.setRedirect(true); | |
| Blob b = pr.getContent(); | |
| Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); | |
| efa.setFileName('添付書類.pdf'); | |
| efa.setBody(b); | |
| mail.setFileAttachments(new List<Messaging.EmailFileAttachment> { efa }); | |
| Messaging.sendEmail(new List<Messaging.Email> { mail }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment