【问题标题】:Visualforce email template unit testVisualforce 电子邮件模板单元测试
【发布时间】:2017-05-10 07:21:33
【问题描述】:

我有一个使用 VF 模板发送电子邮件的触发器。我正在尝试为该代码编写单元测试,但在尝试创建测试数据时一直遇到问题。
这是我的测试方法:

static TestMethod void testQuestionAttachment(){
    Id profileId = SYSTEM_ADMIN_PROFILE_ID;
    List<User> users = TestUtils.createUsers(profileId, 1);
    insert users;
    string templateText = '<messaging:emailTemplate subject="{!relatedTo.Name}" recipientType="User" relatedToType="Cutom_Object__c"><messaging:htmlEmailBody ><ul><li>test content</li></ul></messaging:htmlEmailBody></messaging:emailTemplate>';
    EmailTemplate template = new EmailTemplate(
        developerName = 'TestEmailVFTemplate', 
        TemplateType= 'visualforce', 
        FolderId = users[0].Id, 
        Name = 'TestEmailVFTemplate',
        IsActive = true);
    template.HtmlValue = templateText;
    template.Body = templateText;
    System.runAs(users[0]){
        insert template;
    }
    ...

它失败了FIELD_INTEGRITY_EXCEPTION, &lt;messaging:emailTemplate&gt; is required and must be the outermost tag in the markup at line 1 column 1: [Markup]
我真的不明白为什么这不起作用。我一定是错过了什么……

【问题讨论】:

    标签: salesforce apex-code visualforce apex


    【解决方案1】:

    问题在于您的TemplateType = 'Visualforce'。而是将其更改为template.Markup=templateText;Markup 字段可用于分配 visualforce 模板的正文。我在我的测试课上试过了。 参考下面的例子:

    static @isTest void myTest () { 
        Profile pf = [SELECT Id,Name FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
    
        User usr = new User(
            Alias                 = 'usralias',
            Email                 = 'theuser@email.com',
            Emailencodingkey      = 'UTF-8',
            Lastname              = 'user_lastname',
            Languagelocalekey     = 'en_US',
            Localesidkey          = 'en_US',
            Profileid             =  pf.Id,
            Timezonesidkey        = 'America/Los_Angeles',
            Username              =  Math.random() + 'test@testuser.com',
            CompanyName           = 'the company',
            UserRoleId='00E28000000zqCy'
        );
        insert usr;
        string templateText = '<messaging:emailTemplate subject="{!relatedTo.Name}" recipientType="User" relatedToType="Custom_Object__c"><messaging:htmlEmailBody ><ul><li>test content</li></ul></messaging:htmlEmailBody></messaging:emailTemplate>';
        EmailTemplate template = new EmailTemplate(DeveloperName = 'TestEmailVFTemplate', TemplateType= 'Visualforce', FolderId = UserInfo.getUserId(),
        Name = 'TestEmailVFTemplate',
        IsActive = true);
    
        template.Markup=templateText;
    
        System.runAs(usr){
            insert template;
        }
    }
    

    【讨论】:

    • 我无法将模板类型更改为Text,因为在我的代码中我向模板发送了WhatId(自定义对象)和TargetObject(用户)。对于文本模板,它们不兼容,但可以在 VF 模板中使用。
    • 是的,那是我的第一次尝试。
    • @AvailableName 更新了答案。现在它应该可以工作了。确保给relatedToType= "Custom_Object__c 一个有效的对象。
    • 是的,它是 EmailTemplate 上的 Markup 字段。谢谢。如果有一些关于为text 电子邮件模板以外的任何东西编写单元测试的好文档......
    • @AvailableName 是的,我也找了,没找到。然后尝试通过开发者控制台查看Email模板的各个字段,发现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多