+ Start a Discussion
KevSnellKevSnell 

Trigger Task on Contact Update only when certain contact fields are filled in help!

Hi All,

 

Hope someone can help as I'm still new to developing and trying to learn.

 

I have created a trigger to create a task when a contact record is saved and certain fields are filled in. However, this works fine but if someone goes into the record later and updates some other fields and saves because the fields are already filled in it creates another task. 

 

How can I get the code below to recognise that if a task called '30 Day Call' is already created against the contact to stop and not create another duplicate task?

 

 

trigger AssignNewMemberTask on Contact (after update) {
    List<Task> taskList = new List<Task>();
    for(integer i=0; i<trigger.new.size(); i++)
    {

        if ((trigger.new[i].Joined__c==true)&&(trigger.new[i].Joined_Date__c!=NULL)) {
         Task t = new Task(
         Subject = '30 Day Call',
         WhoId = trigger.new[i].id,
         Description = 'Call this Contact.',
         Priority = 'High',
         Status = 'Not Started',
         ActivityDate = trigger.new[i].Joined_Date__c+30,
         ReminderDateTime = System.now().addDays(2));
 
             if(trigger.new[i].Vistage_MSC__c!=null){
             t.OwnerId = trigger.new[i].Vistage_MSC__c;
             }else{
             t.OwnerId = '005E0000000YhNn';
             }
         taskList.add(t);
        }


    }
    insert taskList;
}

 

 

Many thanks

Kev

Best Answer chosen by Admin (Salesforce Developers) 
Rahul S.ax961Rahul S.ax961

Final:

 

 

trigger AssignNewMemberTask on Contact (after update)
{
 Set<Id> setContact = new Set<Id>();
 for(Task objTask: [Select Id, WhoId from Task where WhoId In: trigger.new and Subject = '30 Day Call'])
   setContact.add(objTask.WhoId);
 List<Task> taskList = new List<Task>();
 for(integer i=0; i<trigger.new.size(); i++)
 {
   if(!setContact.contains(trigger.new[i].Id))
     if (trigger.new[i].Joined_Date__c!=NULL && trigger.new[i].Joined__c==true)
     {
     Task t = new Task(
     Subject = '30 Day Call',
     WhoId = trigger.new[i].id,
     Description = 'Call this Contact.',
     Priority = 'High',
     Status = 'Not Started',
     ActivityDate = trigger.new[i].Joined_Date__c+30,
     ReminderDateTime = System.now().addDays(2));
     if(trigger.new[i].Vistage_MSC__c!=null)
     {
       t.OwnerId = trigger.new[i].Vistage_MSC__c;
     }
         else
     {
       t.OwnerId = '005E0000000YhNn'; // Advice - Instead of hardcording, use a query like this
       //   t.OwnerId = [Select Id from user Where Name = 'Name of your User' limit 1].Id;
     }
     taskList.add(t);
     setContact.add(trigger.new[i].Id);
      }
 }
 if(!taskList.isEmpty())
    insert taskList;
}

 

 

All Answers

Pratibh PrakashPratibh Prakash

Hello,

 

Retrieve all the task under the Contact where Subject='30 Day Call'

 

Logic goes like this:

 

1. Put all the contact ids in a Set say: ContactIds

2. Retrieve all the task where WhoId in ContactIds and Subject='30 Day Call'

3. Iterate all the task retrieved and create a Map which stores ContactId and corresponding 30 Day Call task record. Say Contact_TaskMap

4. Now start iterating on all the contact and do task creation decesion as:

       if Contact_TaskMap.get(ContactId) == null i.e. no 30 Day Call task under the Contact

             Create Task

      Else

           Do Nothing

 

Regards-

PP

 

 


KevSnellKevSnell

Thanks PP,

 

Is it possible to show me the code I need as I'm still learning and not 100% sure how to do the above.

 

Kev

Rahul S.ax961Rahul S.ax961

Try this:

 

 

trigger AssignNewMemberTask on Contact (after update) 
{
Set<Contact> setContact = new Set<Contact>();
for(Task objTask: [Select Id from Task where WhoId In: trigger.new and Subject = '30 Day Call'])
setContact.add(objTask.WhoId);
List<Task> taskList = new List<Task>();
for(integer i=0; i<trigger.new.size(); i++)
{
if(!setContact.isEmpty() && !setContact.contains(trigger.new[i].Id))
if (trigger.new[i].Joined_Date__c!=NULL && trigger.new[i].Joined__c==true)
{
Task t = new Task(
Subject = '30 Day Call',
WhoId = trigger.new[i].id,
Description = 'Call this Contact.',
Priority = 'High',
Status = 'Not Started',
ActivityDate = trigger.new[i].Joined_Date__c+30,
ReminderDateTime = System.now().addDays(2));
if(trigger.new[i].Vistage_MSC__c!=null)
{
t.OwnerId = trigger.new[i].Vistage_MSC__c;
}
else
{
t.OwnerId = '005E0000000YhNn'; // Advice - Instead of hardcording, use a query like this
//
t.OwnerId = [Select Id from user Where Name = 'Name of your User' limit 1].Id;
}
taskList.add(t);
setContact.add(trigger.new[i].WhoId);
}
}
if(!taskList.isEmpty())
insert taskList;
}

