+ Start a Discussion
Glenn Soden 9Glenn Soden 9 

Looking for a little Trigger help that fires off key words in Activity Subject Line

I'm new to Apex coding and trying to create a simple trigger off an Activty Subject.  On the Lead object, if the Activity subject mentions "Do Not Call" (though not the only text of subject) the trigger needs to tag the Lead record's check box field called Do Not Call.   Basically tagging the lead as a do not call off the words Do Not Call on the subject line of the Activity.  Any tips will be great! 

Thanks in Advance
Best Answer chosen by Glenn Soden 9
LBKLBK
Hi Glenn,

Thank you for the nice use case.

Here is the trigger that could help you.
 
trigger trgDoNotCall on Task (after update, after insert) {

    List<Id> lstLeadIds = new List<Id>();
    for (Task task : trigger.new){
        system.debug('task.WhoId : ' + task.WhoId);
        system.debug('task.Who.Type : ' + task.Who.Type); 
        if(task.WhoId != null){
            String sSubject = task.subject;
            System.debug('sSubject ' +  sSubject);
            System.debug('sSubject.indexOf ' + sSubject.indexOf('Do Not Call'));
            if((sSubject.indexOf('Do Not Call') > -1) || (sSubject.indexOf('DoNotCall') > -1)){ 
                lstLeadIds .add(task.WhoId);
            	system.debug('task.WhoId : ' + task.WhoId);
            }
        } 
    }
    
    if(lstLeadIds.size() > 0){
        List<Lead> lstLeads = [SELECT Id, DoNotCall FROM Lead WHERE Id =: lstLeadIds];
        for( Lead lead : lstLeads){
            lead.DoNotCall = true;
            //lead.Description += 'Do Not Call'; 
            System.debug('lead : ' + lead);
        }
        update lstLeads;
    }
}

Let me know if this helps.
 

All Answers

LBKLBK
Hi Glenn,

Thank you for the nice use case.

Here is the trigger that could help you.
 
trigger trgDoNotCall on Task (after update, after insert) {

    List<Id> lstLeadIds = new List<Id>();
    for (Task task : trigger.new){
        system.debug('task.WhoId : ' + task.WhoId);
        system.debug('task.Who.Type : ' + task.Who.Type); 
        if(task.WhoId != null){
            String sSubject = task.subject;
            System.debug('sSubject ' +  sSubject);
            System.debug('sSubject.indexOf ' + sSubject.indexOf('Do Not Call'));
            if((sSubject.indexOf('Do Not Call') > -1) || (sSubject.indexOf('DoNotCall') > -1)){ 
                lstLeadIds .add(task.WhoId);
            	system.debug('task.WhoId : ' + task.WhoId);
            }
        } 
    }
    
    if(lstLeadIds.size() > 0){
        List<Lead> lstLeads = [SELECT Id, DoNotCall FROM Lead WHERE Id =: lstLeadIds];
        for( Lead lead : lstLeads){
            lead.DoNotCall = true;
            //lead.Description += 'Do Not Call'; 
            System.debug('lead : ' + lead);
        }
        update lstLeads;
    }
}

Let me know if this helps.
 
This was selected as the best answer
Glenn Soden 9Glenn Soden 9
Worked like a charm LBK.  Esp once I realized I didn't have to create the Do Not Call Field as it was already there, as a standard, but just hiding off page layout and visability.  As you mentioned, it is a very good use casefor the lead object and managing Do Not Call contacts.