2

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]

@isTest
public class SendKRNotificationTest
{
    static testMethod void SendEmail()
    {
        Test.StartTest();       
EmailTemplate et = new EmailTemplate (developerName = 'test', TemplateType= 'text', Name = 'test');
insert et;
        Keyword__c k = new Keyword__c();
        insert k;
        Test.StopTest();       
    }
}

TRIGGER:

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();
  mail.setTargetObjectId(keyword.OwnerID);
  mail.setSenderDisplayName('Admin');
  mail.setUseSignature(false);
  mail.setBccSender(false);
  mail.setSaveAsActivity(false);
  {
          EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Keyword_Request_Confirmation'];
          mail.setTemplateId(et.id);
          Messaging.SendEmailResult [] r =
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});  
      }
      }
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 Sep 21 '14 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.