+ Start a Discussion
BegginerBegginer 

Create a Trigger on Task object, to send an Email Notification to the assigned user, Upon creating a New Task.

Hi Everyone,

Im learning salesforce and  im unable to figure out how to implement the above program, can anyone help me with my querry?


Thanks in advance
Best Answer chosen by Begginer
Srinivas SSrinivas S
Hi,

As Manoj mentioned, It is not recommended to go for development if something is possible with salesforce out of the box feature like -
  1. Workflow Rules
  2. Validation Rules
  3. Process Builder
  4. Approval Process
For your practice purpose, you can write a trigger as mentioned below -
 
trigger TaskTrigger1 on Task (after insert) {
    //Collecting all the Assigned to userss(Owners)
    Set<Id> ownerSet = new Set<Id>();
    for(Task tsk : trigger.new) {
        ownerSet.add(tsk.ownerId);
    }
    //Query to fetch the email ids based on the owners
    Map<Id,User> ownerMap = new Map<Id,User>([select id, email from User where id in: ownerSet]);
    
    //Perparing list of email to send at a time
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    for(Task tsk : trigger.new) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[]{ownerMap.get(tsk.OwnerId).Email});
        mail.setSubject('Task Create with Id: '+tsk.Id);
        mails.add(mail);
    }
    
    //Triggering the email
    Messaging.sendEmail(mails);
}

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.

All Answers

ManojSankaranManojSankaran
Hi,

You can use a workflow rule to trigger this email. We dont need a trigger.

Steps
1. Create a workflow rule when a record is created. 
2. Create a workflow action as email alert and then select owner email and save.
3. Create an email template if required. and use the template in the workflow action- email alert.


Thanks
Manoj S
Srinivas SSrinivas S
Hi,

As Manoj mentioned, It is not recommended to go for development if something is possible with salesforce out of the box feature like -
  1. Workflow Rules
  2. Validation Rules
  3. Process Builder
  4. Approval Process
For your practice purpose, you can write a trigger as mentioned below -
 
trigger TaskTrigger1 on Task (after insert) {
    //Collecting all the Assigned to userss(Owners)
    Set<Id> ownerSet = new Set<Id>();
    for(Task tsk : trigger.new) {
        ownerSet.add(tsk.ownerId);
    }
    //Query to fetch the email ids based on the owners
    Map<Id,User> ownerMap = new Map<Id,User>([select id, email from User where id in: ownerSet]);
    
    //Perparing list of email to send at a time
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    for(Task tsk : trigger.new) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[]{ownerMap.get(tsk.OwnerId).Email});
        mail.setSubject('Task Create with Id: '+tsk.Id);
        mails.add(mail);
    }
    
    //Triggering the email
    Messaging.sendEmail(mails);
}

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
This was selected as the best answer