【问题标题】:Create Batch Apex to update multiple objects创建 Batch Apex 以更新多个对象
【发布时间】:2020-01-08 06:05:52
【问题描述】:

我已经有一个调度顶点,它适用于 3 个对象以执行基本查询和更新。我想做这门课。但无法在 Batch 顶点中添加多个对象并围绕它们循环。

这是我的日程表顶点的样子

`global class scheduleWorkday implements Schedulable {

 global void execute(SchedulableContext ctx) {

  //Get Accounts

        List<Account> getbdayaccount = [Select ID, Name, Address from Account where State= CT];

        if(!getbdayaccount .isEmpty()) {
                for(Account a : getbdayaccount ) {
                a.name = 'Test';
                a.State= 'NJ';
            }
            update getbdayaccount ;
        }

//get Leads 

   List<Lead> getPreApprovalFollow = [Select ID, Name, State, LeadSource from Lead where State = 'CT' ];

        if(!getPreApprovalFollow .isEmpty()) {
               for(Lead l: getPreApprovalFollow ) {
                l.LeadSource = 'Referral';
                l.State = 'NJ';
            }
            update getPreApprovalFollow ;
        }

//get Opportunities 

List<Opportunity> getopps = [Select Id, CloseDate, State from Lead where State = 'CT'];

   if(!getopps.isEmpty()){
     for(Opportunity o : getopps){
     o.CloseDate = Date.Today();
      o.State = 'CT';
}
update get opps;


}



}


}`

我尝试制作像这样的批处理顶点 -

global class LeadProcessor implements Database.Batchable <SObject> {
//START METHOD
    global Database.QueryLocator start(Database.BatchableContext bc){
        String Query='Select id,LeadSource, State from Lead where state = 'CT';
        return Database.getQueryLocator(Query);
            }
//EXECUTE METHOD
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        for(Lead l: scope){
            l.LeadSource='Referral';
            l.State = 'NJ';

        }
        update scope;
    }
//FINISH METHOD
    global void finish(Database.BatchableContext bc){

    }
}

如何更改此批处理顶点以返回多个查询、添加循环并更新它们。

【问题讨论】:

    标签: salesforce apex-code salesforce-communities


    【解决方案1】:

    这不是 Batch Apex 的工作方式。您可以在 Batch Apex 作业中只迭代一个查询。

    因为您正在以三种不同的方式改变三个不同的对象,所以批处理链接和使用动态 SOQL 的选项在这里并不真正适用。您只需要构建三个不同的批处理类。

    您可以并行运行这些类,或者在其finish() 方法中将每个类链接到下一个。但你不能一次完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多