You need to sign in to do that
Don't have an account?

Updating Contact Field Based on Task Field
I'm trying to write what I think is a really simple trigger: I want to change the contact field "2013 Clinician" if certain criteria are met in a related task. Below is my code: it doesn't have any compile errors but it also just plain doesn't work.
Thank you!
trigger updateConsTask on Task (after update, after insert) { Map<Id, Contact> consMap = new Map<Id, Contact>(); Set<id> Ids = new Set <id>(); for (Task tk: Trigger.new) { Ids.add(tk.WhoId); } Map<id, Contact> consMap2 = new Map<id, Contact>([SELECT Id FROM Contact WHERE Id in :Ids]); for (Task t: Trigger.new) if (t.WhoId != null && t.Status == 'Cultivate') { Contact c = consMap2.get(t.WhoId); c.X2013_Clinician__c = t.Clinician__c; consMap.put(c.id,c); } }
I believe you are not updating the contact records once you modified its value using a DML Statement. Try this out -)
trigger updateConsTask on Task (after update, after insert)
{
Map<Id, Contact> consMap = new Map<Id, Contact>();
Set<id> Ids = new Set <id>();
for (Task tk: Trigger.new)
{
Ids.add(tk.WhoId);
}
Map<id, Contact> consMap2 = new Map<id, Contact>([SELECT Id FROM Contact WHERE Id in :Ids]);
for (Task t: Trigger.new)
if (t.WhoId != null && t.Status == 'Cultivate')
{
Contact c = consMap2.get(t.WhoId);
c.X2013_Clinician__c = t.Clinician__c;
consMap.put(c.id,c);
}
upsert consMap.values();
}
All Answers
I believe you are not updating the contact records once you modified its value using a DML Statement. Try this out -)
trigger updateConsTask on Task (after update, after insert)
{
Map<Id, Contact> consMap = new Map<Id, Contact>();
Set<id> Ids = new Set <id>();
for (Task tk: Trigger.new)
{
Ids.add(tk.WhoId);
}
Map<id, Contact> consMap2 = new Map<id, Contact>([SELECT Id FROM Contact WHERE Id in :Ids]);
for (Task t: Trigger.new)
if (t.WhoId != null && t.Status == 'Cultivate')
{
Contact c = consMap2.get(t.WhoId);
c.X2013_Clinician__c = t.Clinician__c;
consMap.put(c.id,c);
}
upsert consMap.values();
}
I thought I had it but it turns out it's not working! I'm not getting any compile errors, but the code just doesn't do anything, and I can't figure out why not.
What I'm trying to do is change the record type of contacts listed in Parent_Name_2 to Household, if certain requirements on the Clinical Intake Object are met (not canceled, clinician assigned, etc.)