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
Ramana123Ramana123 

Whenever an account owner changed then the realted contacts owner also should be chnaged to Account owner and vice versa

any one help me in this code please
Best Answer chosen by Ramana123
CharuDuttCharuDutt
Hii srikanth yeturu
Try the Following Code Just Copy Paste It.
trigger ownerUpdate on Contact (before Update) {
	Set<Id> relatedAccountId = new Set<Id>();
	List<Account> updatedList = new List<Account>();
    string ownerid;
    for(Contact con: trigger.new){
     if(con.AccountId != Null){
       relatedAccountId.add(con.AccountId);
       ownerid = con.OwnerId;
     }
   }

    for(Account acc: [Select Id, Name ,OwnerId from Account where id in : relatedAccountId]){
    acc.OwnerId = ownerid;
	updatedList.add(acc);
	}
	update updatedList;
}
Please Mark It As Best Answer If It Helps.
Thank you...

All Answers

CharuDuttCharuDutt
Hii srikanth yeturu

Changing the Account owner automatically changes the Contact owner.
Same principle applies to changing the Case owner, if case has tasks then Task's assigned to will get changed.
If you dont want to change the contact owner based on account ownership then you need to write a trigger to handle this.

Please Mark It As Best Answer If It Helps.
Thank you...
Ramana123Ramana123
Hii soni, Thanks for your reply, when contact owner change then account owner should be changed . can you help me in this code please.
CharuDuttCharuDutt
Hii srikanth yeturu
Try the Following Code Just Copy Paste It.
trigger ownerUpdate on Contact (before Update) {
	Set<Id> relatedAccountId = new Set<Id>();
	List<Account> updatedList = new List<Account>();
    string ownerid;
    for(Contact con: trigger.new){
     if(con.AccountId != Null){
       relatedAccountId.add(con.AccountId);
       ownerid = con.OwnerId;
     }
   }

    for(Account acc: [Select Id, Name ,OwnerId from Account where id in : relatedAccountId]){
    acc.OwnerId = ownerid;
	updatedList.add(acc);
	}
	update updatedList;
}
Please Mark It As Best Answer If It Helps.
Thank you...
This was selected as the best answer
Ramana123Ramana123
Hi soni, I am getting below error , how to solve this problem EmailException:SendEmail failed. First exception on row 0; first error: SINGLE_EMAIL_LIMIT_EXCEEDED, Email limit exceeded: [] *global class LeadDays7 implements Database.Batchable{ List listRecords = new List(); global Database.QueryLocator start(Database.BatchableContext BC) { String query = 'Select Id,Name,Email,CheckBox__c,Leadage__c From Lead Where Leadage__c > 7 LIMIT 10 '; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List scope){ System.debug('aaaaaaaaaaaaaaaaaaaa'+scope); for(Lead leads : scope) { if(leads.Email != NULL) { leads.CheckBox__c = TRUE; } listRecords.add(leads); } update listRecords; List emailList = new List(); System.debug('aaaaaaaaaaaaaaaaaaaa'+scope); for(Lead ld: scope) { if(ld.Email != NULL) { emailList.add(ld.Email); } } System.debug('bbbbbbbbbbbbbbbbbbbbb'+emailList); Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); email.setToAddresses(emailList); email.setSaveAsActivity(false); email.setSubject('Age is 7 days'); email.setPlainTextBody('Batch Update for Opportunity Close Date has been completed'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); } global void finish(Database.BatchableContext BC){ }}*