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
Salesforce Dev in TrainingSalesforce Dev in Training 

Apex Trigger - Does Not Contain?

Quick question...

Does anyone know how to add "does not contain" to an Apex trigger (if that's a possibility - if not, I may need a second 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" on 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?

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)
Save
Final Subject = Call (Inbound)

If a user edits and saves, then what happens is this:
Subject = Call (Inbound) (Inbound)
and it keeps adding the Task Result (Inbound) to the end of the Subject.

Temoc MunozTemoc Munoz
Hi again.

You have the following context variables available to you:

Trigger.isUpdate
Trigger.isInsert
and more..
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
 
// You can try this:
trigger TaskTrigger on Task (before insert, before update) {
    for(Task ta: Trigger.new) {
        if(Trigger.isUpdate)
        {
           if(ta.Task_Results__c != null && ta.Subject != ta.Task_Results__c) {
               ta.Subject += ' ('+ta.Task_Results__c + ')';
           }
        }
    }
}

Also, I suggest you put rour logic in a separate class (i.e. TaskTrgHandler) with a static method :
trigger TaskTrigger on Task (before insert, before update) { 
   if(Trigger.isUpdate)
   {
      TaskTrgHandler.myMethod(Trigger.new);
   }
}
 
class TaskTrgHandler
{
   public static void myMethod(List<Task> tasks)
   {
     for(Task ta: tasks) {
     { 
       if(ta.Task_Results__c != null && ta.Subject != ta.Task_Results__c) 
       { 
          ta.Subject += ' ('+ta.Task_Results__c + ')'; }
       } 
   }
}


 
Temoc MunozTemoc Munoz
Please change the context variable according to your requirement (i.e. Trigger.isInsert or Tigger.isUpdate)

If it's only once, try Trigger.isInsert then.
Salesforce Dev in TrainingSalesforce Dev in Training

When I try that it does fix the issue, however it's added the "Task Results" value to the Subject twice.
 

For example, if Subject is blank, we have code that adds "Task Results" to the Subject field.

By doing this code as well, it's adding it twice, that's why I added that extra clause that ta.Subject != ta.Task_Results_c

Salesforce Dev in TrainingSalesforce Dev in Training

So ultimately I want this code:

trigger TaskTrigger on Task (before insert) {
    for(Task ta: Trigger.new) {
        if(ta.Task_Results__c != null && ta.Subject != ta.Task_Results__c) {
            ta.Subject += ' ('+ta.Task_Results__c + ')';
        }
    }
}

PLUS an additional trigger (I'm guessing) for when a Task is edited to do the same thing. I had "before update" in this string before, but what it was doing was adding Task Results every time someone hit edit and save, so some Task Subjects I had to correct were "Call (Inbound - HWN) (Inbound - HWN) (Inbound - HWN) (Inbound - HWN)" because they edited it so many times.

Salesforce Dev in TrainingSalesforce Dev in Training
When I attempt this code (as I follow with your explanation, thank you), the problems say "unexpected Token - String" in line 1
Salesforce Dev in TrainingSalesforce Dev in Training
Would I keep "trigger TaskTrigger on Task (before insert) {
    for(Task ta: Trigger.new) {" at the beginning?
Temoc MunozTemoc Munoz
Not sure what's going on. My posts are not visible..
Salesforce Dev in TrainingSalesforce Dev in Training
Unfortunately that still didn't add the "Task Results" to the end of the "Subject" after hitting Edit and Save.
Salesforce Dev in TrainingSalesforce Dev in Training
Can I shoot you an email? or we do a gtm? Your posts keep delaying I believe. I get the email notifications, but they don't appear on here for a few minutes after the emails.
Temoc MunozTemoc Munoz

It seems I put too much data in one post.

What code did you try last?

If not possible, we can do either an email or collabedit chat.

Thanks
 
Salesforce Dev in TrainingSalesforce Dev in Training

trigger TaskTrigger on Task (before insert) {
    for(Task ta: Trigger.new) {

if(ta.Task_Results__c != null && ta.Subject != ta.Task_Results__c) 
{             
  if(Trigger.isUpdate)
  {   
     // As long as the Task result is enclosed in parentheses. Otherwise this is not scalable   
     String[] currentSubject = ta.subject.split('\\(');  
     if(currentSubject.size() > 0) 
     {  
       ta.Subject = currentSubject[0] + '('+ta.Task_Results__c + ')'; 
     } 
  }  
}

Salesforce Dev in TrainingSalesforce Dev in Training
Did that post?
Temoc MunozTemoc Munoz
Yeah. Thanks.

I tried the code in my developer org and worked. Added the missing else. What's the output of ta.subject before and after?
if(Trigger.isUpdate)
{
  if(ta.subject != null)
  {
      // Not scalable if parenthesese become non-delimiters in the future
      String[] currentSubject = ta.subject.split('\\(');
      if(currentSubject.size() > 0)
      {
         ta.subject = currentSubject[0] + '('+ta.Task_Results__c + ')';
      }
      else
      {
         ta.Subject += '('+ta.Task_Results__c + ')';
      }
  }
}

 
Salesforce Dev in TrainingSalesforce Dev in Training

I really appreciate your help! Should I include the "rigger TaskTrigger on Task (before insert) {
    for(Task ta: Trigger.new) {" before the "IF" statement?

 

Salesforce Dev in TrainingSalesforce Dev in Training
What's not happening is when I edit the Task, and add a Task Result, it doesn't get added to the Subject after hitting Save.
Temoc MunozTemoc Munoz
You got it man.

I suggest this, but you will need to test it first:
trigger TaskTrigger on Task (before insert, before update) {
    for(Task ta: Trigger.new) {
       if(trigger.isInsert)
        {
            if(ta.Task_Results__c != null && ta.Subject != ta.Task_Results__c) {
               ta.Subject += ' ('+ta.Task_Results__c + ')';
            }
       }
       else if(Trigger.isUpdate)
       {
            if(ta.subject != null) 
            {   
               // Not scalable if parenthesese become non-delimiters in the future 
               String[] currentSubject = ta.subject.split('\\(');   
               if(currentSubject.size() > 0) 
               { 
                  ta.subject = currentSubject[0] + '('+ta.Task_Results__c + ')'; 
               }   
               else 
               {    
                  ta.Subject += '('+ta.Task_Results__c + ')'; 
               } 
            }
       }
    }
}

 
Salesforce Dev in TrainingSalesforce Dev in Training
WOW! This looks great! The only issue is If Subject = Task_Results_c then it shouldn't duplicate the value. So this works exactly how I need it except if Subject is left blank then Task_Results_c (auto populates to the Subject due to previous code I've had written), so all of this is correct except we need to add a rule that says if Subject=Task_Results_c then don't add Task_Results_c again to Subject. Does that make sense? Other than that, this is perfect. Although ideally I'd want a " " between the Subject and Task Result, so it looks like Call (Inbound) as opposed to Call(Inbound), does that make sense?
Temoc MunozTemoc Munoz
You're correct! Nice catch.
 I don't have access to my org at the moment,so please modify the code if needed once you test it.
trigger TaskTrigger on Task (before insert, before update) {
    for(Task ta: Trigger.new) {
       if(trigger.isInsert)
        {
            if(ta.Task_Results__c != null && ta.Subject != ta.Task_Results__c) {
               ta.Subject += ' ('+ta.Task_Results__c + ')';
            }
       }
       else if(Trigger.isUpdate)
       {
            if(ta.subject != null && ta.Subject != ta.Task_Results__c)
            {  
               // Not scalable if parenthesese become non-delimiters in the future
               String[] currentSubject = ta.subject.split('\\(');  
               if(currentSubject.size() > 0)
               {
                  ta.subject = currentSubject[0] + ' ('+ta.Task_Results__c + ')';
               }  
            }
       }
    }
}


Test Cases:
1.
Insert
Subject = Call
Result = (Inbound)
Output = Call (Inbound)  ==> Subject

Update
Subject = Call
Result = (Inbound2)
Output = Call (Inbound2)  ==> Subject

2.
Insert
Subject = 
Result = (Inbound)
Output = (Inbound) ==> Subject

Update
Subject = 
Result = (Inbound2)
Output = (Inbound2)  ==> Subject

3.
Insert
Subject = Call
Result = (Inbound)
Output = Call (Inbound) ==> Subject

Update
Subject = Call
Result = Call
Output = Call (Inbound)  ==> Subject

I added the same condition for the update part. Also, I removed the else condition but please go ahead and test it and let me know what you think.
Salesforce Dev in TrainingSalesforce Dev in Training

Thank you so much! You are the man! Seriously, I cannot thank you enough. The one glitch that's very minor, but I'm not sure if it's possible to remove would be the following...

If I hit Edit and Save and don't select a Task Results, the Subject gets "(null)" inserted behind it automatically.

For example:

Subject - Call

Edit + Save

Subject - Call (null)

Temoc MunozTemoc Munoz
Try this.

Let me know if you have more requirements before updating the code.

Thanks!
 
trigger TaskTrigger on Task (before insert, before update) 
{     
     String taskResult = ta.Task_Results__c != null ? ' (' + ta.Task_Results__c + ')' : '';
     for(Task ta: Trigger.new) 
     {        
         if(trigger.isInsert)         
         {             
             if(ta.Task_Results__c != null && ta.Subject != ta.Task_Results__c) 
             {                
                ta.Subject += taskResult;             
             }        
         }        
         else if(Trigger.isUpdate)        
         {             
            if(ta.subject != null && ta.Subject != ta.Task_Results__c)             
            {                  
                // Not scalable if parenthesese become non-delimiters in the future                
                String[] currentSubject = ta.subject.split('\\(');                  
                if(currentSubject.size() > 0)                
                {                   
                     ta.subject = currentSubject[0] + taskResult;               
                }               
            }        
         }     
     } 
}


Salesforce Dev in TrainingSalesforce Dev in Training
It says the variable "ta.Task_Results_c" does not exist?
Temoc MunozTemoc Munoz
My bad.

Put this inside the for -loop:
String taskResult = ta.Task_Results__c != null ? ' (' + ta.Task_Results__c + ')' : '';

 
Salesforce Dev in TrainingSalesforce Dev in Training
Can you write the code out, it's not working still :/
Temoc MunozTemoc Munoz
trigger TaskTrigger on Task (before insert, before update) 
{     
     for(Task ta: Trigger.new) 
     {        
         String taskResult = ta.Task_Results__c != null ? ' (' + ta.Task_Results__c + ')' : '';
         if(trigger.isInsert)         
         {             
             if(ta.Task_Results__c != null && ta.Subject != ta.Task_Results__c) 
             {                
                ta.Subject += taskResult;             
             }        
         }        
         else if(Trigger.isUpdate)        
         {             
            if(ta.subject != null && ta.Subject != ta.Task_Results__c)             
            {                  
                // Not scalable if parenthesese become non-delimiters in the future                
                String[] currentSubject = ta.subject.split('\\(');                  
                if(currentSubject.size() > 0)                
                {                   
                     ta.subject = currentSubject[0] + taskResult;               
                }               
            }        
         }     
     } 
}

 
Salesforce Dev in TrainingSalesforce Dev in Training
It still is adding (null) to any Task that doesn't have a Task Result after hitting Edit and Save on that Task :/ Interesting... again, I cannot stress enough how much I appreciate your help.
Temoc MunozTemoc Munoz
OK.

Let's try this. Sorry I don't have a developer org right now where I can test this.

Change Line 05 to this:
String taskResult = !String.isBlank(ta.Task_Results__c) ? ' (' + ta.Task_Results__c + ')' : ''; 
Salesforce Dev in TrainingSalesforce Dev in Training
Wow! That worked! You're the best! Thank you so much Temoc. If I notice any inconsistencies, I'll keep you posted. Thank you so much for your time, energy, and effort. It really means a lot to me and our non-profit org.
Temoc MunozTemoc Munoz
You got it man!

I'm glad it's working now!
Salesforce Dev in TrainingSalesforce Dev in Training

I do have one additional question... I noticed that pre-exiting Tasks and new Tasks that we create that contain a parenthesis gets replaced with the Task Result after hitting save. For example: 

Old Subject - Call (The People)
Task Result - Outbound

New Subject - Call (Outbound)

So the Task Result is replacing the current parenthesis instead of just adding to it. Is this possible to change to have the Task Result just add to the end of the Subject?

Salesforce Dev in TrainingSalesforce Dev in Training

Sorry about my delay. I tried this code. If I hit Edit and Save without selecting a Task Result, then it reads "null".

Example - RED2 (CALL)null

RED2 (CALL) was the Subject. I hit EDIT and SAVE without selecting a Task Result and got the result above.

Salesforce Dev in TrainingSalesforce Dev in Training
Thanks for your help on this! Now what it's doing is every time I hit EDIT and SAVE it's duplicating the Task Result to the Subject line (even if it's already there).

I just created a Task called "Phoner2" with a Task Result of Outbound. I hit save and it was "Phoner2 (Outbound)" which at this point is correct, but then when I hit EDIT and SAVE again, it changed the Subject to Phoner2 (Outbound) (Outbound)"
Temoc MunozTemoc Munoz
Follow this link please. I wil share my code with you so this way we can test faster:
http://collabedit.com/rxh4d
Salesforce Dev in TrainingSalesforce Dev in Training
I still cannot get this code to execute correctly :/
Salesforce Dev in TrainingSalesforce Dev in Training
Temoc, did you get my message?