You need to sign in to do that
Don't have an account?
Activities/Tasks Apex Question
Thanks to Mahesh D I was able to create a trigger that successfully performed the desired operation, however I have an additional question to the Community.
The code Mahesh helped me with is the following:
01//
02// Trigger on Task to populate the Subject.
03//
04trigger TaskTrigger on Task (before insert, before update) {
05 for(Task ta: Trigger.new) {
06 if(ta.Task_Results__c != null) {
07 ta.Subject += ' ('+ta.Task_Results__c + ')';
08 }
09 }
10}
What this code is doing is adding whatever a user selects in the picklist "Task Results" to the end of the Task Subject inside of parenthesis. The issue is I only want this trigger if the Subject is not equal to null. I tried adding code that said && ta.Subject != null on the "if" clause but that didn't work successfully. I have code written in my org that says if Subject = null then Subject = Task Result. What this is doing now with the code written above is duplicating the Subject if it's left blank.
Subject = Null
Task Result = Inbound Call
Hit Save, then
Subject = Inbound Call (Inbound Call)
^ I do not want the duplicate value.
if ( ta.subject.containsNone( ta.Task_result__c ) {}
You can also add ta.Task_Results__c only when the operation is an insert by either saying if trigger.isInsert or if !trigger.isUpdate
But probably the best way is to update subject when the value of the subject or the value of task_result__c have changed regardless of whether it is an insert or update. You can do this by comparing values of the fields using Trigger.new and Trigger.old. You can read about this here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm) and bydoing some searches on this forum for information about using trigger.old.
All Answers
Well, here's the code that I have which works great except one issue. Every time a user hits edit / save it duplicates the Task Result value over and over again.
1
2
3
4
5
6
7
8
9
10//
// Trigger put on Task to populate the Subject and add Task Results with parenthesis per Angela.
//
trigger TaskTrigger on Task (before insert, before update) {
for(Task ta: Trigger.new) {
if(ta.Task_Results__c != null && ta.Subject != ta.Task_Results__c) {
ta.Subject += ' ('+ta.Task_Results__c + ')';
}
}
}
For example, I have the following code:
trigger TaskTrigger on Task (before insert, before update) {
for(Task ta: Trigger.new) {
if(ta.Task_Results__c != null && ta.Subject != ta.Task_Results__c) {
ta.Subject += ' ('+ta.Task_Results__c + ')';
}
}
}
What's happening is every time a user "Edits" and "Saves" a Task, it's duplicating the Task Result value in the Subject line. I only want this to happen once. Is there a way to add "and ta.Subject DOES NOT CONTAIN ta.Task_Results_c" or ONLY UPDATE ONCE?
Ex:
Subject - Call / Task Result - (Inbound) -- Final Subject = Call (Inbound)
If a user edits and saves, then what happens is this Call (Inbound) (Inbound)
and it keeps adding the Task Result to the end of the Subject.
if ( ta.subject.containsNone( ta.Task_result__c ) {}
You can also add ta.Task_Results__c only when the operation is an insert by either saying if trigger.isInsert or if !trigger.isUpdate
But probably the best way is to update subject when the value of the subject or the value of task_result__c have changed regardless of whether it is an insert or update. You can do this by comparing values of the fields using Trigger.new and Trigger.old. You can read about this here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm) and bydoing some searches on this forum for information about using trigger.old.