You need to sign in to do that
Don't have an account?
how to update child from parent
Hello there.
I have got a simple request and i don't know where I am going wrong:
Account has a field named: Case_Contact___c.id (its Contact lookup) and want this same Contact name updated pn all Account related Case:ContactName (lookup) field using class.
Class written:
public class updateCaseContactFieldFromAcc {
public static void updateCaseList (list <Contact> ConList){
List <Contact> lstContact = [select Account.id,Name from Contact where id in: ConList];
List<Account> lstAccount =[select Case_Contact__r.id from Account where Case_Contact__r.id in: Conlist ];
List<case> lstCase = [select Contact.id, Account.Id from Case where Account.id in: lstAccount];
}
for(contact c: ConList){
for(Account a: lstAccount){
for(case ca: lstCase){
ca.Contact.ID = c.Id;
}
}
}update lstCase;
}
}
---------------------
trigger on Contact:
trigger updateCaseContactFieldFromAccTrigger1 on Contact (before insert, before update) {
updateCaseContactFieldFromAcc.updateCaseList(trigger.new); }
and this does not work.
I got an advice from expert stating 3 lists is not a Good practice and need to use Map here, but dont kow how to proceed.
Any help guys ?
Harshil you were right on the mentioned points.
rest I got my code cut to short, meeting my exact requirement.
CLASS------------
public class updateCaseContactFieldFromAcc {
public Static void UpdateListCase (list <Account> lstAccount){
list<case> lstCase = [select Contact.id, Account.Id from Case where Account.id != null and Account.id in: lstAccount];
for(case ca: lstCase){
for(Account a: lstAccount){
if(ca.AccountId != null){
ca.ContactId = a.Case_Contact__c;
}
}
}update lstCase;
}
}
TRIGGER---------------
trigger updateCaseContactFieldFromAccTrigger on Account (after insert, after update) {
updateCaseContactFieldFromAcc.UpdateListCase(trigger.new);
}
All Answers
I think you are updating Cases' contact ID same as an Account's Case_Contact__c field, so your trigger needs to be on Account object though.
Harshil you were right on the mentioned points.
rest I got my code cut to short, meeting my exact requirement.
CLASS------------
public class updateCaseContactFieldFromAcc {
public Static void UpdateListCase (list <Account> lstAccount){
list<case> lstCase = [select Contact.id, Account.Id from Case where Account.id != null and Account.id in: lstAccount];
for(case ca: lstCase){
for(Account a: lstAccount){
if(ca.AccountId != null){
ca.ContactId = a.Case_Contact__c;
}
}
}update lstCase;
}
}
TRIGGER---------------
trigger updateCaseContactFieldFromAccTrigger on Account (after insert, after update) {
updateCaseContactFieldFromAcc.UpdateListCase(trigger.new);
}