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 Question - Activities/Tasks

I am trying to write apex code for the Activities/Task object.

We have a custom picklist field titled "Task Results".

I want to have whatever is selected in "Task Results" to automaticcaly append/add-to whatever is typed into the Task Subject in parenthesis.

For example, if Subject is "Call" and someone selected "Inbound" from the Task Results picklist, I want the Subject to then be changed to "Call (Inbound)".

Please let me know if that makes sense. 
Best Answer chosen by Salesforce Dev in Training
Mahesh DMahesh D
//
// Trigger on Task to populate the Subject.
//
trigger TaskTrigger on Task (before insert, before update) {
    for(Task ta: Trigger.new) {
        if(ta.Task_Results__c != null && (ta.Subject == null || ta.Subject == '')) {
            ta.Subject += ' ('+ta.Task_Results__c + ')';
        }
    }
}

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Andrew,

Please use the below code:
 
//
// Trigger on Task to populate the Subject.
//
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 + ')';
        }
    }
}

Please do let know if it helps you.

Regards,
Mahesh
Salesforce Dev in TrainingSalesforce Dev in Training
Thank you Mahesh, I forgot one step. I need this to trigger if Subject is not equal to null. For example, there was code written already that if Subject = null then make Subject = Task Results. Well with this trigger what it's doing is duplicating that value due to the previous code. So we need this trigger to run only if Subject is not equal to null.
Salesforce Dev in TrainingSalesforce Dev in Training
Other than that, your answer is exactly what I need!
Salesforce Dev in TrainingSalesforce Dev in Training
So with this new code written... if Subject = null it is displaying Subject = Call (Call). We don't want that duplicate value. We need to add a restriction. I tried adding && ta.Subject != null to the if statement but that didn't work.
Mahesh DMahesh D
//
// Trigger on Task to populate the Subject.
//
trigger TaskTrigger on Task (before insert, before update) {
    for(Task ta: Trigger.new) {
        if(ta.Task_Results__c != null && (ta.Subject == null || ta.Subject == '')) {
            ta.Subject += ' ('+ta.Task_Results__c + ')';
        }
    }
}

Regards,
Mahesh
This was selected as the best answer
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 + ')';
        }
    }
}
Mahesh DMahesh D
Hi Andrew,

Below code will execute only in insert scenario and Task Results changes in the update scenario.
 
//
// 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(Trigger.isInsert || ta.Task_Results__c != Trigger.oldMap.get(ta.Id).Task_Results__c) {
			if(ta.Task_Results__c != null && ta.Subject != '' && ta.Subject != null) {
				ta.Subject += ' ('+ta.Task_Results__c + ')';
			}
		}
    }
}

Regards,
Mahesh
Salesforce Dev in TrainingSalesforce Dev in Training
Thank you Mahesh, 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? I just removed "before update" from the code in the Sandbox and now it's working as needed, but I can't seem to figure out the next method in updating the current code in Production.
Salesforce Dev in TrainingSalesforce Dev in Training
In essence I just want to replace that trigger with a new one.
Salesforce Dev in TrainingSalesforce Dev in Training
I created the change set and pushed to Production. The issue I'm worried about before deploying, is there a chance that both triggers will exist? or will it automatically replace the existing once? Thank you for the links by the way.
Mahesh DMahesh D
Please make sure that you are updating the same trigger in Sandbox and deploy the same one. Don't create a new trigger if you want to replace the excisting one.

If you deploy new one then both the triggers will exists in the production.

Regards,
Mahesh
Salesforce Dev in TrainingSalesforce Dev in Training
Thank you!, I appreciate all your help Mahesh.
Salesforce Dev in TrainingSalesforce Dev in Training
Quick question...

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?
Mahesh DMahesh D
Hi Andrew,

You can use the below code and it will fire only if you change the Task Result.
 
//
// 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(Trigger.isInsert || ta.Task_Results__c != Trigger.oldMap.get(ta.Id).Task_Results__c) {
			if(ta.Task_Results__c != null && ta.Subject != '' && ta.Subject != null) {
				ta.Subject += ' ('+ta.Task_Results__c + ')';
			}
		}
    }
}

Regards,
Mahesh