Memos About Salesforce

Salesforceにハマってたこと!

Salesforce InBound Email テスト Salesforceファイル追加

こんにちは、管理人の@Salesforce.Zです。

実装ができたが、Messaging.InboundEmailHandlerの実装クラスのテストはどう書くか少し迷うでしょう

読んだら得ること

★ Messaging.InboundEmailHandlerの実装クラスのテストクラス記載テンプレート

目次

Messaging.InboundEmailHandler

メール受信時にMessaging.InboundEmailHandlerの実装クラス Apexクラスを呼び出すことができます

例えば、取引先責任者からメールを受信したときに対象の取引先責任者に紐付く標準ToDoやカスタムオブジェクトを作成するなどがこの実装クラスにてカスタマイズできます。

あるいは所有者変更など。

Messaging.InboundEmailHandlerの実装クラス例

global class implementsCtl implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        sObject mailobj = createDataByMail(email);
        if(String.isNotEmpty(mailobj.Id)){
            result.success = true;
        }
        return result;
    }

    private sObject createDataByMail(Messaging.InboundEmail email){
        sObject mailobj = new sObject();
        // ここからのmailobjeの項目はカスタム項目です。
        mailobj.subject__c = email.subject;
        mailobj.body__c = email.plainTextBody;
        mailobj.date__c = Datetime.now();
        mailobj.isComplete__c = false;
        mailobj.sbt__c = '受信';
        mailobj.sender__c = email.fromAddress;
        mailobj.OwnerId = xxxxxx;//好きなように処理して~
    }
}

テストクラス例

//Test Method for main class
@isTest
private class implementsCtlTest {
   static testMethod void TestinBoundEmail(){
        // create a new email and envelope object

        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

        // setup the data for the email

        email.subject = 'Test Job Applicant';
        email.fromAddress = 'someaddress@email.com';

        // add an Binary attachment

        Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
        attachment.body = blob.valueOf('my attachment text');
        attachment.fileName = 'textfileone.txt';
        attachment.mimeTypeSubType = 'text/plain';
        email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };

        // add an Text atatchment

        Messaging.InboundEmail.TextAttachment attachmenttext = new Messaging.InboundEmail.TextAttachment();
        attachment.body = blob.valueOf('my attachment text');
        attachment.fileName = 'textfiletwo.txt';
        attachment.mimeTypeSubType = 'texttwo/plain';
        email.textAttachments =   new Messaging.inboundEmail.TextAttachment[] { attachmenttext };

        // call the email service class and test it with the data in the testMethod
        implementsCtl ctl = new implementsCtl ();
        ctl.handleInboundEmail(email, env);

        // create a contact data

        Contact testContact= new Contact();
        testContact.Email='someaddress@email.com';
        testContact.LastName='lastname';
        testContact.Phone='1234567234';
        testContact.Title='hello';
        insert testContact;

        system.debug('insertedcontact id===>' +testContact.Id);

        // Create Attachmenat data

        Attachment attachmnt =new Attachment();
        attachmnt.name='textfileone.txt';
        attachmnt.body =blob.valueOf('my attachment text');
        attachmnt.ParentId =testContact.Id;
        insert  attachmnt ;
    }
}

終わりに

実はここで、メインは添付ファイルをどう追加し、テストするかだった記事です。