SalesforceのVisualforceメールテンプレート
公開: 2021-05-13Visualforce電子メールテンプレートを使用すると、ユーザーはターゲットを絞ったパーソナライズされた電子メールを送信できます。 テンプレート作成者は、強力なVisualforceプログラミング言語を利用して、テンプレート化された電子メールコンテンツを作成できます。
開発者と管理者は、Visualforceを使用して電子メールテンプレートを作成できます。 標準のHTML電子メールテンプレートよりもVisualforceを使用する利点は、Visualforceを使用すると、受信者に送信されるデータに対して高度な操作を実行できることです。
すべてのVisualforce電子メールテンプレートは、単一の<messaging:emailTemplate>タグ内に含まれている必要があります。 これは、単一の<apex:page>タグ内で定義されている通常のVisualforceページに類似しています。
- <messaging:emailTemplate>タグには、単一の<messaging:htmlEmailBody>タグまたは単一の<messaging:plainTextEmailBody>タグが含まれている必要があります。
- <messaging:emailTemplate>内では、いくつかの標準Visualforceコンポーネントを使用できません。 これには、<apex:detail>、<apex:pageBlock>、関連するすべてのpageBlockコンポーネント、および<apex:form>などのすべての入力コンポーネントが含まれます。 これらのコンポーネントを使用してVisualforce電子メールテンプレートを保存しようとすると、エラーメッセージが表示されます。
次のいずれかを実行します。
- パブリックテンプレートを編集する権限がある場合は、[設定]から[クイック検索]ボックスに「電子メールテンプレート」と入力し、[クラシック電子メールテンプレート]を選択します。
- パブリックテンプレートを編集する権限がない場合は、個人設定に移動してください。 [クイック検索]ボックスに「テンプレート」と入力し、[メールテンプレート]または[マイテンプレート]のいずれか表示されている方を選択します。
- 1. [新しいテンプレート]をクリックします。
- 2. Visualforceを選択し、[次へ]をクリックします。
- 3.Visualforce電子メールテンプレートを使用して大量の電子メールを送信することはできません。
- 4.テンプレートを保存するフォルダを選択します。
- 5.テンプレートを使用できるようにするには、[使用可能]チェックボックスを選択します。
- 6. [電子メールテンプレート名]に名前を入力します。
- 7.必要に応じて、テンプレートの一意の名前を変更します。 この一意の名前は、Lightning PlatformAPIを使用するときのコンポーネントを指します。 管理対象パッケージでは、この一意の名前により、パッケージのインストールでの名前の競合が防止されます。 この名前には、アンダースコアと英数字のみを含めることができ、組織内で一意である必要があります。 文字で始まり、スペースを含めず、アンダースコアで終わらせず、2つの連続したアンダースコアを含めないようにする必要があります。 [テンプレートの一意の名前]フィールドを使用すると、管理パッケージ内の特定のコンポーネントの名前を変更でき、その変更はサブスクライバーの組織に反映されます。
- 8.必要に応じて、[エンコーディング]ドロップダウンリストから別の文字セットを選択します。
- 9.テンプレートの説明を入力します。 テンプレート名と説明はどちらも内部使用のみを目的としています。
- 10.電子メールの件名にテンプレートの件名を入力します。
- 11. [受信者の種類]ドロップダウンリストで、テンプレートから作成された電子メールを受信する受信者の種類を選択します。
- 12.必要に応じて、[関連するタイプ]ドロップダウンリストで、テンプレートがマージフィールドデータを取得するオブジェクトを選択します。
- 13. [保存]をクリックします。
- 14. [Salesforce Classicでの電子メールテンプレートの表示と編集]ページで、[テンプレートの編集]をクリックします。
- 15.Visualforce電子メールテンプレートのマークアップテキストを入力します。
注:画像を含める場合は、サーバー上の画像のコピーを参照するために、画像を[ドキュメント]タブにアップロードすることをお勧めします。 例えば:
<apex:image id =” Logo” value =” https://yourInstance.salesforce.com/servlet/servlet.ImageServer?
id = 015D0000000Dpwc&oid = 00DD0000000FHaG&lastMod = 127057656800″ />
- 1. Visualforceのバージョンとこの電子メールテンプレートで使用されるAPIを指定するには、[バージョン設定]をクリックします。 AppExchangeから管理パッケージをインストールした場合は、この電子メールテンプレートで使用する各管理パッケージのバージョンを指定することもできます。 通常、すべてのバージョンのデフォルト値を使用して、電子メールテンプレートを最新バージョンのVisualforce、API、および各管理対象パッケージに関連付けます。 特定の動作を維持するために、古いバージョンのVisualforceとAPIを指定できます。 最新のパッケージバージョンとは異なるコンポーネントまたは機能にアクセスするには、管理パッケージの古いバージョンを指定できます。
- 2.テンプレートの詳細を表示するには、[保存]をクリックします。 テンプレートの編集を続行するには、[クイック保存]をクリックします。 テンプレートを保存する前に、Visualforceマークアップが有効である必要があります。

