-1

Please help me how to write apex code to update the user's emails id with apex class which can be run after sandbox refresh.

global static void run() { 

    List<User> userEmailList = [select Email from User where profile.name = ‘System Administrator’]; 

    for(User uc : userEmailList) 

    { 

        uc.Email = uc.Email.replace('=','@'); 
        String addedPhrase = '@example.com'; 
        uc.Email = uc.Email.remove(addedPhrase); 
        userEmailList.add(uc); 
    } 
    if(userEmailList.size() > 0) 
    { 
         Update userEmailList; 
    } 
}
  • 1
    Hello @Enriquez, welcome to SFSE. Your question is very general and thus difficult to answer. Additionally, there's a broad expectation here that you should show your research and the work you've done so far, rather than asking for others to do it for you. Could you expand on your question along those lines? (How to Ask is a good resource for formulating a good question). – David Reed Apr 4 '18 at 18:07
  • I am trying the update the System admin email Id's after post refresh by using the following code, but unable to achieve it. Unable to figure out what am I missing – Mark McGill Apr 4 '18 at 18:09
  • Sorry..missed my code in above comments – Mark McGill Apr 4 '18 at 18:11
  • What exactly happens when you run this code? What, if any, error message do you receive? Again, please add this information to your question. – David Reed Apr 4 '18 at 18:24
  • I found that email addresses aren't actually updated, but that confirmation emails are sent out to each updated user. In a company with many sandboxes, this can get a bit spammy, so we wrote code to restrict this to targeted individuals as per sandbox name. – Kimball Robinson Nov 21 '18 at 16:33
0

Looks like a problem here:

List<User> userEmailList = [select Email from User where profile.name = ‘System Administrator’]; 

You're using Smart Quotes (‘...’), which won't compile in Apex Code. Replace them with apostrophes (').

Minor detail: if(userEmailList.size() > 0) is not necessary.

In the future, avoid copying code to or from Office software (e.g. Microsoft Office, Open Office, etc), since they tend to modify your code in unexpected ways.

  • I used apostrophes, but still unable to save the class. I got an error stating Expecting '}' but was: 'for'. at line 3. – Mark McGill Apr 4 '18 at 18:34
  • @Enriquez Did you put this code in a class? After fixing apostrophes, it worked fine in my org. – sfdcfox Apr 4 '18 at 18:36
  • Thank you very much..I forgot I was using other than global while defining class. Now i could save it. – Mark McGill Apr 4 '18 at 18:40
  • Wouldn't this still give a runtime error on this line -- userEmailList.add(uc); ? as the collection is modified while being iterated. – Jayant Das Apr 4 '18 at 18:42
  • @JayantDas Actually, I did overlook that. They'll need to remove that line of code, too. – sfdcfox Apr 4 '18 at 18:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.