3

I have a trigger that sends an email using a VF template. I'm trying to write a unit test for that code, but keep running into a problem when trying to create the test data.
Here is my test method:

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;
    }
    ...

And it fails with FIELD_INTEGRITY_EXCEPTION, <messaging:emailTemplate> is required and must be the outermost tag in the markup at line 1 column 1: [Markup].
I really don't understand why this isn't working. I must be missing something...

AvailableName
  • 666
  • 4
  • 13
  • 24

1 Answers1

3

The issue is with your TemplateType = 'Visualforce'. Instead change it to template.Markup=templateText;. Markup field can be used to assign the body of visualforce template. I tried it in my test class. Refer below example:

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;
    }
}
Rohit Mourya
  • 285
  • 5
  • 17
  • I can't change the template type to `Text` becuase in my code I am sending the template a `WhatId`(custom object) and a `TargetObject` (User). And for text templates those are incompatible, but do work in VF templates. – AvailableName May 11 '17 at 06:14
  • Yes, that was my first attempt. – AvailableName May 11 '17 at 06:40
  • @AvailableName Updated the answer. Now it should work. Make sure to give `relatedToType= "Custom_Object__c` a valid object. – Rohit Mourya May 11 '17 at 06:43
  • Yeah, it was the `Markup` field on `EmailTemplate`. Thanks. If only there were some good documentation on writing unit tests for anything other than a `text` email template... – AvailableName May 11 '17 at 06:47
  • @AvailableName Yes, I also searched for it and could not find any. And then tried to look various fields of Email template through developer console and found that. – Rohit Mourya May 11 '17 at 06:48