I have this SOQL:

SELECT Id, mudouProprietario__c,(SELECT Id, AccountId, OwnerId  FROM Contacts) FROM Account

I need get value of owner account and set the value at the contact owner

I created a for passing the list that returns the select

        Task[] listaTarefaUpdate = new Task[] {};
        Opportunity[] listaOportunidadeUpdate = new Opportunity[] {};
        Contact[] listaContatosUpdate = new Contact[] {};

        Account[] listaContas = (Account[]) scope;

        for( Account contas : listaContas){

            for(Task task: contas.Tasks){

                task.OwnerId = contas.OwnerId; 

            for(Opportunity oportunidade : contas.Opportunities){

                oportunidade.OwnerId = contas.OwnerId;

            for(Contact contato : contas.Contacts){

                contato.OwnerId = contas.OwnerId;

            contas.mudouProprietario__c = false;


        Database.SaveResult[] srListOpt = Database.update(listaOportunidadeUpdate, false);
        Database.SaveResult[] srListCont = Database.update(listaContatosUpdate, false);
        Database.SaveResult[] srListTar = Database.update(listaTarefaUpdate, false);

It would be like this?

  • you can use declarative way using process builder – Santanu Boral Feb 10 at 16:12

It would be far simpler to approach this from the opposite direction; query the contacts and the account owner in a child-parent relationship instead of a parent-child relationship:

Contact[] records = [SELECT Account.OwnerId FROM Contact WHERE AccountId <> NULL];
for(Contact record: records) {
  record.OwnerId = record.Account.OwnerId;
update records;

For many different children, then you might do something like this:

Account[] records = [SELECT OwnerId, (SELECT Id FROM Contacts), (SELECT Id FROM Opportunities WHERE IsClosed = false) FROM Account];
sObject[] updates = new sObject[0];
for(Account record: records) {
  for(Contact contactRecord: record.Contacts) {
    updates.add(new Contact(Id=contactRecord.Id, OwnerId=record.Id));
update updates;
for(Account record: records) {
  for(Opportunity opportunityRecord: record.Opportunities) {
    updates.add(new Opportunity(Id=opportunityRecord.Id, OwnerId=record.Id));
update updates;

This isn't the only way to do so, but one of several different possibilities.

  • And I have more relationships in this select, I put only the contact to simplify example SELECT Id, changed PropertyId, (SELECT Id, AccountId, OwnerId FROM Contacts), (SELECT Id, AccountId, OwnerId, IsClosed FROM Opportunities WHERE IsClosed = false) how would I put this select in its context? – rolnid Feb 10 at 16:27
  • @rolnid That information would have been useful in the question. I've updated my answer to reflect this new information. – sfdcfox Feb 10 at 16:42
  • I rewrote the code, can you take a look? – rolnid Feb 10 at 17:28
  • I am doing a batch class – rolnid Feb 10 at 17:44
  • Tanks your answer helped a lot – rolnid Feb 10 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.