You need to sign in to do that
Don't have an account?
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.
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
Also, I suggest you put rour logic in a separate class (i.e. TaskTrgHandler) with a static method :
If it's only once, try Trigger.isInsert then.
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
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.
for(Task ta: Trigger.new) {" at the beginning?
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
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 + ')';
}
}
}
I tried the code in my developer org and worked. Added the missing else. What's the output of ta.subject before and after?
I really appreciate your help! Should I include the "rigger TaskTrigger on Task (before insert) {
for(Task ta: Trigger.new) {" before the "IF" statement?
I suggest this, but you will need to test it first:
I don't have access to my org at the moment,so please modify the code if needed once you test it.
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.
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)
Let me know if you have more requirements before updating the code.
Thanks!
Put this inside the for -loop:
I'm glad it's working now!
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?
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.
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)"
http://collabedit.com/rxh4d