9

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

2
  • Are you trying to also change their login Id at the same time, or just their user contact email address. The User Login Id doesn't need to change. This could be part of the cause of your issue. Their password doesn't have to change if they're just changing their email address. Also, please post the code for your trigger.
    – crmprogdev
    Sep 21, 2015 at 3:41
  • @crmprogdev, I'm not updating any login ids. Just want to sync the email from User to the Contact Associated with it. Updated my question with a sample code for reference.
    – sfdx
    Sep 21, 2015 at 4:10

2 Answers 2

4

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.

4
  • Thanks for the idea. Might have to go with the Scheduled Job on the User Trigger. The only issue here is that there is only a window time for the users wherein they can confirm their email or the scheduled batch job won't be able to get the correct email address. Might be 5 mins after the scheduled job has been triggered.
    – sfdx
    Sep 21, 2015 at 23:28
  • @SeanSoriano You can have a heartbeat scheduled job to check whether the email address of the user has been updated. You can run it for every 5 minutes for say the next 24 hours. And stop running if the email address has already been updated.
    – Lance Shi
    Sep 21, 2015 at 23:56
  • Hi @Lance - I think there's gonna be issues with the Scheduled Batch Job limit. If multiple users has updated their email, all of those updates will schedule their own batch jobs. Is this correct?
    – sfdx
    Sep 22, 2015 at 2:00
  • @SeanSoriano Could be. I am not very sure if you can put the userList as a static variable or database data and if the scheduled job is already running just add into the userList. Otherwise, you are right, it will hit the limit
    – Lance Shi
    Sep 22, 2015 at 2:28
0

I noticed that the only thing I could see in debug logs after a user did the 'Changed Email Confirmation' was a VisualForce homepage component loading (and not the expected User Trigger running). This had nothing to do with the Email address change, but just ran because the user was directed to his/her homepage after confirming the email address update, and we happen to have a visualforce home page component on that homepage.

So I added a controller and an action to the homepage component ('action' property on the apex:page tag). This controller checks if the current user's email address is different from the 'previous email' (stored in a custom field on User). If so, it can trigger any needed actions. If not, do nothing.

This at least saves you from having to go through a tedious scheduling process. Only requirement is that the Visualforce component needs to be on each user's homepage, because that is where they will be directed after confirming the updated email address.

Example page:

<apex:form >
    <!-- nothing needs to be  on this page. Just including some example content-->
    <apex:pageBlock title="Please log a case for SFDC related Questions">
        <apex:pageBlockButtons location="top">
            <apex:commandButton value="New SFDC Case" onclick="window.open('{!URLFOR($Action.Case.NewCase)}','_parent');" />
        </apex:pageBlockButtons>

    </apex:pageBlock>
</apex:form>

Example controller:

public with sharing class SalesforceContactInfoController {

    public void checkUserEmail(){
        // check if the current user's email address has changed
        // this has to be done here as this is a homepage component that is loaded for every user
        // and the APEX trigger on User cannot detect an Email address change on User (SFDC BUG!)
        User u = [Select Id, Email, Previous_Email__c from User Where Id = :UserInfo.getUserId()];
        if(u.Email != u.Previous_Email__c ){
            // resync user (this should also reset the 'previous email' field)
            UserExport.exportUpdatedUsersOffline(New Set<Id>{u.Id});            
        }
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .