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
Afzaal HassanAfzaal Hassan 

Update a given list and its child objects

Hello,
I have created a lightning flow where I have gotten a list of accounts based on criteria/information input by a user. I am trying to take this list of accounts and update its owner values and all the child contacts for eac account in the list. So it is a list within a list. I know I have to use map for it but I am not sure what I am doing wrong/how to get started. Here is what I have:
public with sharing class LPP_UpdateOwner {
    public static void updateAccountOwner(List<List<Account>> accounts, newName){ //accounts would be the input list from the flow and is a list/colection of accounts that the flow queried. newName would be the name the user typed in the flow
List<Account> aUpdated = new List<Account>(); //should this be a map??
for( Account a: accounts){
      a.AccountOwner__c = newName;
      aUpdated.add(a)
}
update aUpdated;

    Map<Id, Opportunity> oppList = new Map<Id, Opportunity>([SELECT Id, Name from Opportunity where AccoundId IN :accounts.keySet() and Stage !='Closed Won']);
List<Opportunity> oppToUpdate = new Opportunity();
for (Opportunity opp :oppList.values()){
       opp.OwnerId = account.ownerId ? not sure what to put here
       oppToUpdate.add(opp);
}
update OpptoUpdate;


So I am not sure if this is correct or not. Basically, I am trying to update all te accounts and each account's opportunity with a new name thats provided. Also, The reason why I am trying to use Maps is to avoid CPU Time limit because doing this in flow and process builder is casuing CPU time out errors.
Thank you