Test case failures due to VF email template

I encountered the same problem today:

System.DmlException: Update failed. First exception on row 0 with id 006o0000003SSs7AAG; first error: INSUFFICIENT_ACCESS_OR_READONLY, user does not have access to use approval assignment email template: []

While the symptom was indeed a Visualforce Email Template, the true cause was that the running user context was the Site Guest User when firing the Workflow Rule.

It is not possible to send emails on behalf of Site Guest User. My workaround in this case was to change the Workflow Rule Email Action to a Create Task Action, which guests are permitted to own.


Just ran into this issue today.

Solution background:

  1. testmethod with System.runAs(myuser) updates an Opportunity.
  2. Opportunity fires a workflow with an email alert
  3. Email alert uses a VF email template that refers to a VF component with an APEX controller

Answer for me was to grant myuser's profile access to VF component's controller class.


Your testing user usrAE may be the key as to why this works when done manually but not when run in the test cases.

As a first step, could you try running the test as the user that works in the manual tests? I.e. select the existing User rather than creating a new one.

If that still fails, try using an existing Account, Opportunity and Quote objects with seealldata=true.

The idea is to simplify the test back towards what works in the UI until a cause is found. It certainly isn't a long term solution, but will hopefully provide more clues.


I did find a similar question: Test case fails in eclipse, same test case succeeds in webinterface. Click-through also succeeds. If I'm reading the answer correctly the issue there was profile permissions for the controller used by the Visualforce component included in the email template.