-1

write a trigger so that Once an account is created an email should go to the admin user

--------helper -----

public class AccountEmailHelper
{
public static List<account> sendEmail(List<account> accounts)
 {
     EmailTemplate et=[Select id from EmailTemplate where name=:'new account'];
     List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

      for (Account acc:accounts )
      {
       if (acc.Email != null && acc.Name != null) 
        {
         Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
         singleMail.setTargetObjectId(user.Id);
         singleMail.setTemplateId(et.Id);
         singleMail.setSaveAsActivity(false);
         emails.add(singleMail);
         }
        }
     Messaging.sendEmail(emails);
     return accounts;

    }
}

trigger below ;;;;;

trigger AccountEmail on Account (after insert) 
{
if(Trigger.isAfter)
       {
        if(Trigger.isInsert )
          { 
          AccountEmailhelper.sendEmail(trigger.new);
          }
        }
}

closed as off-topic by Adam Spriggs, Adrian Larson, Tushar Sharma, martin, Ratan Paul Dec 14 '16 at 5:41

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Adam Spriggs, Adrian Larson, Tushar Sharma, martin, Ratan Paul
If this question can be reworded to fit the rules in the help center, please edit the question.

  • are you getting an error message? If so please add that to the question as it helps identify where the error is in the code. – Dave Humm Dec 13 '16 at 10:25
  • Did you get any error? One thing to not is Account object don't have Email standard field – Reshma Dec 13 '16 at 10:25
  • its not working ...not getting an error there is no mail going to system admin aftr account is created i want a trigger which sends a email to system admin whenever a new account is created – rahul gupta Dec 13 '16 at 11:10
  • why don't you use Workflow here? – Tushar Sharma Dec 13 '16 at 11:37
  • my requirement is by use of trigger – rahul gupta Dec 13 '16 at 12:01
1

In such situations, when you receive no errors, but your code still doesn't work as you expected - put several System.debug() methods, put your variables here to see their values during execution(like System.debug(acc)), OR just write any string to make sure that your code reached this system debug, i.e. System.debug('My code reaches here'). I inserted System.debug methods into your code below.

public class AccountEmailHelper
{
public static List<account> sendEmail(List<account> accounts)
 {
     EmailTemplate et=[Select id from EmailTemplate where name=:'new account'];
     List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

      for (Account acc:accounts )
      {
       if (acc.Email != null && acc.Name != null) 
        {
         System.debug('This debug allows me to understand that I have at least 1 account with not empty email and name');
         Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
         String[] toAddresses = new String[] {acc.Email};
         singleMail.setToAddresses(toAddresses);
         singleMail.setTargetObjectId(user.Id);
         singleMail.setTemplateId(et.Id);
         singleMail.setSaveAsActivity(false);
         emails.add(singleMail);
         }
        }
     System.debug('And this debug will allow you to understand if your emails list is not empty ' + emails);
     Messaging.sendEmail(emails);
     return accounts;

    }
}

And the obvious problem in your code is that you're not assigning setToAddresses() variable. In the code above I added rows that should dismiss the problem.

  • its showing error now Error: Compile Error: Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setTargetObjectId(Schema.SObjectField) at line 16 column 10 – rahul gupta Dec 13 '16 at 11:28
  • What error does it show? Try to not forget to post the error as soon as you receive it, it saves time and allows to provide you solution faster. – EvAzi Dec 13 '16 at 11:29
  • I reviewed the code and removed possible errors. Try again. – EvAzi Dec 13 '16 at 11:31
  • there is no email field on account its error again no email field found on object – rahul gupta Dec 13 '16 at 11:32
  • 1
    @rahulgupta stackexchange is not about providing full and correct code/solutions by your requests, but rather a platform to help developers/consultants/enthusiasts where to look and what to read in order to solve their problem. – EvAzi Dec 13 '16 at 11:54
0

This value:

User.Id

is an SObjectType token that represents the field itself not the value of the field.

To set a specific Id you could user UserInfo.getUserId() but that is the User that initiated the transaction. To get a "System Administrator" User you will have to query - see e.g. Send mail to the System Administrator.

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