We created a trigger and test class: when a new record under Keyword object is created, we want an email notification sends out automatically to record owner and confirm we receive his keyword request. But we got error message for test class, we don't know how to reference folderId for email template and could not find information that makes it work. Please help.

Error Message System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [FolderId]: [FolderId]

public class SendKRNotificationTest
    static testMethod void SendEmail()
EmailTemplate et = new EmailTemplate (developerName = 'test', TemplateType= 'text', Name = 'test');
insert et;
        Keyword__c k = new Keyword__c();
        insert k;


trigger SendKRNotification on keyword__c (after insert) {
  keyword__c keyword = trigger.new[0];
  String[] toAddresses = new String[] {keyword.email__c};
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Keyword_Request_Confirmation'];
          Messaging.SendEmailResult [] r =
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});  

1 Answer 1


You are querying a template in the trigger, do you don't need to make a test method template, which wouldn't be used anyways. However, if you did need to create a template, you could use UserInfo.getUserId(), which would place the template in the user's "My Templates" folder. You could also create or query for a Folder that supports email templates. For that case, check the documentation for the Folder object in the standard documentation.

  • oh, I remove the template and it passed, easier than I thought, thank you!! Have a nice weekend.
    – user11377
    Commented Sep 21, 2014 at 2:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .