使用 Salesforce Apex 更新给定列表及其子对象

Update a given list and its child objects using Salesforce Apex

问题更新:

所以我创建了一个可调用的 variable/method,其中流传递以下参数:

    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;

    }

所以此帐户列表中的每个帐户都有自己的机会。但是列表中的每个帐户都将更新为同一个新所有者,并且每个机会都将转移给同一个所有者

我创建了一个闪电流,其中我根据用户输入的 criteria/information 获得了一个帐户列表。我正在尝试获取此帐户列表并更新其所有者值以及列表中每个帐户的所有子联系人。所以它是列表中的列表。我知道我必须为此使用地图,但我不确定我在做什么 wrong/how 才能开始。这是我拥有的:

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;

所以我不确定这是否正确。基本上,我正在尝试使用提供的新名称更新所有帐户和每个帐户的机会。此外,我尝试使用地图的原因是为了避免 CPU 时间限制,因为在流程和流程构建器中这样做会导致 CPU 超时错误。 谢谢

//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;
    }
}