前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Salesforce Flow如何调用Batch(一)

Salesforce Flow如何调用Batch(一)

原创
作者头像
repick
发布2022-09-27 20:57:29
4240
发布2022-09-27 20:57:29
举报
文章被收录于专栏:Salesforce

1.BatchApex做成

a.Flow调用ApexClass例

ApexClass中做成用于传递参数的内部类【UpdateOpportunityRequest】,引数必须指定成【InvocableVariable】

UpdateOpportunityProject.cls

代码语言:javascript
复制
public with sharing class UpdateOpportunityProject {
    private static String oppId;
    private static List<String> nameList = new List<String>();
    private static final String SOBJECT_TYPE_PREFIX_OPPORTUNITY
                        = Schema.SObjectType.Opportunity.getKeyPrefix();
    public class UpdateOpportunityRequest {
        @InvocableVariable(label='Id')
        public String recordId;
    }
    @InvocableMethod(label='UpdateOpportunityProjectBatch.案件更新バッチ' description='案件更新' )
    public static void updateOpportunityProject(List<UpdateOpportunityRequest> request) {
        if (request != null || request.size() > 0) {
            for (UpdateOpportunityRequest csReq : request){
                system.debug('>>>>>csReq.recordId>>>' + csReq.recordId);
                if (csReq.recordId != null) {
                    if ((csReq.recordId).startsWith(SOBJECT_TYPE_PREFIX_OPPORTUNITY)) {
                        oppId = csReq.recordId;
                        nameList.add('TestJson01');
                        nameList.add('TestJson04');
                    }
                    system.debug('>debuglog>>flow>>oppId>>>' + oppId);
                    system.debug('>debuglog>>flow>>nameList>>>' + nameList);
                    Database.executeBatch(new UpdateOpportunityProjectBatch(), 200);
                }
            }
        }
    }
}

b.batch类的写法

代码语言:javascript
复制
public with sharing class UpdateOpportunityProjectBatch implements Database.batchable<SObject>, Database.Stateful{

    public Database.QueryLocator start(Database.BatchableContext BC) {
        
        String queryS = '';
        return Database.getQueryLocator(queryS);
    }
    public void execute(Database.BatchableContext BC, list<sObject> scope) {
        Savepoint sp = Database.setSavepoint();
        try {
            
        } catch (Exception e) {

        }
    }
    public void finish(Database.BatchableContext BC) {
        try {

        } catch (Exception e) {
            System.debug(e);
        }
    }
}

c.把上边两个ApexClass结合

UpdateOpportunityProjectBatch.cls

代码语言:javascript
复制
public with sharing class UpdateOpportunityProjectBatch implements Database.batchable<SObject>, Database.Stateful{

    private static String oppId;

    private static List<String> nameList = new List<String>();

    private static final String SOBJECT_TYPE_PREFIX_OPPORTUNITY
                        = Schema.SObjectType.Opportunity.getKeyPrefix();

    public class UpdateOpportunityRequest {
        @InvocableVariable(label='Id')
        public String recordId;
    }
    @InvocableMethod(label='UpdateOpportunityProjectBatch.案件更新バッチ' description='案件更新' )
    public static void updateOpportunityProject(List<UpdateOpportunityRequest> request) {
        if (request != null || request.size() > 0) {
            for (UpdateOpportunityRequest csReq : request){
                system.debug('>>>>>csReq.recordId>>>' + csReq.recordId);
                if (csReq.recordId != null) {
                    if ((csReq.recordId).startsWith(SOBJECT_TYPE_PREFIX_OPPORTUNITY)) {
                        oppId = csReq.recordId;
                        nameList.add('TestJson01');
                        nameList.add('TestJson04');
                    }
                    system.debug('>debuglog>>flow>>oppId>>>' + oppId);
                    system.debug('>debuglog>>flow>>nameList>>>' + nameList);
                    Database.executeBatch(new UpdateOpportunityProjectBatch(), 200);
                }
            }
        }
    }

    public Database.QueryLocator start(Database.BatchableContext BC) {
        system.debug('>debuglog>>start>>oppId>>>' + oppId);
        system.debug('>debuglog>>start>>nameList>>>' + nameList);
        String queryS = '';
        if (oppId != null && oppId != '') {
            queryS = 'SELECT Id, Name';
            queryS += ' FROM Project__c ';
            queryS += ' WHERE Opportunity__c = ' + '\'' + oppId + '\'';
            if (nameList != null && nameList.size() > 0) {
                String queryC = '';
                Integer countCun = 0;
                for (String ids : nameList) {
                    countCun++;
                    if (countCun > 1) {
                        queryC += ',';
                    }
                    queryC += '\'' + ids + '\'';
                }
                queryS += ' AND Name IN (' + queryC + ')';
            }
            system.debug('>>>>>queryS>>>' + queryS);
        }

        return Database.getQueryLocator(queryS);
    }
    public void execute(Database.BatchableContext BC, list<sObject> scope) {
        Savepoint sp = Database.setSavepoint();
        try {
            system.debug('>>>>>scope>>>' + scope);
            for(Project__c projectItem : (List<Project__c>)scope) {
                system.debug('>>>>>>>>'+projectItem);
            }
        } catch (Exception e) {

        }
    }
    public void finish(Database.BatchableContext BC) {
        try {

        } catch (Exception e) {
            System.debug(e);
        }
    }
}

