My current Apex Trigger places the last activity subject into a custom field I created for both leads and opportunities. I am rather unfamiliar with triggers and I am still unsure exactly what I can do with them. I was wondering if I could add something like a does not contain function to prevent any activity subjects with a certain word from being placed into the field. I tried adding t.subject.doesnotcontain('word') to line 11 of my code, but that did not work. Does anyone have any suggestions?
Here is my code:
trigger updateRelatedLeadOrOpportunity on Task (after insert,after update) {
List<Id> OpportunityIds = new List<Id>();
List<Opportunity> OpportunityList = new List<Opportunity>();
List<Id> LeadIds = new List<Id>();
List<Lead> LeadList = new List<Lead>();
List<Task> taskToProcess = new List<Task>();
for(Task t :trigger.new)
{
//This will make sure that we only process the task who's subject is added/updated
if(t.status == 'Completed' && ((trigger.isInsert | trigger.isUpdate) || trigger.newMap.get(t.id).subject != trigger.oldMap.get(t.id).subject)){
//You'll have WhoId populated when the Task related to a Lead/Contact(only)
//and whatId will be populated if the task is realted to Account/Opportunity/Custom Objects...
if(t.whatId !=null)
{
Schema.SObjectType tType= t.whatid.getSObjectType();
if(tType == Opportunity.Schema.SObjectType)
{
OpportunityIds.add(t.Whatid);
taskToProcess.add(t);
}
}
if(t.whoId != null){
Schema.SObjectType tType= t.whoId.getSObjectType();
if(tType == Lead.Schema.SObjectType)
{
LeadIds.add(t.whoId);
taskToProcess.add(t);
}
}
}
}
//Querying the related Opportunity as well as Lead based on whatid on Task
map<Id, Opportunity> oppsToUpdateMap = new map<Id, Opportunity>([select id,Last_Activity_Subject__C from Opportunity where id in:OpportunityIds]);
Map<Id,Lead> LeadMap = new Map<Id,Lead>([select id,Last_Activity_Subject__C from Lead where id in:LeadIds]);
//Now we have all the tasks that we need to process in this loop
for(Task t :taskToProcess){
if(t.whatid!=null)
{
Schema.SObjectType tType= t.whatId.getSObjectType();
if(tType == Opportunity.Schema.SObjectType){
Opportunity opp = oppsToUpdateMap.get(t.whatId);
opp.Last_Activity_Subject__C = t.subject;
oppsToUpdateMap.put(opp.Id, opp);
}
}
if(t.whoId!=null){
Schema.SObjectType tType= t.whoId.getSObjectType();
if(tType == Lead.Schema.SObjectType){
Lead lead = LeadMap.get(t.whoId);
lead.Last_Activity_Subject__C = t.subject;
LeadList.add(lead);
}
}
}
// updating the Opportunity and lead
update oppsToUpdateMap.values();
if(!LeadList.isEmpty()) update LeadList;
}
!s.contains('abc')
means "s does not contain 'abc'".