3

When deactivating a Communities User in code, you can't directly update the User.ContactId field. Rather, setting User.IsPortalEnabled to FALSE has the side effect of nulling out User.ContactId.

I have the following trigger handler method that takes the expected Trigger variables as params:

public static void onAfterUpdate (List<User> trgNew, Map<Id,User> trgOld) {
    List<Id> conIds = new List<Id>();
    for (User u : trgNew) {
        if (u.ContactId != null) {
            conIds.add(u.ContactId);
        } else if (u.ContactId == null && trgOld.get(u.Id).ContactId != null) {
            //get the Contact ID from the old record if necessary
            conIds.add(trgOld.get(u.Id).ContactId);
        }
    }
    // past ID list to @future method to avoid Mixed DML 
    setContactLU(conIds);
}

I can't get the else if block to be tested when updating a portal User so that IsPortalEnabled and IsActive are both false - the ContactId seems to still be present in the after update context. I don't need the coverage of those two lines, but if I took them out, would it be guaranteed that ContactId is still populated for a User that is changing IsPortalEnabled from TRUE to FALSE? I'm not certain where in the order of execution a system field update falls.

  • When deactivating a user, I wouldn't expect the link to their contactId to be nulled. If that were to the case, it would seem that you wouldn't be able to activate them again without creating an entirely new user. Otherwise, most everything that was previously linked from that user to their contact record would have been lost. Perhaps things have changed as communities have evolved. It's been a while since I've worked in one. – crmprogdev Jun 5 '18 at 19:34
  • You can indeed not reactivate a Contact whose user has been deactivated without creating a new user. This is documented behavior, including in a popup when using the "Disable [Partner|Customer] User" button on contact. – Thomas Taylor Jun 5 '18 at 19:48
  • Learn something new every day! I'll have to look at the docs to see if that applies to other license types like HVCU too. – crmprogdev Jun 5 '18 at 21:16

Your Answer

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

Browse other questions tagged or ask your own question.