I’ve just been trying to figure out how to trigger some action on User creation. For example, imagine you want to make sure that a new user automatically follows some important folk in Chatter! (signed up yet?) It turns out to be a little tricky because the User object is a little special.

The documentation states that some objects “require that you perform DML operations on only one type per transaction” – the User object is one of these objects. So if I want to manipulate other objects (I do), I have to use a second method (that uses a @future).

Here’s the basic outline of what I did. Here’s the trigger, on a after insert of a User object:

Now all we need is to make sure that the addFriendstoUsers() uses the @future annotation:

As you can see, I’m not actually befriending the user – but I do create another record just to test that this all works. Which it appears to do!

Postscript

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS