【问题标题】:Update a given list and its child objects using Salesforce Apex使用 Salesforce Apex 更新给定列表及其子对象
【发布时间】:2019-12-03 21:49:24
【问题描述】:

更新问题:

所以我创建了一个可调用的变量/方法,其中流程传递以下参数:

    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;

    }

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

我创建了一个闪电流,其中我获得了基于用户输入的标准/信息的帐户列表。我正在尝试获取此帐户列表并更新其所有者值以及列表中每个帐户的所有子联系人。所以它是列表中的列表。我知道我必须为此使用地图,但我不确定我做错了什么/如何开始。这是我所拥有的:

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;

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

【问题讨论】:

  • 您是说传入此方法的每个帐户都设置为同一所有者吗?我问是因为您声明了在 it is a list within a list 中传递的帐户,但您只传递了一个所有者 ID,因此在您当前的设置中,所有这些都将设置为同一所有者。
  • @nerdybeast 是的。所以我会有一个账户列表,所有账户都归约翰所有。我将通过该帐户列表和名称,让我们说马克。该列表中的所有帐户都需要转移以进行标记。此外,例如,列表中的每个帐户都有机会。这些机会中的每一个现在都需要由来自 John 的 Mark 拥有
  • 我遇到的另一个问题是@invokable 方法中只能使用一个输入参数。所以我可以将帐户列表或名称传递给班级
  • 我在下面留下了一个答案,我认为这就是你所追求的

标签: apex


【解决方案1】:
//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;
    }
}

【讨论】:

  • 感谢您的回复。我想添加的一件事是,流程不是在可调用变量中传递 accountId,而是传递一个帐户列表。那么,如何修改代码?
  • 您能否更新您的问题以显示您如何调用updateAccountOwner()?我想看看数据是如何传入的
  • 我对代码做了一些修改。感谢您的帮助
  • 好的,我知道你现在在做什么,我很快就会在这里编辑我的答案
  • accountownername 变量实际上是一个 id。我应该修改那个变量名。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多