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
kavya mareedukavya mareedu 

This example is from TrialHead Module: Example


}Please Help!!!!!


In the above example, I need to write a trigger in such a way that whenever I create a new contact an email should be sent to me with the details also the count of number of triggers.

I have written this class for the above example:

public class EmailManager {
    public static void sendMail(List<contact> contacts)
    {
        for(Contact c: contacts)
        {
            if(c.email!=null)
            {
                c.email='kavya@system.in';
            }
        }
  
    }
    

}

This is the trigger:

trigger ExampleTrigger on Contact (after insert,after delete) {
    list<contact> contact= Trigger.new;
    if(Trigger.isInsert){
        Integer recordcount= Trigger.new.size();
        EmailManager.sendMail('kavya@system.in','Trailhead Trigger Tutorial',  recordcount+'contact(s) were inserted.');
    }else if(Trigger.isDelete){
        Integer recordcount = Trigger.new.size();
        
    }
kavya mareedukavya mareedu
Please let me know where am I going wrong!!!!! Thanks 
Meghna Vijay 7Meghna Vijay 7
Hi Kavya,
In trigger you are calling EmailManager class which has (String [] addresses, String [] subjects, String [] messages) as parameters  but in your case you created a class which has parameters (List<Contact> contact)  so in the2nd case why don't you just pass the Trigger.new . And in the class you'll write the code like :-
public class EmailManager {
    public static void sendMail(List<contact> contacts)
    {
        Messaging.SingleEmailMessage [] emails = new Messaging.SingleEmailMessage[]{};
        for(Contact c: contacts)
        {
            if(c.email!=null)
            {
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setSubject('Trailhead Trigger Tutorial');
            email.setToAddresses(new List<String> {c.email});
            email.setPlainTextBody(contacts.size()+'contact(s) were inserted.);
            emails.add(email);
            }
        }
        if(emails.size()>0) {
        Messaging.sendEmail(emails);
}
    }
    

}
Hope it helps and if it does select it as the best answer. :)
 
Waqar Hussain SFWaqar Hussain SF
What issue are you facing?
kavya mareedukavya mareedu
@meghnaVijay How should I write the triggers ????? That class is fine. I am so confused now!
I don't know what to write and how????????????? Help me!!!!!!!!!!
kavya mareedukavya mareedu
@Waqar I am not able to write the trigger and class for the above scenerio!!!
Meghna Vijay 7Meghna Vijay 7
@Kavya,
Trigger for the Email Manager Class which i wrote previously.
trigger ExampleTrigger on Contact (after insert,after delete) {
    list<contact> contact= Trigger.new;
if(Trigger.isAfter) {
   if(Trigger.isInsert || Trigger.isDelete){       
        EmailManager.sendMail(Trigger.new);
    }
}
   
Waqar Hussain SFWaqar Hussain SF
try below code
trigger ExampleTrigger on Contact (after insert,after delete) {
    if(Trigger.isInsert){
        EmailManager.sendMail(trigger.new);
    }else if(Trigger.isDelete){
        EmailManager.sendMail(trigger.old);
    }
}

 
kavya mareedukavya mareedu
Nothing is working!!!!!!!!!!!!!!!!!!
Waqar Hussain SFWaqar Hussain SF
What error are you getting while saving the trigger?
kavya mareedukavya mareedu
I am not getting any error in specific but the trigger is not getting executed.
Waqar Hussain SFWaqar Hussain SF
You mean that the email is not receiving, Correct?
If the trigger is active then the trigger must be firing. 
kavya mareedukavya mareedu
yes, you are correct @waqar
Waqar Hussain SFWaqar Hussain SF
Goto 
Setup > Email Administration > Deliverability
and set the Access level  to All Email