2.TriggerFlow做成

当Record更新时启动

BatchApexClass做成之后,调用Apex

Api名和Label名输入之后保存

3.测试

当更新Opportunity表中的CloseDate时,启动flow

Apex中Flow调用的updateOpportunityProject()方法中的DebugLog能够正常输出,说明参数RecordId能够正常传入Apex中

【Database.executeBatch()】调用Batch时,start方法中的全局变量并没有正常输出,说明使用static声明的全局变量的值并没有传递到Batch的start方法中。

4.解决方法

创建final变量,并在构造方法中重新赋值

UpdateOpportunityProjectBatch.cls

代码语言:javascript
复制
public with sharing class UpdateOpportunityProjectBatch implements Database.batchable<SObject>, Database.Stateful{

    private static String oppId;
    private final String oppIdBatch;

    private static List<String> nameList = new List<String>();
    private final List<String> nameListBatch = new List<String>();

    private static final String SOBJECT_TYPE_PREFIX_OPPORTUNITY
                        = Schema.SObjectType.Opportunity.getKeyPrefix();

    public class UpdateOpportunityRequest {
        @InvocableVariable(label='Id')
        public String recordId;
    }
    @InvocableMethod(label='UpdateOpportunityProjectBatch.案件更新バッチ' description='案件更新' )
    public static void updateOpportunityProject(List<UpdateOpportunityRequest> request) {
        if (request != null || request.size() > 0) {
            for (UpdateOpportunityRequest csReq : request){
                system.debug('>>>>>csReq.recordId>>>' + csReq.recordId);
                if (csReq.recordId != null) {
                    if ((csReq.recordId).startsWith(SOBJECT_TYPE_PREFIX_OPPORTUNITY)) {
                        oppId = csReq.recordId;
                        nameList.add('TestJson01');
                        nameList.add('TestJson04');
                    }
                    system.debug('>debuglog>>flow>>oppId>>>' + oppId);
                    system.debug('>debuglog>>flow>>nameList>>>' + nameList);
                    Database.executeBatch(new UpdateOpportunityProjectBatch(), 200);
                }
            }
        }
    }

    public UpdateOpportunityProjectBatch() {
        system.debug('>debuglog>>Constructor>>oppId>>>' + oppId);
        system.debug('>debuglog>>Constructor>>nameList>>>' + nameList);

        oppIdBatch = oppId;
        nameListBatch.addAll(nameList);
        system.debug('>debuglog>>Constructor>>oppIdBatch>>>' + oppIdBatch);
        system.debug('>debuglog>>Constructor>>nameListBatch>>>' + nameListBatch);
    }
    public Database.QueryLocator start(Database.BatchableContext BC) {
        system.debug('>debuglog>>start>>oppId>>>' + oppId);
        system.debug('>debuglog>>start>>nameList>>>' + nameList);

        system.debug('>debuglog>>start>>oppIdBatch>>>' + oppIdBatch);
        system.debug('>debuglog>>start>>nameListBatch>>>' + nameListBatch);

        String queryS = '';
        if (oppIdBatch != null && oppIdBatch != '') {
            queryS = 'SELECT Id, Name';
            queryS += ' FROM Project__c ';
            queryS += ' WHERE Opportunity__c = ' + '\'' + oppIdBatch + '\'';
            if (nameListBatch != null && nameListBatch.size() > 0) {
                String queryC = '';
                Integer countCun = 0;
                for (String ids : nameListBatch) {
                    countCun++;
                    if (countCun > 1) {
                        queryC += ',';
                    }
                    queryC += '\'' + ids + '\'';
                }
                queryS += ' AND Name IN (' + queryC + ')';
            }
            system.debug('>>>>>queryS>>>' + queryS);
        }

        return Database.getQueryLocator(queryS);
    }
    public void execute(Database.BatchableContext BC, list<sObject> scope) {
        Savepoint sp = Database.setSavepoint();
        try {
            system.debug('>>>>>scope>>>' + scope);
            for(Project__c projectItem : (List<Project__c>)scope) {
                system.debug('>>>>>>>>'+projectItem);
            }
        } catch (Exception e) {

        }
    }
    public void finish(Database.BatchableContext BC) {
        try {

        } catch (Exception e) {
            System.debug(e);
        }
    }
}

构造方法中能够正常取得,并重新赋值

Batch中的start方法中新的变量也能够正常表示

Batch中的execute方法中的query结果也能够正常表示

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.BatchApex做成
  • 2.TriggerFlow做成
  • 3.测试
  • 4.解决方法
相关产品与服务
批量计算
批量计算(BatchCompute,Batch)是为有大数据计算业务的企业、科研单位等提供高性价比且易用的计算服务。批量计算 Batch 可以根据用户提供的批处理规模,智能地管理作业和调动其所需的最佳资源。有了 Batch 的帮助,您可以将精力集中在如何分析和处理数据结果上。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档