I'm toying with an idea of auto-activating (or deactivating) contacts for Communities/customer portal after certain criteria have been met on their contact records. It would look something like this (inefficient stub, for demonstration purposes):
- Go through Contacts in trigger, pulling list of contacts to activate/deactivate
- If there are contacts to activate/deactivate, query for any existing Portal users linked to these contact records. Activate/Deactivate accordingly (via
IsActive
) field. - For any remaining contacts (who didn't match existing users), create & insert new portal user accounts.
My question is: would this work?
- I know how to create portal users and manage their permissions in Apex
- I also know that there are some API restrictions on creating/updating User records in the same transaction where you update other data
- But I don't know if those limits apply to portal users, or in trigger contexts, or if there are ways to make it work anyway using @future calls or some other technique.
I can definitely explore this, but thought I'd ask here in case anyone has experience with this approach or whether it would work.
Best Answer
If your action is tripped by a DML on Contact, then you're mixing a DML operation of a non-setup object (Contact) with a setup object (User) and therefore you have to kick your trigger action to be handled by an @future method. There are no special limits here (other than apex trigger limits) as it applies to portal or communities context. Some things to consider though as you architect this code: