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
Amit_Amit_ 

Insert/ Update Account and opportunity on picklist filed insert on contact.

Hi , I am new to salesforce so Have some issue with myrequirement.

I have a simple requirement, where I have a picklist filed on Contact object and same picklist on Account and opportunities, I have a requirement where on Insert or Update of Picklist filed in contact the same should be insert or updated in picklist filed of Account and its related Opportunity picklist filed object.

any help and suggestion is highly appricated.

 

Thanks & Reagrds,

 

 

souvik9086souvik9086

Try this

 

trigger UpdatePkVal on Contact(after insert, after update){
Contact conObj = [select id,name,Accountid, PicklistField__c from Contact where id =: Trigger.new[0].id];
Account accObj;
List<Opportunity> oppList = new List<Opportunity>();
accObj = [select id,name, PicklistField__c,(select id, name, PicklistField__c from Opportunities) from account where id =: conObj.Accountid];
accObj.PicklistField__c = conObj.PicklistField__c;
upsert accObj;
for(Opportunity opp: accObj.Opportunities){
opp.PicklistField__c = conObj.PicklistField__c;
oppList.add(opp):
}
if(oppList.size()>0){
upsert oppList;
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

SamReadySamReady

Try this solution. It is bulkified and covers both the insert and update case for a contact. Being that Contacts are not directly related to Opportunity (see OpportunityContactRole), you cannot directly update Opportunities. I created a map to store contact ids as keys, and contacts as values. Update accounts directly for new contacts where the picklist isn't null, or update if the values change. Then query for opportunity contacts based off of the contact ids in that keyset.

 

trigger updateAccOpp on Contact (after insert, before update) {
List<Account> acctsToUpdate = new List<Account>();
Map<String, Contact> contacts = new Map<String, Contact>();
List<Opportunity> oppsToUpdate = new List<Opportunity>();

if(trigger.isInsert){
   for(Contact c: trigger.new){
      if(c.picklistField__c != null){
         c.Account.SamePicklistField__c = c.picklistField__c;
         acctsToUpdate.add(c.Account);
         contacts.put(c.Id, c);
      }
   }

   List<OpportunityContactRole> oppContacts = [select OpportunityId, ContactId from OpportunityContact where ContactId IN: contacts.keySet()];
   for(OpportunityContactRole oc : oppContacts){
   Contact tmp = contacts.get(oc.ContactId);
   oc.Opportunity.SamePicklistField__c = tmp.pickListField__c;
   oppsToUpdate.add(oc.Opportunity);
   }
} // end isInstert

if(trigger.isUpdate){
   for(Contact c: trigger.new){
      Contact oldContact = trigger.oldMap.get(c.Id);
      if(c.picklistField__c != oldContact.picklistField__c){
         c.Account.SamePicklistField__c = c.picklistField__c;
         acctsToUpdate.add(c.Account);
         contacts.put(c.Id, c);
      }
   }

   List<OpportunityContactRole> oppContacts = [select OpportunityId, ContactId from OpportunityContact where ContactId IN: contacts.keySet()];
   for(OpportunityContactRole oc : oppContacts){
      Contact tmp = contacts.get(oc.ContactId);
      oc.Opportunity.SamePicklistField__c = tmp.pickListField__c;
      oppsToUpdate.add(oc.Opportunity);
   }
} //end isUpdate

 

update oppsToUpdate;

update acctsToUpdate;
}

Amit_Amit_

Hi Sam, 

 

Thanks for your reply. I have tried your method in my coding I am not getting any error but when I am trying to insert new contact or udate contact at that point I am getting error as :

AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.contactAll for the red colored line in code 

if(trigger.isInsert){
   for(Contact c: trigger.new){
      if(c.picklistField__c != null){
         c.Account.SamePicklistField__c = c.picklistField__c;
         acctsToUpdate.add(c.Account);
         contacts.put(c.Id, c);
      }
   }

and same for update :

if(trigger.isUpdate){
   for(Contact c: trigger.new){
      Contact oldContact = trigger.oldMap.get(c.Id);
      if(c.picklistField__c != oldContact.picklistField__c){
         c.Account.SamePicklistField__c = c.picklistField__c;
         acctsToUpdate.add(c.Account);
         contacts.put(c.Id, c);
      }
   }

can you please guide me why I am getting this error.

 

Thanks & Regards,

 

SamReadySamReady

It needs to be a before update in order to use a trigger.oldMap. I had made it after insert or before update in the sample above.