Skip to content

Instantly share code, notes, and snippets.

@na0AaooQ
Last active December 16, 2018 20:50
Show Gist options
  • Save na0AaooQ/9f9e230609f405f8091ad4b42c2364cc to your computer and use it in GitHub Desktop.
Save na0AaooQ/9f9e230609f405f8091ad4b42c2364cc to your computer and use it in GitHub Desktop.
Salesforce Visualforce + Apexで入力フォーム作成 (入力内容をChatWorkで通知 + メールで送信する) ref: https://qiita.com/na0AaooQ/items/6f4a5ff6229bf040c571
<apex:page controller="PostMessage">
<apex:form >
<apex:pageBlock title="PostMessageForm">
<apex:pageMessages />
<table class="chatwork_messages">
<tr>
<th>
Chatworkへ投稿するメッセージタイトル 及び 送信するメール件名を入力
</th>
<td>
<apex:pageBlockSection columns="1">
<apex:inputText id="chatwork_title" value="{!chatwork_title}" required="true" />
</apex:pageBlockSection>
</td>
</tr>
<tr>
<th>
Chatworkへ投稿するメッセージ 及び メール本文を入力
</th>
<td>
<apex:pageBlockSection columns="1">
<apex:inputTextarea id="chatwork_meesage" value="{!chatwork_message}" required="true" />
</apex:pageBlockSection>
</td>
</tr>
<tr>
<th>
Chatworkへメッセージを投稿する時の投稿先ルーム番号を入力
</th>
<td>
<apex:pageBlockSection columns="1">
<apex:inputText id="chatwork_post_room" value="{!chatwork_post_room}" required="true" />
</apex:pageBlockSection>
</td>
</tr>
<tr>
<th>
メッセージ送信先のメールアドレスを入力
</th>
<td>
<apex:pageBlockSection columns="1">
<apex:inputText id="to_mail_address" value="{!to_mail_address}" required="true" />
</apex:pageBlockSection>
</td>
</tr>
</table>
<apex:commandButton action="{!post_message}" value=" メッセージのChatwork投稿とメール送信 "/>
</apex:pageBlock>
</apex:form>
</apex:page>
////////////////////////////////////////
// Visualforceで入力されたメッセージ内容やタイトル等をChatworkやメール配信するApexクラス
////////////////////////////////////////
public with sharing class PostMessage {
// Visualforceページのテキストエリアで入力されたChatworkへ投稿するメッセージ本文を取得
public String chatwork_message { get; set; }
// Visualforceページのテキストボックスで入力されたChatworkへ投稿するメッセージタイトルを取得
public String chatwork_title { get; set; }
// Visualforceページのテキストボックスで入力されたメッセージ投稿先のChatworkルーム番号を取得
public String chatwork_post_room { get; set; }
// Visualforceページのテキストボックスで入力されたメール送信先アドレスを取得
public String to_mail_address { get; set; }
public void post_message() {
PostChatworkMessage post_chatwork = new PostChatworkMessage();
SendMailMessage send_mail_message = new SendMailMessage();
String mail_subject = chatwork_title;
String mail_message = chatwork_message;
// Visualforceページで入力されたメッセージをChatworkへ投稿する
post_chatwork.post_chatwork_message( chatwork_title, chatwork_message, chatwork_post_room );
// Visualforceページで入力されたメッセージをメール送信する
send_mail_message.send_mail_message( mail_subject, mail_message, to_mail_address );
}
}
////////////////////////////////////////
// Chatworkにメッセージを投稿するApex
////////////////////////////////////////
public with sharing class PostChatworkMessage {
// カスタム設定からChatwork APIトークン情報等を取得
private static String ChatworkApiEndPointURL;
private static String ChatworkApiToken;
private static String ChatworkApiVersion;
static {
ChatworkApiSetting__c setting = ChatworkApiSetting__c.getInstance();
ChatworkApiEndPointURL = setting.ChatworkApiEndPointURL__c;
ChatworkApiToken = setting.ChatworkApiToken__c;
ChatworkApiVersion = setting.ChatworkApiVersion__c;
}
public void post_chatwork_message(String chatwork_title, String chatwork_message, String chatwork_post_room) {
String result_message = '';
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(ChatworkApiEndPointURL + '/' + ChatworkApiVersion + '/rooms/' + chatwork_post_room + '/messages');
request.setMethod('POST');
request.setHeader('X-ChatWorkToken', ChatworkApiToken);
request.setBody('body=' + '[info][title]' + chatwork_title + '[/title]' + chatwork_message + '[/info]');
HttpResponse response = http.send(request);
Integer status = response.getStatusCode();
if ( status == 200 ) {
result_message = response.getBody();
} else {
result_message = status.format() + response.getBody();
}
}
}
////////////////////////////////////////
// メッセージをメール送信するApex
// 詳細は以下を参照。
// https://developer.salesforce.com/docs/atlas.ja-jp.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm
////////////////////////////////////////
public with sharing class SendMailMessage {
// カスタム設定からメール送信設定等を取得
private static String MailReplyAddress;
private static String MailSenderDisplayName;
static {
MailSetting__c setting = MailSetting__c.getInstance();
MailReplyAddress = setting.MailReplyAddress__c;
MailSenderDisplayName = setting.MailSenderDisplayName__c;
}
public void send_mail_message(String mail_subject, String mail_message, String to_mail_address) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[]{to_mail_address});
mail.setReplyTo(MailReplyAddress);
mail.setSenderDisplayName(MailSenderDisplayName);
mail.setSubject(mail_subject);
mail.setPlainTextBody(mail_message);
// メッセージをメール送信
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment