• santhoshkumar Bhojan 14
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 5
    Replies
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)
Caselookup 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,
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)
Caselookup 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,