Skip to content

Instantly share code, notes, and snippets.

@zaki-yama
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save zaki-yama/a98012d00114e616e37b to your computer and use it in GitHub Desktop.

Select an option

Save zaki-yama/a98012d00114e616e37b to your computer and use it in GitHub Desktop.
[Salesforce]スケジュール済みApexでVisualforceページをpdf化
public class ComponentController {
public String opportunityId {get; set;}
public Opportunity getOpp() {
System.debug(LoggingLevel.INFO, this.opportunityId);
return [
SELECT
Name,
(SELECT Product2.Name, UnitPrice, Quantity FROM OpportunityLineItems)
FROM Opportunity
WHERE Id = :this.opportunityId
][0];
}
}
global class ScheduledApex implements Schedulable {
global void execute(SchedulableContext sc) {
// 本来は SOQL などで取得する
ID oppId = '0061000000Y9L3H';
EmailTemplate template = [
SELECT
Name, Developername, Body
FROM EmailTemplate
WHERE DeveloperName = 'VfEmailTemplate' LIMIT 1
];
System.debug(LoggingLevel.INFO, UserInfo.getUserId());
System.debug(LoggingLevel.INFO, template.Name);
System.debug(LoggingLevel.INFO, template.DeveloperName);
System.debug(LoggingLevel.INFO, template.Body);
Messaging.SingleEmailMessage mail = new messaging.SingleEmailMessage();
// mail.setToAddresses(new List<String> { '[email protected]' });
mail.setTargetObjectId(UserInfo.getUserId());
mail.setTemplateId(template.id);
mail.setWhatId(oppId);
mail.setSaveAsActivity(false);
System.debug(LoggingLevel.INFO, mail.plainTextBody);
Messaging.sendEmail(new List<Messaging.Email> { mail });
}
}
<apex:component controller="ComponentController" access="global" >
<apex:attribute name="oppId" type="String" assignTo="{!opportunityId}" description="" />
<html>
<head>
<style>
@page {
size: 8.27in 11.69in;
margin: 10px;
}
body {
font-family: 'Arial Unicode MS';
}
.opportunityName {
width: 100%;
text-align: center;
margin-bottom: 10px;
font: 20pt;
}
.opportunityItems th {
text-align: center;
background-color: #cccccc;
}
.opportunityItems .unitPrice, .quantity {
text-align: right;
}
</style>
</head>
<body>
<div class="opportunityName">
{!opp.name}
</div>
<div class="opportunityItems">
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<th width="40%"> 商品名 </th>
<th width="30%"> 単価 </th>
<th width="30%"> 数量 </th>
</tr>
<apex:repeat value="{!opp.opportunityLineItems}" var="lineItem" >
<tr>
<td> {!lineItem.product2.name} </td>
<td class="unitPrice"> {!lineItem.unitPrice} </td>
<td class="quantity"> {!lineItem.quantity} </td>
</tr>
</apex:repeat>
</table>
</div>
</body>
</html>
</apex:component>
<messaging:emailTemplate subject="test" recipientType="Contact" relatedToType="Opportunity">
<messaging:plainTextEmailBody >
おめでとうございます!
これが新しい Visualforce メールテンプレートです。
</messaging:plainTextEmailBody>
<messaging:attachment renderAs="PDF" filename="添付書類.pdf">
<c:VfComponent oppId="{!relatedTo.Id}" />
</messaging:attachment>
</messaging:emailTemplate>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment