+ Start a Discussion
ruchika Nayyarruchika Nayyar 

Once an account an email should go to the admin user with specified text below. An account has been created and name is "Account Name".

trigger Account2 on Account (before insert) {
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    for (Account myAccount : Trigger.new) {
    if (myAccount.Email != null && myAccount.FirstName = "AccountName") {
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         List<String> sendTo = new List<String>();
        mail.setSubject('An account has been created');
      String body = 'Dear ' + myAccount.FirstName + ', ';
      body += 'An account has been created and name is "AccountName"';


please help me for this code
Martijn SchwarzerMartijn Schwarzer
Hi Ruchika,

You can use workflow rules to accomplish this. Please check the following link for an example of how to do this:


You will want to create an email template for this purpose and send it to the admin user once a new Account has been created.

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.
Hi Rukchika,

I believe Martijn Suggested well on this. Workflow will do in your case, and just a add on to Martijn's Answer, We should not use SF Ids in the code as when code deploy to higher orgs(Test, Prod) Id changes. 
ruchika Nayyarruchika Nayyar
hi please hlp me for this code

i want to create this
Once an account an email should go to the admin user with specified text below.
An account has been created and name is "Account Name".

trigger emailtoadmin on Account (before insert) {
    for(Account acc:trigger.new){
             if(acc.myruchika__email1__c!= null)
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
       email.setToAddresses(new String[]{'ruchikanayyar12@gmail.com'});        
        email.setReplyTo('ruchika Nayyar');
        email.setPlainTextBody('An account has been created and name is "Account Name".');
                 email.saveAsActivity = false;
      Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });

Mohd Sayeed Khan 1Mohd Sayeed Khan 1
Try this one

trigger sendEmailToAdmin on Account (after insert){
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    User userObj=[select Id,Profile.Name,Email from user where Profile.Name='System Administrator'];
    for(Account accObj:Trigger.new){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            //Assigning the receiver Mail Address
            mail.toAddresses = new String[]{userObj.Email};
                mail.setSubject('New Account was Created.');
            String body = 'Dear System Administrator, <br/>';
            body += 'An account has been created and name is '+accObj.Name+'.';