+ Start a Discussion
Salesforce Dev in TrainingSalesforce Dev in Training 

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.

Best Answer chosen by Salesforce Dev in Training
3 Creeks3 Creeks
There are a few ways to do that.  You can use the string method containsNone():
 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

3 Creeks3 Creeks
If subject is null and somewhere else in your code you are making subject == Task Result but not then nulling out Task Result, then Task Result will never eval to null in the above code.  Maybe a better approach is to check if subject and task result have the same value and if they do not, then add task result to subject.  It would look something like this:
 
for(Task ta: Trigger.new) {
  if ( ! ta.Subject.equals( ta.Task_Results__c )  ) {
            ta.Subject += ' ('+ta.Task_Results__c + ')';
       }
 }


 
Salesforce Dev in TrainingSalesforce Dev in Training

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 + ')';
        }
    }
}
 

 

 

Salesforce Dev in TrainingSalesforce Dev in Training
I realized I need to remove "before update", however, I am having problems updating the trigger in Production. What is the best method to do this?
3 Creeks3 Creeks
What kind of problem are you having?
Salesforce Dev in TrainingSalesforce Dev in Training
Do you know how to add "does not contain" to an Apex trigger?
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?
Salesforce Dev in TrainingSalesforce Dev in Training
Do I need another trigger?
Salesforce Dev in TrainingSalesforce Dev in Training
In other words, I don't want the Task Results to be added to the Subject if it's already there.

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.
3 Creeks3 Creeks
There are a few ways to do that.  You can use the string method containsNone():
 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.

 
This was selected as the best answer
Salesforce Dev in TrainingSalesforce Dev in Training
Can I add that string "if ( ta.subject.containsNone( ta.Task_result__c ) {}" to the existing code?
 
3 Creeks3 Creeks
Yes.
Salesforce Dev in TrainingSalesforce Dev in Training
When I do, I keep receiving the unexpected { error.
Salesforce Dev in TrainingSalesforce Dev in Training
I believe I figured it out creating a second trigger with your logic. Thank you.