+ Start a Discussion
Dhananjay PV 6Dhananjay PV 6 

User Email field Update with Batch Apex class

Not able to update the User email field in database eventhough the logic is correct and also debug log shows that the Email field update is done. But not able to see the same in database.

global class UserEmailupdates implements Database.Batchable<sObject>
{
    User1__c uc = User1__c.getValues('Domainuser'); 
    String inteluc = uc.Domain__c;
//    List<User> userlist=new List<User>;


    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT name,Username,UpdatedEmail__c,email,userrole.name,profile.name From user where userrole.name = \'CRM Developer\'';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<User> scope)
    {
        for ( User u : scope)
        {
          if(u.userrole.name == 'CRM Developer')
           {  
               if(u.username.Contains('@example.com'))  
               {       
               String str=u.username;
               String str1=u.email;
//             String username1;
               u.username=str.substringbefore('@');
               u.username=u.username.replace('=','@');
               u.Email=str1.substringbefore('@');
               u.Email=u.Email.replace('=','@');
               System.debug('Email = ' + u.Email);
               System.debug('User name = ' + u.Username);  
               }
               else
               {
               String str=u.username;
               String str1=u.Email;
               u.username=str.substringbefore('@');
               u.username=u.username+inteluc;
//             u.Username =u.DomainUser__c+inteluc;
               u.Email=str1.substringbefore('@');
               u.Email =u.Email+inteluc;
               
               System.debug('Email = ' + u.Email);
               System.debug('User name = ' + u.Username);     
               }
           }
           
        }
        update scope;
        System.debug('Scope is= '+ Scope);
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}

Below is the debug log details
global class UserEmailupdates implements Database.Batchable<sObject>
{
    User1__c uc = User1__c.getValues('Domainuser'); 
    String inteluc = uc.Domain__c;
//    List<User> userlist=new List<User>;


    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT name,Username,UpdatedEmail__c,email,userrole.name,profile.name From user where userrole.name = \'CRM Developer\'';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<User> scope)
    {
        for ( User u : scope)
        {
          if(u.userrole.name == 'CRM Developer')
           {  
               if(u.username.Contains('@example.com'))  
               {       
               String str=u.username;
               String str1=u.email;
//             String username1;
               u.username=str.substringbefore('@');
               u.username=u.username.replace('=','@');
               u.Email=str1.substringbefore('@');
               u.Email=u.Email.replace('=','@');
               System.debug('Email = ' + u.Email);
               System.debug('User name = ' + u.Username);  
               }
               else
               {
               String str=u.username;
               String str1=u.Email;
               u.username=str.substringbefore('@');
               u.username=u.username+inteluc;
//             u.Username =u.DomainUser__c+inteluc;
               u.Email=str1.substringbefore('@');
               u.Email =u.Email+inteluc;
               
               System.debug('Email = ' + u.Email);
               System.debug('User name = ' + u.Username);     
               }
           }
           
        }
        update scope;
        System.debug('Scope is= '+ Scope);
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}


Debug log info is attached.

Input data :

User Name : suri101070@gmail.com
Email : suri101070@gmail.com

Output expecting :

User name : suri101070@intel.com
Email : suri101070@intel.com

But here can see the username getting updated in database but email field is not getting updated. Is Salesforce is enforcing any rule/restriction and preventing the Email field Update?Debug log info
Martijn SchwarzerMartijn Schwarzer
Hi Dhananjay,

If you change the user's email address, there will be an email sent to the new address containing a link to confirm the email change. Only after that link is clicked, the new email address will be active and visible in the database.

Please check if the new email address has received such an email. If you click the link, the new email address should be active.

Hope this helps!

Best regards,
Martijn Schwärzer 

Ps. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
VineetKumarVineetKumar
Perhaps you want to go an extra step in your batch class to replicate something like this
  • Retrieve your users
  • Deactivate the users
  • Correct their email addresses
  • Reactivate the users
  • Fire a password reset and send the reset email
Reference : http://salesforce.stackexchange.com/questions/9878/how-to-mass-change-emails-with-sending-only-a-password-reset-mail-in-apex

Let me know if that helped.
Dhananjay PV 6Dhananjay PV 6
Hi Martijn,

I have received the email from support@salesforce.com, saying that "
Your Salesforce account’s email address was changed".

We recently received a request to change your Salesforce account’s email address for username suri101070@gmail.com.

Previous email address: suri101070@gmail.com
New email address: suri101070@intel.com

If you didn’t request this change, contact your Salesforce administrator.

But i didn't receive any link in the email, so not able to see the email address change in my developer org.

Thanks,
​Dhananjay
Martijn SchwarzerMartijn Schwarzer
Hi Dhananjay,

You would receive an email on both the new and old address. The email sent to the old address contains the info you mention above. The new address should have an email with the link.

Thanks,
Martijn