function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ali MeyerAli Meyer 

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);
    }
    }

 

Best Answer chosen by Admin (Salesforce Developers) 
Sgt_KillerSgt_Killer

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

Sgt_KillerSgt_Killer

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();
}

This was selected as the best answer
Ali MeyerAli Meyer
Of course! Thank you so much.
Ali MeyerAli Meyer

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.)

 

trigger updateClinical2 on Clinical_Intake_Object__c (after update, after insert)
{
Map<Id, Contact> consMap = new Map<Id, Contact>();
Set<id> Ids = new Set <id>();
for (Clinical_Intake_Object__c cio: Trigger.new)
{
Ids.add(cio.Parent_Name_2__c);
}
Map<id, Contact> consMap2 = new Map<id, Contact>([SELECT Id FROM Contact WHERE Id in :Ids]);
for (Clinical_Intake_Object__c t: Trigger.new)
if (t.Parent_Name_2__c != null && t.Canceled__c!= true && t.Clinician_Assigned__c !=null && t.CMI_Appointment_Date__c !=null)
{
Contact c = consMap2.get(t.Parent_Name_2__c);
List<Contact> consList = [SELECT Name, Id FROM Contact WHERE Id in :Ids];

List<RecordType> rtypes = [SELECT Name, Id FROM RecordType WHERE sObjectType = 'Contact' and isActive = true];
Map<String, String> contactRecordTypes = new Map<String, String>{};
for(RecordType rt: rtypes)
    contactRecordTypes.put(rt.Name, rt.Id);

c.RecordTypeId = contactRecordTypes.get('Household Contact');  
upsert consMap.values();
consMap.put(c.Id,c);

}}