You need to sign in to do that
Don't have an account?
update contact reference field on case
I have a trigger that I am firing on cases before update. After the contacts are created, I am trying to update the contact lookup (reference) field on the case.
Here is what I am trying:
trigger InsertCaseContact on Case (before update) { //Create a new list for caseIds List<Id> caseIds = new List<Id>{}; //Loop through the Cases we're adding and build a list of Ids for(Case cas: Trigger.new) caseIds.add(cas.Id); List<Contact> addContacts = new List<Contact>(); // Loop through the Trigger.new array of Cases. for(Case cas:Trigger.new){ if (cas.SuppliedName != null) { addContacts.add(new Contact (LastName = cas.SuppliedName, Email = cas.SuppliedEmail, Phone = cas.SuppliedPhone, Email_ExtID__c = cas.SuppliedEmail)); } } upsert addContacts Email_ExtID__c; // loop thru contacts to retrieve contact ids List<Contact> newContacts = new List<Contact>(); for (Contact c: addContacts) { newContacts.add(c); } List<Case> cases = new List<Case>{}; for (Case c : Trigger.new) { c.ContactId = newContacts.Id; cases.add(c); } update cases; }
Save error: Initial term of field expression must be a concrete SObject: LIST:SOBJECT:Contact
How can I update the contact reference field in the cases with with the list of new contacts?
Thanks for any help.
// Create list of cases of the trigger
List<Case> cases = new List<Case>();
// Loop through the cases and add to list
for(Case c : Trigger.new)
cases.add(c);
//Now in the last section do the changes
for(Integer i=0; i<newcontact.size(); i++)
{
Case c1 = caese.get(i);
c1.contactId = newcontact[i] .Id;
}
All Answers
It occurs in the very last loop in the trigger:
for (Case c : Trigger.new) { c.ContactId = newContacts.Id; cases.add(c); }
On this line:
c.ContactId = newContacts.Id;
I cannot get the before insert trigger to update the contact (reference field) on the case to update in my trigger. I tried an after insert and the record is read only so that did not work either.
Here are both triggers that I attempted. Maybe someone can point out how I can accomplish this?
Before insert trigger:
trigger InsertCaseContact on Case (before insert) { //Create a new list for caseIds List<Id> caseIds = new List<Id>{}; //Loop through the Cases we're adding and build a list of Ids for(Case cas: Trigger.new) caseIds.add(cas.Id); List<Contact> addContacts = new List<Contact>(); // Loop through the Trigger.new array of Cases. for(Case cas:Trigger.new){ if (cas.SuppliedName != null) { addContacts.add(new Contact (LastName = cas.SuppliedName, Email = cas.SuppliedEmail, Phone = cas.SuppliedPhone, Email_ExtID__c = cas.SuppliedEmail)); } } upsert addContacts Email_ExtID__c; // loop thru contacts to retrieve contact ids //Set<Id> ConIds = new Set<Id>(); List<Contact> newContacts = new List<Contact>(); for (Contact c: addContacts) { newContacts.add(c); } List<Case> cases = new List<Case>{}; for (Case c : Trigger.new) { for (Integer i=0; i < newContacts.size(); i++) { c.ContactId = newContacts[i].Id; } } }
After insert trigger:
trigger InsertCaseContact on Case (after insert) { //Create a new list for caseIds List<Id> caseIds = new List<Id>{}; //Loop through the Cases we're adding and build a list of Ids for(Case cas: Trigger.new) caseIds.add(cas.Id); List<Contact> addContacts = new List<Contact>(); // Loop through the Trigger.new array of Cases. for(Case cas:Trigger.new){ if (cas.SuppliedName != null) { addContacts.add(new Contact (LastName = cas.SuppliedName, Email = cas.SuppliedEmail, Phone = cas.SuppliedPhone, Email_ExtID__c = cas.SuppliedEmail)); } } upsert addContacts Email_ExtID__c; // loop thru contacts to retrieve contact ids //Set<Id> ConIds = new Set<Id>(); List<Contact> newContacts = new List<Contact>(); for (Contact c: addContacts) { newContacts.add(c); } List<Case> cases = new List<Case>{}; for (Case c : Trigger.new) { for (Integer i=0; i < newContacts.size(); i++) { c.ContactId = newContacts[i].Id; cases.add(c); } } update (cases); }
Is the reason the before insert does not work is because I'm creating new contacts and then trying to add those ids into a case that hasn't been created yet?
Is there a solution?
// Create list of cases of the trigger
List<Case> cases = new List<Case>();
// Loop through the cases and add to list
for(Case c : Trigger.new)
cases.add(c);
//Now in the last section do the changes
for(Integer i=0; i<newcontact.size(); i++)
{
Case c1 = caese.get(i);
c1.contactId = newcontact[i] .Id;
}