Created
February 13, 2015 12:15
-
-
Save tyoshikawa1106/fd35b80d1299a4702265 to your computer and use it in GitHub Desktop.
Chatter Message Trigger Sample
This file contains 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
trigger ChatterMessageTrigger on ChatterMessage (before insert, after insert) { | |
// Handler Class | |
ChatterMessageTriggerHandler handler = new ChatterMessageTriggerHandler(); | |
if (Trigger.isBefore) { | |
if (Trigger.isInsert) { | |
// Check NG Keyword | |
handler.isNGKeyword(Trigger.new); | |
} | |
} else if (Trigger.isAfter) { | |
if (Trigger.isInsert) { | |
// Create Task | |
handler.doCreateTask(Trigger.new); | |
} | |
} | |
} |
This file contains 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 with sharing class ChatterMessageTriggerHandler { | |
// Helper Class | |
private ChatterMessageTriggerHelper helper = new ChatterMessageTriggerHelper(); | |
// Dao Class | |
private ChatterMessageTriggerDao dao = new ChatterMessageTriggerDao(); | |
/** | |
* Constructor | |
*/ | |
public ChatterMessageTriggerHandler() { | |
} | |
/** | |
* Check NG Keyword | |
*/ | |
public void isNGKeyword(List<ChatterMessage> chatterMessages) { | |
// Check NG Keyword | |
this.helper.isNGKeyword(chatterMessages); | |
} | |
/** | |
* Create Task | |
*/ | |
public void doCreateTask(List<ChatterMessage> chatterMessages) { | |
// Get Conversation ID | |
Set<Id> conversationIds = this.helper.getConversationIds(chatterMessages); | |
// Get ChatterConversationMember | |
List<ChatterConversationMember> chatterMembers = this.dao.getChatterConversationMember(conversationIds); | |
// Get ChatterConversationMemberMap | |
Map<Id, List<ChatterConversationMember>> chatterMemberMap = this.helper.getChatterConversationMemberMap(chatterMembers); | |
// Get Create Tasks | |
List<Task> tasks = this.helper.getCreateTasks(chatterMessages, chatterMemberMap); | |
// INSERT (DmlException対応は省略) | |
List<Database.Saveresult> results = database.insert(tasks, true); | |
} | |
} |
This file contains 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
@isTest | |
private class ChatterMessageTriggerHandlerTest { | |
private static User testAdminUser = CommonTester.getLoginUser(); | |
/** | |
* Check NG Keyword | |
*/ | |
@isTest(SeeAllData=true) | |
static void doSendChatterMessageTest1() { | |
System.runAs(testAdminUser) { | |
// ユーザ テストデータ作成 | |
Id profileId = UserInfo.getProfileId(); | |
User user = CommonTester.createUser(profileId, true); | |
String message = 'NG!!'; | |
Id userId = user.Id; | |
Test.startTest(); | |
// テスト結果判定 | |
Boolean result = false; | |
// Chatter Message 送信 | |
try { | |
// ※ try - catch でもbefore Triggerのエラーをキャッチできないっぽい... | |
//ConnectApi.ChatterMessage chatterMessage = CommonTester.doSendChatterMessage(message, userId); | |
} catch (Exception e) { | |
result = true; | |
} | |
Test.stopTest(); | |
// Result | |
//System.assertEquals(result, true); | |
} | |
} | |
/** | |
* Check NG Keyword | |
*/ | |
@isTest(SeeAllData=true) | |
static void doSendChatterMessageTest2() { | |
System.runAs(testAdminUser) { | |
// ユーザ テストデータ作成 | |
Id profileId = UserInfo.getProfileId(); | |
User user = CommonTester.createUser(profileId, true); | |
String message = 'OK!!'; | |
Id userId = user.Id; | |
Test.startTest(); | |
// テスト結果判定 | |
Boolean result = false; | |
// Chatter Message 送信 | |
try { | |
ConnectApi.ChatterMessage chatterMessage = CommonTester.doSendChatterMessage(message, userId); | |
} catch(Exception e) { | |
result = true; | |
} | |
Test.stopTest(); | |
// Result | |
System.assertEquals(result, false); | |
} | |
} | |
/** | |
* Check NG Keyword | |
*/ | |
@isTest(SeeAllData=true) | |
static void doCreateTaskTest1() { | |
System.runAs(testAdminUser) { | |
// ユーザ テストデータ作成 | |
Id profileId = UserInfo.getProfileId(); | |
User user = CommonTester.createUser(profileId, true); | |
String message = 'OK!!'; | |
Id userId = user.Id; | |
Test.startTest(); | |
// Chatter Message 送信 | |
ConnectApi.ChatterMessage chatterMessage = CommonTester.doSendChatterMessage(message, userId); | |
Test.stopTest(); | |
// Result | |
List<Task> resultTasks = [SELECT Id,Subject FROM Task WHERE OwnerId =: user.Id]; | |
System.assertEquals(resultTasks.size(), 1); | |
System.assertEquals(resultTasks[0].Subject, 'Send Chatter Message'); | |
} | |
} | |
} |
This file contains 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 ChatterMessageTriggerHelper { | |
/** | |
* Constructor | |
*/ | |
public ChatterMessageTriggerHelper() { | |
} | |
/** | |
* Check NG Keyword | |
*/ | |
public Boolean isNGKeyword(List<ChatterMessage> chatterMessages) { | |
Boolean isError = false; | |
for (ChatterMessage c : chatterMessages) { | |
String message = c.Body; | |
if (message.indexOf('NG!!') >= 0) { | |
c.addError('Sorry... Your Message NG Keyword : 『NG!!』'); | |
// IsError | |
isError = true; | |
} | |
} | |
return isError; | |
} | |
/** | |
* Get Conversation ID | |
*/ | |
public Set<Id> getConversationIds(List<ChatterMessage> chatterMessages) { | |
Set<Id> conversationIds = new Set<Id>(); | |
for (ChatterMessage c : chatterMessages) { | |
if (String.isNotEmpty(c.ConversationId)) { | |
conversationIds.add(c.ConversationId); | |
} | |
} | |
return conversationIds; | |
} | |
/** | |
* Get ChatterConversationMember Map | |
* Key = ConversationId | |
*/ | |
public Map<Id, List<ChatterConversationMember>> getChatterConversationMemberMap(List<ChatterConversationMember> chatterConversationMembers) { | |
Map<Id, List<ChatterConversationMember>> chatterMemberMap = new Map<Id, List<ChatterConversationMember>>(); | |
for (ChatterConversationMember cMember : chatterConversationMembers) { | |
List<ChatterConversationMember> chatterMembers = new List<ChatterConversationMember>(); | |
if (chatterMemberMap.containsKey(cMember.ConversationId)) { | |
chatterMembers = chatterMemberMap.get(cMember.ConversationId); | |
} | |
// Add List | |
chatterMembers.add(cMember); | |
// Add Map | |
chatterMemberMap.put(cMember.ConversationId, chatterMembers); | |
} | |
return chatterMemberMap; | |
} | |
/** | |
* Get Create Tasks | |
*/ | |
public List<Task> getCreateTasks(List<ChatterMessage> chatterMessages, Map<Id, List<ChatterConversationMember>> chatterMemberMap) { | |
List<Task> tasks = new List<Task>(); | |
for (ChatterMessage c : chatterMessages) { | |
if (chatterMemberMap.containsKey(c.ConversationId)) { | |
List<ChatterConversationMember> chatterMambers = chatterMemberMap.get(c.ConversationId); | |
for (ChatterConversationMember cMember : chatterMambers) { | |
Task task = new Task( | |
OwnerId = cMember.MemberId | |
,Subject = 'Send Chatter Message' | |
,Description = URL.getSalesforceBaseUrl().toExternalForm() + '/_ui/core/chatter/ui/ChatterPage' | |
); | |
// Add List | |
tasks.add(task); | |
} | |
} | |
} | |
return tasks; | |
} | |
} |
This file contains 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
@isTest | |
private class ChatterMessageTriggerHelperTest { | |
private static ChatterMessageTriggerHelper helper = new ChatterMessageTriggerHelper(); | |
private static User testAdminUser = CommonTester.getLoginUser(); | |
/** | |
* Check NG Keyword | |
*/ | |
static testMethod void isNGKeywordTest1() { | |
System.runAs(testAdminUser) { | |
List<ChatterMessage> chatterMessages = new List<ChatterMessage>(); | |
Test.startTest(); | |
// Test | |
Boolean result = helper.isNGKeyword(chatterMessages); | |
Test.stopTest(); | |
// Result | |
System.assertEquals(result, false); | |
} | |
} | |
/** | |
* Check NG Keyword | |
*/ | |
static testMethod void isNGKeywordTest2() { | |
System.runAs(testAdminUser) { | |
ChatterMessage chatterMessage = new ChatterMessage( | |
Body = 'NG!!' | |
); | |
List<ChatterMessage> chatterMessages = new List<ChatterMessage>(); | |
chatterMessages.add(chatterMessage); | |
Test.startTest(); | |
// Test | |
Boolean result = helper.isNGKeyword(chatterMessages); | |
Test.stopTest(); | |
// Result | |
System.assertEquals(result, true); | |
} | |
} | |
/** | |
* Get Conversation ID | |
*/ | |
static testMethod void getConversationIdsTest1() { | |
System.runAs(testAdminUser) { | |
List<ChatterMessage> chatterMessages = new List<ChatterMessage>(); | |
Test.startTest(); | |
// Test | |
Set<Id> results = helper.getConversationIds(chatterMessages); | |
Test.stopTest(); | |
// Result | |
System.assertEquals(results.isEmpty(), true); | |
} | |
} | |
/** | |
* Get ChatterConversationMember Map | |
*/ | |
static testMethod void getChatterConversationMemberMapTest1() { | |
System.runAs(testAdminUser) { | |
List<ChatterConversationMember> chatterConversationMembers = new List<ChatterConversationMember>(); | |
Test.startTest(); | |
// Test | |
Map<Id, List<ChatterConversationMember>> resultMap = helper.getChatterConversationMemberMap(chatterConversationMembers); | |
Test.stopTest(); | |
// Result | |
System.assertEquals(resultMap.isEmpty(), true); | |
} | |
} | |
/** | |
* Get ChatterConversationMember Map | |
*/ | |
static testMethod void getCreateTasksTest1() { | |
System.runAs(testAdminUser) { | |
List<ChatterMessage> chatterMessages = new List<ChatterMessage>(); | |
Map<Id, List<ChatterConversationMember>> chatterMemberMap = new Map<Id, List<ChatterConversationMember>>(); | |
Test.startTest(); | |
// Test | |
List<Task> results = helper.getCreateTasks(chatterMessages, chatterMemberMap); | |
Test.stopTest(); | |
// Result | |
System.assertEquals(results.isEmpty(), true); | |
} | |
} | |
} |
This file contains 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 ChatterMessageTriggerDao { | |
/** | |
* Constructor | |
*/ | |
public ChatterMessageTriggerDao() { | |
} | |
/** | |
* getChatterConversationMember | |
*/ | |
public List<ChatterConversationMember> getChatterConversationMember(Set<Id> conversationIds) { | |
return [ | |
SELECT | |
Id | |
,MemberId | |
,ConversationId | |
FROM | |
ChatterConversationMember | |
WHERE | |
ConversationId IN: conversationIds | |
]; | |
} | |
} |
This file contains 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
@isTest | |
private class ChatterMessageTriggerDaoTest { | |
private static ChatterMessageTriggerDao dao = new ChatterMessageTriggerDao(); | |
private static User testAdminUser = CommonTester.getLoginUser(); | |
/** | |
* getChatterConversationMember | |
*/ | |
static testMethod void getChatterConversationMemberTest1() { | |
System.runAs(testAdminUser) { | |
Set<Id> conversationIds = new Set<Id>(); | |
Test.startTest(); | |
// Test | |
List<ChatterConversationMember> results = dao.getChatterConversationMember(conversationIds); | |
Test.stopTest(); | |
// Result | |
System.assertEquals(results.isEmpty(), true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment