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
TheSFDeveloperTheSFDeveloper 

Email Alert and Trigger

Hi Experts,

I have created one Email Alert "TestEmailAlert" which is having
1. e-mail template
2. recipients e-mail address.

Now I am creating a new Trigger on my custom object for the event before_insert and before_update and if certain condition gets satisfied then i want to use Email Alert "TestEmailAlert" which i have created above into my Trigger to send an email to the targeted users.

Right now we are not sure how to call Email Alert within Trigger.

Any help would be appreciated !!

Thank You,

werewolfwerewolf
If you actually want to call the workflow with the email alert, you'll have to change the object (but if the change in the very same object has already called the trigger then that's out).  You're probably better off just coding the email alert right into your Apex as per the docs above.
TheSFDeveloperTheSFDeveloper

Hi Again,

Thanks for the suggestions.

I have gone through the docs which you have provided and in this case i have to select the number of users ID to whom i have to send an email along with the email template.

And what i am looking for is: CALL EMAIL ALERT FROM TRIGGER

The main benefit of calling EMAIL ALERT from Trigger is, I do not need to select the user's ID and Template ID inside the trigger as it is been already defined in the EMAIL ALERT.

Please let us know your thoughts.

Thank You,

Vishal

werewolfwerewolf
Yeah, and I'm saying that if your trigger is on the exact same object that you intend the workflow to operate on, then it's not going to work unless the workflow would have matched the criteria on the object anyway.  Your other option is to tweak some other object which triggers some other workflow with an email alert, but then you lose some of the context of the original triggering object.
 
So for example, let's say you change a Case such that it triggers your Apex, but it wouldn't normally trigger this workflow email alert.  You make some change in your Apex so that it would trigger the workflow.  The workflow email alert will still not fire (I think) because you're already in the chain of execution, and you're past the criteria evaluation already.
 
If you really want to do it with workflow you can go tweak some custom object X which has its own workflow email alert.  That will in fact fire, but the Case merge fields will not be available to its template.


Message Edited by werewolf on 05-26-2008 09:56 AM
TheSFDeveloperTheSFDeveloper

You mean to say, below sample code is not possible from trigger?

trigger sendEmailTesting on Custom_Object__c (after insert,after update) {

 

    

   for (Custom_Object__c  newCustomObject : Trigger.new)

    {       

           if(newCustomObject[0].PRODUCT_TYPE == 'ABC')

           {

                  EmailAlert Ea = new EmailAlert();

                  Ea.setID = '121121'

                  Ea.send();

            }

    }

 

 

}

 
 
Thank You,
Vishal
werewolfwerewolf
Right, you can't do that.  But that's what those MailMessage objects I mentioned earlier are for.  So you have to set the User ID and template in them -- what's the big deal about that?
TheSFDeveloperTheSFDeveloper

Okie.

Let me give a try :)

 

Thanks for all your suggestions.

TheSFDeveloperTheSFDeveloper
Hi All,

Can someone please help me in disabling the trigger on SalesForce.com production environment.

Thanks in advance,
Vishal