Skip to content

Instantly share code, notes, and snippets.

@tyoshikawa1106
Created July 31, 2013 04:34
Show Gist options
  • Save tyoshikawa1106/6119293 to your computer and use it in GitHub Desktop.
Save tyoshikawa1106/6119293 to your computer and use it in GitHub Desktop.
Messaging.InboundEmailHandler
global class CreateTaskEmailExample implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,
Messaging.InboundEnvelope env) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
String myPlainText= email.plainTextBody;
Task[] newTask = new Task[0];
try {
Contact vCon = [SELECT Id, Name, Email
FROM Contact
WHERE Email = :email.fromAddress
LIMIT 1];
newTask.add(new Task(Description = myPlainText,
Priority = 'Normal',
Status = 'Inbound Email',
Subject = email.subject,
IsReminderSet = true,
ReminderDateTime = System.now()+1,
WhoId = vCon.Id));
insert newTask;
System.debug('New Task Object: ' + newTask );
} catch (QueryException e) {
System.debug('Query Issue: ' + e);
}
result.success = true;
return result;
}
}
@isTest
private class CreateTaskEmailExampleTest {
static testMethod void CreateTaskEmailExampleTest() {
String subject = 'Dummy Account Name 123';
String contactEmail = 'contactEmail@test.com';
String plainTextBody = 'This should become a note';
// 取引先テストデータ作成
Account account = new Account(Name = subject);
insert account;
// 取引先責任者テストデータ作成
Contact contact = new Contact(firstName = 'Jon', LastName = 'Smith', email = contactEmail);
insert contact;
// メモテストデータ作成
Note note = new Note(Title = 'Test', Body = plainTextBody, ParentId = account.Id);
insert note;
// Create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
// Set up your data if you need to Create the email body
email.plainTextBody = plainTextBody;
email.fromAddress = contactEmail;
email.ccAddresses = new String[]{};
email.subject = subject;
// コンストラクタ
CreateTaskEmailExample edr = new CreateTaskEmailExample();
Test.startTest();
// handleInboundEmailの実行
Messaging.InboundEmailResult result = edr.handleInboundEmail(email, env);
Test.stopTest();
// 成功メッセージの確認
System.assert (result.success, 'InboundEmailResult returned a failure message');
// 取引先テストデータ取得
Account [] accDb = [select ID from Account where name=:email.subject];
System.assertEquals (1, accDb.size(),'Account was not inserted');
// 取引先責任者テストデータ取得
Contact [] cDb = [select firstname,lastname from Contact where email=:contactEmail];
System.assertEquals (1, cDb.size(),'Contact was not inserted!');
Contact c = CDb[0];
System.assertEquals ('Jon', c.firstName);
System.assertEquals ('Smith', c.LastName);
// メモテストデータ取得
Note [] nDb = [select body from Note where ParentID=:accDb[0].id];
System.assertEquals (1,nDb.size(), 'A note should have been attached');
System.assertEquals (email.plainTextBody, nDb[0].body);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment