[SalesForce] Updating Contact Email from User Email Update

Is there anyway I can update a contact email when the user's email is updated?
I though this can be done via User trigger easily, but the problem is whenever the email gets updated from the User page, the user has to confirm first the updated email before the user email gets updated.

When the user clicks the confirmation link on the email, the trigger didn't fire(checked via debug logs). Thus, not updating the associated contact with it.

And I know that there is a workaround here wherein the user won't have to confirm the email if the "generate new password" is ticked, and I don't want to reset the user's password each time they have updated their email, so this is not an option.

Is there anyway around this?

Sample Code:

   trigger UserTrigger on User (after update) {

        if(Trigger.isUpdate && Trigger.isAfter) {
        //ChatterUtility.AddToGroups(Set userIds)
        Map<String, User> setUnamesCon = new Map<String, User>();

        for(User u : Trigger.new) {
            if (u.Email != Trigger.oldMap.get(u.Id).Email) {
                setUnamesCon.put(u.contactId, u);
            } 
        }

        if(setUnamesAcc.size() > 0 && setUnamesAcc != null) {
            List<Contact> lConForUpdates =   new List<Contact>();
            for(Contact con : [SELECT Id, Email FROM Contact WHERE id in: setUnamesCon.keySet()]) {
                    con.Email = setUnamesCon.get(con.Id).Email;
                    lConForUpdates.add(con);
            }
            update lConForUpdates;
        }

    }
}

Cheers

Best Answer

It seems like a Salesforce bug to me. In order to fully resolve this, the best we can do is probably to vote on this idea: https://success.salesforce.com/ideaView?id=08730000000kyTtAAI

Meanwhile, to walkaround this, you have two possible solutions:

1, The most correct, but might be painful solution. Fire a scheduled job in the user trigger. Check for every several minutes to see whether the email has been changed. If so, update the related contact.

2, If you don't mind updating the contact email right away without waiting for user confirmation, you can write a VF page and capture the new email address and update contact info there. Not very accurate solution though.

Related Topic