/ *********************メールテンプレート1 ************* /
例1:コンタクトケース
受信者
<messaging:emailTemplate subject = "アカウントとケース情報{!recipient.Name}" receiveicity = "Contact">
<messaging:htmlEmailBody>
<html>
<頭>
</ head>
<本体>
親愛なる{!recipient.Name}、<br/>
開いているすべてのケースを見つけてください:<br/>
<表>
<tr>
<th>ケース番号</ th>
<th>ケースの起源</ th>
<th>ケースステータス</ th>
</ tr>
<apex:repeat value = "{!recipient.cases}" var = "case">
<tr>
<td> {!case.CaseNumber} </ td>
<td> {!case.Origin} </ td>
<td> {!case.Status} </ td>
</ tr>
</ apex:repeat>
</ table>
</ body>
</ html>
</ messagesing:htmlEmailBody>
<messaging:plainTextEmailBody>
おめでとう!
これは、新しいVisualforce電子メールテンプレートです。
</ messages:plainTextEmailBody>
</ messages:emailTemplate>
/ ********************メールテンプレート2 *************** /
例2:アカウントケース
受信者およびRelatedTo
<messaging:emailTemplate subject = "関連するアカウントケース:{!relatedTo.Name}" receiveicity = "Contact" relatedToType = "Account">
<messaging:htmlEmailBody>
<html>
<頭>
</ head>
<本体>
親愛なる{!relatedTo.Name}、<br/>
開いているすべてのケースを見つけてください:<br/>
<表>
<tr>
<th>ケース番号</ th>
<th>ケースの起源</ th>
<th>ケースステータス</ th>
</ tr>
<apex:repeat value = "{!relatedTo.cases}" var = "case">
<tr>
<td> {!case.CaseNumber} </ td>
<td> {!case.Origin} </ td>
<td> {!case.Status} </ td>
</ tr>
</ apex:repeat>
</ table>
</ body>
</ html>
</ messagesing:htmlEmailBody>
<messaging:plainTextEmailBody>
おめでとう!
これは、新しいVisualforce電子メールテンプレートです。
</ messages:plainTextEmailBody>
</ messages:emailTemplate>
例3:コンポーネントを使用した学生料金の詳細

<messaging:emailTemplate subject = "学生料金の提出" recipientType = "連絡先" relatedToType = "Student__c">
<messaging:htmlEmailBody>
<c:StudentFeesDetails stu> </ c:StudentFeesDetails>
</ messagesing:htmlEmailBody>
<messaging:plainTextEmailBody>
おめでとう!
これは、新しいVisualforce電子メールテンプレートです。
</ messages:plainTextEmailBody>
</ messages:emailTemplate>
/ ***************メールテンプレートの終了************ /
/******************* 成分 ******************/
<apex:component controller = "StudentFeesController" access = "global">
<スタイル>
table {border:1px solid#000;}
td、th {border:1px solid#000}
</ style>
<apex:attribute name = "stuId" assignTo = "{!studentId}" type = "Id" description = "Student Id" />
<h1>あなたの料金は無効になります</ h1>
<表>
<tr>
<th>日付</ th>
<th>金額</ th>
</ tr>
<apex:repeat value = "{!feesDetails}" var = "fee">
<tr>
<td> <apex:outputField value = "{!fee.date__c}" /> </ td>
<td> {!fee.amount__c} </ td>
</ tr>
</ apex:repeat>
</ table>
</ apex:component>
/************** クラス ******************/
パブリッククラスStudentFeesController {
public Id studentId {get; set;}
public List <Fees__c> getFeesDetails(){
return [select id、amount__c、date__c from Fees__c where student_Id__c =:studentId];
}
}
/ *******************ページ********************** /
<apex:page controller = "SendVFEmailTemplateController">
<apex:form>
<apex:pageBlock>
<apex:pageBlockButtons>
<apex:commandButton value = "Send Contact Cases" action = "{!sendCases}" />
<apex:commandButton value = "Send Account Cases" action = "{!sendAccountCases}" />
<apex:commandButton value = "Send Fees Details" action = "{!sendFeeDetails}" />
</ apex:pageBlockButtons>
</ apex:pageBlock>
</ apex:form>
</ apex:page>
/******************* クラス ********************/
パブリッククラスSendVFEmailTemplateController {
EmailTemplate et;
public SendVFEmailTemplateController(){
}
public void sendCases(){
et = [select Id、Name from EmailTemplate where DeveloperName = 'Account_Cases'];
Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
semail.setTargetObjectId( '00328000005cwjF');
semail.setTemplateId(et.Id);
Messaging.sendEmail(new Messaging.SingleEmailMessage [] {semail});
}
public void sendAccountCases(){
et = [select Id、Name from EmailTemplate where DeveloperName = 'Account_s_Cases'];
Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
semail.setTargetObjectId( '00335672305cwjF');
semail.setTemplateId(et.Id);
semail.setWhatId( '00123400006azlA');
Messaging.sendEmail(new Messaging.SingleEmailMessage [] {semail});
}
public void sendFeeDetails(){
et = [select Id、Name from EmailTemplate where DeveloperName = 'Student_Fees_Info'];
Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
semail.setTargetObjectId( '00335672305cwjF');
semail.setTemplateId(et.Id);
semail.setWhatId( 'a002320080DGNjx');
Messaging.sendEmail(new Messaging.SingleEmailMessage [] {semail});
}
}
このブログが、SalesforceでVisualforceメールテンプレートを使用する方法を学ぶのに役立つことを願っています。 セールスフォース開発サービスが必要な場合は、セールスフォースコンサルティングチームにご連絡ください。
