顶点 CPU 限制。在 apex class 中使用地图而不是列表

APEX CPU Limit. Use maps instead of lists in apex class

我创建了一个流程,它根据一些要求查找帐户列表。然后,流程将此帐户列表和新帐户所有者 (id) 传递给顶点 class。顶点 class 然后用这个新所有者更新所有帐户,并用相同的帐户所有者更新每个机会和每个 activity 下列出的每个任务。在我说更新了大量帐户之前,这一切都很好。我现在达到 APEX CPU 极限。我的顶点class如下图。我想我需要使用地图,但我不知道如何使用。关于如何重写此代码以使其更高效以便我不 运行 进入 APEX CPU 限制的任何想法?谢谢

public class LCD_AccountinCounty {
    @InvocableMethod(label='Account Owner Update flow' Description='Update Account Object with new owner')
    public static void updateAccountOwner(List<FlowDetail> flowdetails) {

        List<Account> accList = new List<Account>();

        for(FlowDetail fd : flowdetails){
            for(Account acct : fd.accounts){
                acct.OwnerId = fd.newAccountOwnerName;
                acc.Salesperson__c = SalespersonName;
                accList.add(acct);
            }
        }

        update accList;

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

        for(Opportunity opp: [SELECT Id, OwnerId, AccountId, Account.OwnerId FROM Opportunity WHERE AccountId IN :accList and StageName !='Closed']){
            opp.OwnerId = opp.Account.OwnerId;

            opportunities.add(opp);

        }

        update opportunities;

        List<Task> activities = new List<Task>();

        for(Task t: [SELECT Id, OwnerId, WhatId, Account.OwnerId FROM Task WHERE WhatId IN :accList]){
            t.OwnerId = t.Account.OwnerId;

            activities.add(t);

        }

        update activities;


    }


    public with sharing class FlowDetail{
        @InvocableVariable
        public List<Account> accounts;

        @InvocableVariable
        public String newAccountOwnerName;

        @InvocableVariable
        public String SalespersonName;

    }

}

这些对象上是否有触发一些额外逻辑的触发器?

您不能将包装器 class 传递给批处理 class。 但是,您可以使用 Queueable 接口传入复杂的数据类型。

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

您可以将上面的代码移动到 Queueable class 中并像下面这样将其入队:

   public class LCD_AccountinCounty {
        @InvocableMethod(label='...' Description='...')
        public static void updateAccountOwner(List<FlowDetail> flowdetails) {
            AsyncExecutionExample a = new AsyncExecutionExample(flowdetails);
            System.enqueueJob(a);
        }
    }

    public class AsyncExecutionExample implements Queueable {
        public LCD_AccountinCounty.FlowDetail flowdetails;
        public AsyncExecutionExample(LCD_AccountinCounty.FlowDetail flowdetails){
            this.flowdetails = flowdetails;
        }
        public void execute(QueueableContext context) {
            *Old updateAccountOwner code goes here...*      
        }
    }