LoginSignup
2
1

More than 3 years have passed since last update.

Salesforceから送信するメール本文を動的に変更する

Posted at

Salesforceからメールを送信する方法は複数ありますが、今回はメールアラートに設定できる Visualforcメールテンプレート を使います。

イメージ図


メールテンプレートを作成する際は Visualforce を選択する必要があります。
こちらはコンポーネントを呼び出すだけのシンプルなものです。

Visualforce メールテンプレートの編集
<messaging:emailTemplate subject="タイトル" relatedToType="Case">
<messaging:plainTextEmailBody >
<!-- コンポーネントの呼び出し、その際にCase.Idを引き渡す -->
<c:MailComp targetCaseId="{!relatedTo.Id}" />
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

Visualforce メールテンプレートから渡された Case.Id をコントローラー(Apexクラス)に渡すことで、必要な情報を作成するために使います。
rendered を使うことで、メール内に表示するかどうかを判定しています。

MailComp.component
<apex:component access="global" controller="MailCompExt">
<!-- VFテンプレートからCase.Idを取得 -->
<apex:attribute name="targetCaseId" type="Id" description="Case.Id" assignTo="{!caseId}" />
{!userName} 様

この度は{!subject}をお申し込みいただきありがとうございます。
<apex:outputText rendered="{!isToday}"><!-- レンダリングするかどうかをApexから取得 -->
いよいよ本日ですね。
</apex:outputText>
お待ちしています。
</apex:component>

コンポーネント内で動的に表示させたいテキストや、表示有無に関してはApexで管理します。

MailCompExt.apex
public class MailCompExt {
    public Id caseId { get;
                       set {
                           // CaseIdを元にカスタムオブジェクトからデータを取得
                       }
                     }

    public Boolean getIsToday() {
        return true;
    }
}

以上。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1