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
kishore cmxkishore cmx 

Hi Experts, Iam created text field name is Account Owner in contact object.When i change account owner in Account Object that will affeted on the account owner field in contact Object?( How to write trigger please eplain each and every step?)

Amit Chaudhary 8Amit Chaudhary 8
Hi Kishore cmx,

Please try below code to update contact field on after account udate .
trigger AccountTrigger on Account ( after insert, after update ) 
{

	Set<String> setAccountID = new Set<String>();
	For(Account acc: trigger.New)
	{
		setAccountID.add(acc.id);
	}
	
	List<Account> listAccount =[select id,name,ownerid, (Select, firstName, LastName,
ownerid from Contacts) from account where id in :setAccountID ];
	
    List<Contact> lstContactToUpdate = new List<Contact>();
	For( Account acc: listAccount )
	{
		System.debug('----------->'+acc.Name);
		List<Contact> lstContact = acc.Contacts;
		for(Contact cont : lstContact)
		{
          cont.ownerid = acc.ownerid
          lstContactToUpdate.add(cont);
 

			System.debug('----------->'+cont.firstName);
			// Add all your logic here
		}
	}
    if(lstContactToUpdate.size() > 0)
	{
      update lstContactToUpdate;
    }
}

Please make this as solution if this will help you.
 
ManojjenaManojjena
HI Kishore,

Tryt with below code and let me know if you hav eany doubt .I think you can understand eaisly .
 
trigger ContactOwnerUpdate on Account (after update ) {
   List<Contact> conListToUpdate=new List<Contact>();
   Map<Id,Account> accMap=new Map<Id,Account>();
   for(Account acc: trigger.New){
     //Condition to check an dcollect those accountid whose owner changed  on update 
     if(Trigger.oldMap.get(acc.Id).OwnerId) != acc.OwnerId{
	    accMap.put(acc.Id,acc);
	 }
  }
  if(!accMap.isEmepty()){
	  For(Contact con :[SELECT id,AccountOwner__c,AccountId FROM Contact WHERE AccountId IN : accMap.keySet()]){
		 con.AccountOwner__c=accMap.get(AccountId).OwnerId;
		 conListToUpdate.add(con);
	  }
  }
  try{
      update conListToUpdate;
  }catch(DmlException de ){
     System.debug(de);
  }
}

Thanks 
Manoj
Dinesh PDinesh P
Hi Kishore,

Use formula field instead of trigger. Don't user trigger for sunch small stuff. Create one formula text field on contact.  Assign Account.OwnerId to newly created forumula text field.

Thanks,
dinesh
kishore cmxkishore cmx
Thank manoj , But Iam getting this (Compile Error: unexpected token: '!=' at line 6 column 44) Error
ManojjenaManojjena
Hi Kishore ,

Try with below code 
trigger ContactOwnerUpdate on Account (after update ) {
   List<Contact> conListToUpdate=new List<Contact>();
   Map<Id,Account> accMap=new Map<Id,Account>();
   for(Account acc: trigger.New){
     //Condition to check an dcollect those accountid whose owner changed  on update 
     if(Trigger.oldMap.get(acc.Id).OwnerId != acc.OwnerId){
	    accMap.put(acc.Id,acc);
	 }
  }
  if(!accMap.isEmepty()){
	  For(Contact con :[SELECT id,AccountOwner__c,AccountId FROM Contact WHERE AccountId IN : accMap.keySet()]){
		 con.AccountOwner__c=accMap.get(AccountId).OwnerId;
		 conListToUpdate.add(con);
	  }
  }
  try{
      update conListToUpdate;
  }catch(DmlException de ){
     System.debug(de);
  }
}

 
kishore cmxkishore cmx
Hi manoj AccountID variable does not exit Plz modify the code
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
trigger ContactOwnerUpdate on Account (after update ) {
   List<Contact> conListToUpdate=new List<Contact>();
   Map<Id,Account> accMap=new Map<Id,Account>();
   for(Account acc: trigger.New){
     //Condition to check an dcollect those accountid whose owner changed  on update 
     if(Trigger.oldMap.get(acc.Id).OwnerId != acc.OwnerId){
	    accMap.put(acc.Id,acc);
	 }
  }
  if(!accMap.isEmepty()){
	  For(Contact con :[SELECT id,AccountOwner__c,AccountId FROM Contact WHERE AccountId IN : accMap.keySet()]){
		 con.AccountOwner__c=accMap.get(con.AccountId).OwnerId;
		 conListToUpdate.add(con);
	  }
  }
  try{
      update conListToUpdate;
  }catch(DmlException de ){
     System.debug(de);
  }
}


 
ManojjenaManojjena
Hi Kishore ,

Actually you need to retrive AccountId with contact variable .You can replac  the code like below .
accMap.get(AccountId).OwnerId; to accMap.get(con.AccountId).OwnerId; 

Thanks 
Manoj

 
kishore cmxkishore cmx
Hi manoj, In which scenario you go for custom settings ? explain one or two scenarios?
kishore cmxkishore cmx
Explain CRM life cycle in salesforce in step by step process? ( I faced interview question)