function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pokalakari nagapokalakari naga 

Trigger on contact and send email notification

Hi everyone,

Can anyone help me to write a trigger to create an Contact when an Account is created and send an email notification to the contact. In Account object I have created a custom field for email ID and create a contact.

Thanks in advance.

Regards,
naga.
Best Answer chosen by pokalakari naga
AvaneeshAvaneesh
Hii 
I write your trigger, please check

 
trigger SendingEmailOfContact on Account (after insert) {

        List<Contact> ContactList = new List<Contact>();
        String subject = 'An contact is created when account is created ';
        String message ='Account name is ';
        for(Account ac:trigger.new)
        {
           if(ac.Id !=NULL && ac.Email_id__c !=NULL)
           {               
               Contact con = new Contact();
               con.lastName = ac.Name;
               con.Email = ac.Email_id__c;
               con.AccountId = ac.Id;
               ContactList.add(con);
               List<String> ls = new List<String>();
               ls.add(ac.Email_id__c);
                message +=ac.Name ;   
               Messaging.SingleEmailMessage email  = new Messaging.SingleEmailMessage();
               email.setSubject(subject);
               email.setPlainTextBody(message);        
               email.setToAddresses(ls);
               Messaging.sendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage [] {email} );
           }
        }
    
        if(ContactList.size()>0)
        {
            insert contactList;
        }
    
}

Please don't forget to marked as best answer if this was helpful 


Thank you
Avaneesh Singh

All Answers

D-CoderD-Coder
You need contact last name , which is a required field. What you are going to populate in last name ?
AvaneeshAvaneesh
Hii 
I write your trigger, please check

 
trigger SendingEmailOfContact on Account (after insert) {

        List<Contact> ContactList = new List<Contact>();
        String subject = 'An contact is created when account is created ';
        String message ='Account name is ';
        for(Account ac:trigger.new)
        {
           if(ac.Id !=NULL && ac.Email_id__c !=NULL)
           {               
               Contact con = new Contact();
               con.lastName = ac.Name;
               con.Email = ac.Email_id__c;
               con.AccountId = ac.Id;
               ContactList.add(con);
               List<String> ls = new List<String>();
               ls.add(ac.Email_id__c);
                message +=ac.Name ;   
               Messaging.SingleEmailMessage email  = new Messaging.SingleEmailMessage();
               email.setSubject(subject);
               email.setPlainTextBody(message);        
               email.setToAddresses(ls);
               Messaging.sendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage [] {email} );
           }
        }
    
        if(ContactList.size()>0)
        {
            insert contactList;
        }
    
}

Please don't forget to marked as best answer if this was helpful 


Thank you
Avaneesh Singh
This was selected as the best answer
AvaneeshAvaneesh
Hi

if you want anything else then let me know

 
Amit Chaudhary 8Amit Chaudhary 8
You can try same with process builder or workflow. Try same
pokalakari nagapokalakari naga
Thanks Avaneesh.
pokalakari nagapokalakari naga
Hi Avaneesh,

I  want to know why you have take the string(Is email.setToAddresses(ls). Can't we directly give the contact.email.
 
AvaneeshAvaneesh
Hii naga

this method only accepts List of string for sending email either you have to send one email or more than that you just have to put in the list and pass it as an argument 
setToAddresses(List<String> email);

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

Check this Link or see this image

 User-added image


Thank you 
Avaneesh Singh
pokalakari nagapokalakari naga
Thanks you Avaneesh.