1
votes

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

1
Are you saying that every account that is passed into this method is getting set to the same owner? I ask because you state about the accounts being passed in that it is a list within a list but 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
@nerdybeast yes. So I will have a list of accounts, all owned by John. I will pass that list of accounts and a name, lets say Mark. All accounts in that list need to be transferred to mark. Also, each account in the list has opportnities for example. Each of those opportunities will need to be owned now by Mark from John - user3685949
Another problem im running into is that only one input parameter can be used in an @invokable method. so i can either pass a list of accounts or the name to the class - user3685949
I left an answer below that I think is what you're after - nerdy beast

1 Answers

0
votes
//I would recommend renaming this class, "Request" is far too generic...
public class Request {

    @InvocableVariable
    public List<Account> accounts;

    @InvocableVariable
    public String newAccountOwnerName; //Is this the name of a user or the actual user Id???

    @InvocableVariable
    public String oldAccountOwnerName; //This property isn't needed...
}
public class InvocableLPPOwnerUpdate {

    @InvocableMethod(label='Assign New Owner' description='Will assign new ownership for the given account')
    public static void updateAccountOwner(List<Request> requests) {

        //You don't need a map in this situation, this approach is bulkified already because once you update
        //the owner on the Account, you can just read the owner from there when updating the opportunities
        List<Account> accounts = new List<Account>();

        for(Request req : requests) {
            for(Account acct : req.accounts) {
                acct.AccountOwner__c = req.newAccountOwnerName;
                accounts.add(acct);
            }
        }

        update accounts;

        List<Opportunity> opportunities = new List<Opportunity>();

        for (Opportunity opp : [SELECT Id, Name, Account.AccountOwner__c FROM Opportunity WHERE AccoundId IN :accounts and Stage !='Closed Won']) {

            //No Map needed because we can get the correct owner from the linked Account that was already updated above
            opp.OwnerId = opp.Account.AccountOwner__c;

            opportunities.add(opp);
        }

        update opportunities;
    }
}