You need to sign in to do that
Don't have an account?
Opportunity conditional statements +task to send email to task owner
1. IF opportunity record is a financial vertical type And IF the opportunity is above $500K
2. Create a task to send an email to opportunity owner.
Here's what I have so far . I would appreciate your input of whats missing or filling the blanks to make it work .
trigger on OpportunityCreation on opportunity (before insert) {
//loop through all new opportunities saved
for(Opportunity Op: Trigger.new)
{
// if the Opportunity amount is greater than 500000
if (Op. Amount_c >= 500000)
//and if Opportunity Vertical (customized pick list field) value = Financial
If (Op. Vertical_c) = 'Financial'
// create a user task that ...
Task T = new Task();
T.Type = 'Email';
T.Description = Opportunity was created with amount of above 500,000 in your speciality field'; //string
T.OwnerId = Op.OwnerId;
//T.WhatId = ''; //record id
insert T;
}else
{
// if the anual amount is less than 500000 and not financial vertical do nothing ....
}
}
}
Trigger
Apex Class
Other than that, not sure why you need a task, you could just create a workflow rule using that criteria and fire an email alert.
Magdiel, thanks for the code input I appreciate it. The reason I'm using an Apex trigger is that in the next step my client wants to assign those opportunities type tasks to a specidific financial team role based specializing in those type of opportunities. I just wanted to make sure that the conditional part of the trigger works first. I assume the task command at this first part will send email to me as a test as the opportunity owner and task owner.
This is the error I got from the code input your provided. I'm new to this so your input on making this trigger work is appreciated.
the error on line 29 :"Duplicate variable: tList"
You should be creating 2 components as per the code provided by Magdiel,one Apex Class and another Apex Trigger.
I can see you are merging both into one.
@Ben as @Vinit_Kumar said, I splitted the logic into one apex trigger, and one apex class,
If it makes sense for the business then it makes sense for us if nothing more is available, I was about to mention if you knew about the big deal alert in salesforce, to accomplish this requirement the only one thing I'll suggest is this line of code requires your attention,
You'll need to change your trigger to run on after insert as that's the only way you'll have access to the record's id, so try this trigger instead,
Once your trigger is up and running with no issues, it comes the time when additional processing can be added to your helper class, this is one of many approaches, I tried to cover the basics on some best practices, including the email you need to send.
Hope that helps.
public
class
OpportunityTriggerHelper
02
03
public
static
void
createTasks(
List
<Task
>
tList){
04
try
{
05
insert
tList;
06
}
07
catch
(Exception err){
08
system.debug(
'\n Error while creating tasks. \n'
09
+ err.getMessage() +
' \n'
10
+ err.getStackTraceString());
11
}
12
}
13
14
}
When creating a task using the Salesforce UI, you have an option to send an email, but when using Apex code, you don't have this feature,
I found these comments,
No it's not a flag you could set on the Task
http://salesforce.stackexchange.com/questions/14205/which-field-represents-the-send-notification-email-checkbox-present-on-the-tas
More than one are suggesting a workaround which I do not recommend,
http://salesforceapexcodecorner.blogspot.com/2011/07/send-email-when-create-task-using-apex.html?_sm_au_=i7HMnRRPv7rkFMrr
http://blog.wdcigroup.net/2013/11/salesforce-apex-send-notification-when-task-created-from-apex/
The solution I suggested, uses the successful creation of a task to decide if an email should be sent to the opp owner or not, it involves apex, and yes you need all the code, plus the code coverage,
But there are as many other solutions you could try,
Your requirement #2 reads "Create a task to send an email to opportunity owner", I assumed the email alert was dependent upon the task creation and assignment, no task then no email alert,
We already have the task created only for our oppportunities (evaluating the vertical), we need then to check for the task to be successfuly created to then fire the email if your requirement is correct, in which case the last code you posted won't be sufficient UNLESS,
You find a way up from the successful creation of a task, to a workflow based on the opportunity to fire the email alert,
If the 2 conditions together (above 500k and vertical "finance" from a pick list type field ) are not met, then no task and no email. The two conditions together need to happen to execute the task. It's most important that it will create a task for the opportunity owner in those conditions. Email alert to the opportunity owner is just a bonus extra alert to the task here.
added your after Trigger and those are the two errors:
Notice we needed to use a map which contains the opp info, you might want to restrict this to a more granular information, choise is yours,
This is the updated solution,
APEX TRIGGER
APEX CLASS
Thanks,
Hi, Any input on that on that Debug log and what ismissing to create that conditional assigned task work? Need to provides some results to the client in the next 3days.
Thanks,
It seems like it's not evaluating to true.
" if (Op.
Amount
>
=
500000
& Op.
Vertical__c
=
=
'Finance'
)
Activity was created but can't get into the task and doesn't see the task description line: "T.Description = 'Opportunity was created with amount of above 500,000 in your speciality field'; (see opportunity record screenshot below).Am I missing something here regarding getting into the task? Im new to this so appreciate your input.
You just need to set the Name and Subject for the task and change the search layout to display the columns you want on the related list.
Hi Magdiel, I truly appreciate your valuable input and contribution with this use case. I just wanted to make sure I didn't miss anything with the Apex code. This is my first Salesforce project so please be patient with me .
Now that we got the main part of the conditional working the final part of the logic is just to add the vertical task assignment to the vertical specific vertical user team:
1.//and if Op. Vertical_c = Finance then assign that Opportunity task to financial user team role
2. // and if Op. Vertical_c = Retail then assign opportunity task to Retail user team
I assume I will have to set up a and ID based Queue for Financial role team and Queue team for the Retail role for the retail user team role. I guess I will need to change my object from Opportunity to leads since salesforce doesn't allow building role based queues for the opportunity object.
I would appreciate if you can your insight for a code reference for this final part of the trigger. so this is what we got so farthat works.
Thanks,