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 

Trigger doesn't work

Hello,

 

I don't get any compile errors, but the thing that's supposed to happen just doesn't when I run this trigger. Basically, I have a contact lookup field on my Clinical_Intake_Object (Parent_Name_2), and when certain criteria on the Clinical lntake Object are met (clinician assigned, not canceled) I want to change the record type of the contact to "Household." Instead, this trigger just keeps the record type the same, and I don't know why.

 

Thank you!

 

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 tk: Trigger.new)
{
Ids.add(tk.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);

}}

 

Best Answer chosen by Admin (Salesforce Developers) 
hpereirahpereira

And now with more user-friendly indentation:

 

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 tk: Trigger.new)
{
Ids.add(tk.Parent_Name_2__c);
}
Map<id, Contact> consMap2 = new Map<id, Contact>([SELECT Id, RecordTypeId 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');  
consMap.put(c.Id,c);
}
upsert consMap.values();
}

 

All Answers

hpereirahpereira

Hello, I believe you have 2 problems:

1) Your query to contact should include field "RecordTypeId";

2) your upsert should be outside the for cycle.

Try the following:

 

trigger updateClinical2 on Clinical_Intake_Object__c (after update, after insert) { Map consMap = new Map(); Set Ids = new Set (); for (Clinical_Intake_Object__c tk: Trigger.new) { Ids.add(tk.Parent_Name_2__c); } Map consMap2 = new Map([SELECT Id, RecordTypeId 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 consList = [SELECT Name, Id FROM Contact WHERE Id in :Ids]; List rtypes = [SELECT Name, Id FROM RecordType WHERE sObjectType = 'Contact' and isActive = true]; Map contactRecordTypes = new Map{}; for(RecordType rt: rtypes) contactRecordTypes.put(rt.Name, rt.Id); c.RecordTypeId = contactRecordTypes.get('Household Contact'); consMap.put(c.Id,c); } upsert consMap.values(); }

hpereirahpereira

And now with more user-friendly indentation:

 

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 tk: Trigger.new)
{
Ids.add(tk.Parent_Name_2__c);
}
Map<id, Contact> consMap2 = new Map<id, Contact>([SELECT Id, RecordTypeId 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');  
consMap.put(c.Id,c);
}
upsert consMap.values();
}

 

This was selected as the best answer