Salesforce Visualforce电子邮件模板单元测试
我有一个使用VF模板发送电子邮件的触发器。我正试图为该代码编写一个单元测试,但在尝试创建测试数据时一直遇到问题。Salesforce Visualforce电子邮件模板单元测试,salesforce,apex-code,visualforce,apex,Salesforce,Apex Code,Visualforce,Apex,我有一个使用VF模板发送电子邮件的触发器。我正试图为该代码编写一个单元测试,但在尝试创建测试数据时一直遇到问题。 以下是我的测试方法: static TestMethod void testQuestionAttachment(){ Id profileId = SYSTEM_ADMIN_PROFILE_ID; List<User> users = TestUtils.createUsers(profileId, 1); insert users; s
以下是我的测试方法:
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;
}
...
static TestMethod void testQuestionAttachment(){
Id profileId=系统\管理\配置文件\ Id;
List users=TestUtils.createUsers(profileId,1);
插入用户;
字符串templateText='- 测试内容
';
EmailTemplate=新的EmailTemplate(
developerName='TesteMailvTemplate',
TemplateType='visualforce',
FolderId=用户[0]。Id,
名称='TestEmailVFTemplate',
i活动=真);
template.HtmlValue=templateText;
template.Body=templateText;
System.runAs(用户[0]){
插入模板;
}
...
并且它会因字段\u完整性\u异常而失败,该异常是必需的,并且必须是第1行第1列的标记中最外层的标记:[markup]
我真的不明白为什么这不起作用。我一定是遗漏了什么…问题在于你的
TemplateType='Visualforce'
。改为template.Markup=templateText;
标记字段可以用来分配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;
}
}
static@isTest void myTest(){
Profile pf=[从Profile中选择Id、Name,其中Name='系统管理员'限制1];
User usr=新用户(
别名='usralias',
电子邮件theuser@email.com',
Emailencodingkey='UTF-8',
Lastname='user\u Lastname',
Languagelocalekey='en_US',
Localesidkey='en_US',
Profileid=pf.Id,
Timezonesidkey=‘美国/洛杉矶’,
Username=Math.random()+'test@testuser.com',
CompanyName=‘公司’,
UserRoleId='00E2800000ZQCY'
);
插入usr;
字符串templateText='- 测试内容
';
EmailTemplate template=new EmailTemplate(DeveloperName='TesteMailvTemplate',TemplateType='Visualforce',FolderId=UserInfo.getUserId(),
名称='TestEmailVFTemplate',
i活动=真);
template.Markup=templateText;
系统运行方式(usr){
插入模板;
}
}
我无法将模板类型更改为文本
,因为在我的代码中,我正在发送模板aWhatId
(自定义对象)和aTargetObject
(用户)。对于不兼容的文本模板,它们可以在VF模板中使用。是的,这是我第一次尝试。@AvailableName更新了答案。现在应该可以使用了。请确保提供relatedToType="自定义对象\uuuu c
是一个有效的对象。是的,它是EmailTemplate
上的标记
字段。谢谢。如果有一些关于为文本
email模板以外的任何东西编写单元测试的好文档…@AvailableName是的,我也搜索了它,但找不到任何内容。然后尝试查找各个领域ds的电子邮件模板通过开发人员控制台,并发现。