You need to sign in to do that
Don't have an account?
santhoshkumar Bhojan 14
Trigger to update lookup field in its related objects
Hi, Could you please help me with the below Apex trigger. I am new to coding and SFDC as well .I am not sure what went wrong. Here is my code
Contact-lookup with case(Case_c)
Caselookup with Opportunity (Opportunity_c)and Contact(contact)
Opportunity lookup with Contact(contact_c)
Whenever I update case_c field in Contact,it should update case object and its related opportunity (I mean the lookup field should be updated with contact name automatically)
trigger Contacttriggertoupdatecase1 on Contact (before update) { Set<Id> contactids=new Set<Id>(); Map<Id,case> oppcase=new Map<Id,case>(); for(contact CON:Trigger.new) { contactids.add(con.id); } List<contact> conlist=[Select Id,FirstName,Name,case__r.id from contact where Id IN:contactids]; List<case> caselist=[Select Id,contactid from case where contactId IN: contactids]; if(Caselist.size()>0) { for(Integer i=0;i<caselist.size();i++) { if(Trigger.isbefore&&Trigger.isupdate) { if(Trigger.New[i].case__c!='NULL') { Caselist[i].contactid=Trigger.New[i].id; } } } } //Child Parent SOQL query to retrieve its related opportunity List<case> caseopplist=[Select Id,caseNumber,Opportunity__r.id from Case where id IN:caselist]; for(case co:caseopplist) { //Adding case and its related opportunity id in Map oppcase.put(co.Opportunity__r.id,co); } //Querying related opportunities List<opportunity> opplisttoupdate=[Select id,contact__c from opportunity where id in:oppcase.keyset()]; if(opplisttoupdate.size()>0) { for(contact con:Trigger.New) { for(opportunity o:opplisttoupdate) { o.contact__c =Trigger.New[0].id; o.contact__c=con.id; //opplisttoupdate[l].contact__c =Trigger.new[l].id; opplisttoupdate.add(o); } } update opplisttoupdate; } } Regards,
Contact-lookup with case(Case_c)
Caselookup with Opportunity (Opportunity_c)and Contact(contact)
Opportunity lookup with Contact(contact_c)
Whenever I update case_c field in Contact,it should update case object and its related opportunity (I mean the lookup field should be updated with contact name automatically)
trigger Contacttriggertoupdatecase1 on Contact (before update) { Set<Id> contactids=new Set<Id>(); Map<Id,case> oppcase=new Map<Id,case>(); for(contact CON:Trigger.new) { contactids.add(con.id); } List<contact> conlist=[Select Id,FirstName,Name,case__r.id from contact where Id IN:contactids]; List<case> caselist=[Select Id,contactid from case where contactId IN: contactids]; if(Caselist.size()>0) { for(Integer i=0;i<caselist.size();i++) { if(Trigger.isbefore&&Trigger.isupdate) { if(Trigger.New[i].case__c!='NULL') { Caselist[i].contactid=Trigger.New[i].id; } } } } //Child Parent SOQL query to retrieve its related opportunity List<case> caseopplist=[Select Id,caseNumber,Opportunity__r.id from Case where id IN:caselist]; for(case co:caseopplist) { //Adding case and its related opportunity id in Map oppcase.put(co.Opportunity__r.id,co); } //Querying related opportunities List<opportunity> opplisttoupdate=[Select id,contact__c from opportunity where id in:oppcase.keyset()]; if(opplisttoupdate.size()>0) { for(contact con:Trigger.New) { for(opportunity o:opplisttoupdate) { o.contact__c =Trigger.New[0].id; o.contact__c=con.id; //opplisttoupdate[l].contact__c =Trigger.new[l].id; opplisttoupdate.add(o); } } update opplisttoupdate; } } Regards,
All Answers
What Error you are facing. Your question is not clear. Still as per my usderstanding I am posting a snippet. Let me know if you have any doubts.
trigger Contacttriggertoupdatecase1 on Contact (before update) {
for(contact CON:Trigger.new) {
newContactMap.put(con.id,con);
}
for(contact CON:Trigger.old) {
oldContactMap.put(con.id,con);
}
List<contact> conlist=[Select Id,FirstName,Name,case__r.id from contact where Id IN:newContactMap.keySet()];
List<case> caselist=[Select Id,contactid from case where contactId IN: newContactMap.keySet()];
List<case> upsertList = new List<case>();
map<id,contact> newContactMap = new map<id,contact>();
map<id,contact> oldContactMap = new map<id,contact>();
for(contact con : trigger.new){
if(newContactMap.get(con.id) == oldContactMap.get(con.id)){
if(newContactMap.get(con.id).case_c != oldContactMap.get(con.id).case_c){
for(case c :caselist){
if(c.id == con.case__c && c.id != null){
upsertList.add(c);
}
}
}
}
}
if(upsertList.size()>0){
upsert upsertList;
}
Error: Compile Error: Variable does not exist: case_c at line 15 column 43.
for(Case c : caseList){
mapOfCaseByCaseId.put(c.Id, c);
But in line 24,you are retreiving as contact field i.e Case c = mapOfCaseByCaseId.get(ct.case__c).Can you please give me some headsup on this .I am completely confused
Regards,
Santhosh Kumar
Regards,
Santhosh Kumar