Let me know if you face any issues. :)

KevSnellKevSnell

Thanks Rahul

 

However, on Save I get the error message:

 

Error: Compile Error: Incompatible element type Id for collection of SOBJECT:Contact at line 5 column 4

 

This seems to be referring to this line:    setContact.add(objTask.WhoId);

 

Rahul S.ax961Rahul S.ax961

Oops, i made a mistake in hurry,Try this:

 

 

trigger AssignNewMemberTask on Contact (after update) 
{
Set<Id> setContact = new Set<Id>();
for(Task objTask: [Select Id from Task where WhoId In: trigger.new and Subject = '30 Day Call'])
setContact.add(objTask.WhoId);
List<Task> taskList = new List<Task>();
for(integer i=0; i<trigger.new.size(); i++)
{
if(!setContact.isEmpty() && !setContact.contains(trigger.new[i].Id))
if (trigger.new[i].Joined_Date__c!=NULL && trigger.new[i].Joined__c==true)
{
Task t = new Task(
Subject = '30 Day Call',
WhoId = trigger.new[i].id,
Description = 'Call this Contact.',
Priority = 'High',
Status = 'Not Started',
ActivityDate = trigger.new[i].Joined_Date__c+30,
ReminderDateTime = System.now().addDays(2));
if(trigger.new[i].Vistage_MSC__c!=null)
{
t.OwnerId = trigger.new[i].Vistage_MSC__c;
}
else
{
t.OwnerId = '005E0000000YhNn'; // Advice - Instead of hardcording, use a query like this
// t.OwnerId = [Select Id from user Where Name = 'Name of your User' limit 1].Id;
}
taskList.add(t);
setContact.add(trigger.new[i].WhoId);
}
}
if(!taskList.isEmpty())
insert taskList;
}

 

 

KevSnellKevSnell

Sorry one more issue:

 

Error: Compile Error: Invalid field WhoId for SObject Contact at line 30 column 36

 

Refers to setContact.add(trigger.new[i].WhoId);

 

Thanks for your help on this.


Kev

Rahul S.ax961Rahul S.ax961

Again a mistake,

try this,

 

setContact.add(trigger.new[i].Id);
KevSnellKevSnell

That has now saved, however, nothing happens:

 

If I edit a contact record with no tasks associated and add Joined_Date__c and tick Joined__c it should create a task called '30 Day Call' but it doesn't.

KevSnellKevSnell

Bit of an update: I added a '30 day call' Task manually and then edited the record.  On Save I get the following:

 

Error:Apex trigger AssignNewMemberTask caused an unexpected exception, contact your administrator: AssignNewMemberTask: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Task.WhoId: Trigger.AssignNewMemberTask: line 5, column 20

Rahul S.ax961Rahul S.ax961

Final:

 

 

trigger AssignNewMemberTask on Contact (after update)
{
 Set<Id> setContact = new Set<Id>();
 for(Task objTask: [Select Id, WhoId from Task where WhoId In: trigger.new and Subject = '30 Day Call'])
   setContact.add(objTask.WhoId);
 List<Task> taskList = new List<Task>();
 for(integer i=0; i<trigger.new.size(); i++)
 {
   if(!setContact.contains(trigger.new[i].Id))
     if (trigger.new[i].Joined_Date__c!=NULL && trigger.new[i].Joined__c==true)
     {
     Task t = new Task(
     Subject = '30 Day Call',
     WhoId = trigger.new[i].id,
     Description = 'Call this Contact.',
     Priority = 'High',
     Status = 'Not Started',
     ActivityDate = trigger.new[i].Joined_Date__c+30,
     ReminderDateTime = System.now().addDays(2));
     if(trigger.new[i].Vistage_MSC__c!=null)
     {
       t.OwnerId = trigger.new[i].Vistage_MSC__c;
     }
         else
     {
       t.OwnerId = '005E0000000YhNn'; // Advice - Instead of hardcording, use a query like this
       //   t.OwnerId = [Select Id from user Where Name = 'Name of your User' limit 1].Id;
     }
     taskList.add(t);
     setContact.add(trigger.new[i].Id);
      }
 }
 if(!taskList.isEmpty())
    insert taskList;
}

 

 

This was selected as the best answer