You need to sign in to do that
Don't have an account?
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
Final:
All Answers
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
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
Try this:
Let me know if you face any issues. :)
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);
Oops, i made a mistake in hurry,Try this:
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
Again a mistake,
try this,
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.
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
Final: