Update to question:
So I have created an invokable variable/method where the flow is passing the following parameters:
public static void updateAccountOwner(List<TransferDetail> transferDetails) {
for(TransferDetail td : transferDetails) { //THIS IS WHERE I AM COMPLETELY UNSURE ABOUT, AND I WAS TOLD A MAP WOULD BE BETTER TO BULKFY?
for(Account a : td.accounts){
// Account a = new Account(Id = td.a.accountId, AccountOwner__c = td.a.newOwnerId);
// accounts.add(a);
// }
}
}
public with sharing class Request{
@InvocableVariable
public List<Account> accounts;
@InvocableVariable
public String newAccountOwnerName;
@InvocableVariable
public String oldAccountOwnerName;
}
So each account in this list of accounts has opportunities of its own. But each account in the list will be updated with the same new owner, and also each opportunity will be transfered to the same owner
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 each 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, newOwnerId){ //accounts would be the input list from the flow and is a list/colection of accounts that the flow queried. newOWNERID 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 = newOwnerId;
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 = aUpdated.AccountOwner__c ? not sure what to put here(opportunity owner and account owner ha to be same, which is newNameId
oppToUpdate.add(opp);
}
update OpptoUpdate;
So I am not sure if this is correct or not. Basically, I am trying to update all the 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
it is a list within a listbut you're only passing in a single owner Id so in your current setup, all those are going to be set to the same owner. - nerdy beast