Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
error

We made a wrong turn. Try again.

Learner SF asked in #Apex
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
2 answers
  1. May 24, 2016, 7:23 AM
    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.
0/9000