I have an email service handler that parses text and creates a new task for the user (based on the 'from' email). This is similar to the Email2Salesforce feature.

I'm having trouble setting the create user to the user my apex code is creating the task for.

If I specify the createUserId field in code then I get error that create/update system fields are not editable. But if I don't specify then it defaults to me, who authored the apex code.

By not setting the createUserId and letting default to me then the chatter feeds of my reps' accounts and contacts are full of "Doug Ayers assigned XYZ..." or "Doug Ayers created..." But I'm not actually doing anything. I just happened to write the apex code.

  • 2
    You might want to consider creating a User called "System" and use that to edit the code for this scenario. That way their feeds get updated with "System assigned XYZ" and not "Doug Ayers assigned XYZ". – Peter Knolle Nov 29 '13 at 13:20
  • Yes, an excellent idea - I second the idea of a service account for those types of activities. – Mike Chale Nov 29 '13 at 19:44

That is not currently possible for email handlers. When you create an email service you set the context user and that cannot be changed at run-time. When testing, there is a System.runAs() method that lets you set the user running the code, but that is only available for unit testing.

The best option might be to create a custom field and populate that as the record's "owner."

  • That's what I was afraid of. I'll have to discipline my team to use a "system" user to author the apex code as Peter Knolle suggested. Thanks – Doug Ayers Nov 29 '13 at 14:27
  • 1
    I created a generic system user and assigned it as the Context User for the email services. wiki.developerforce.com/page/… – Doug Ayers Nov 29 '13 at 16:22

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.