Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
社区首页 >专栏 >Salesforce Flow如何调用Batch(一)

Salesforce Flow如何调用Batch(一)

原创
作者头像
repick
发布于 2022-09-27 12:57:29
发布于 2022-09-27 12:57:29
48200
代码可运行
举报
文章被收录于专栏:SalesforceSalesforce
运行总次数:0
代码可运行

1.BatchApex做成

a.Flow调用ApexClass例

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

UpdateOpportunityProject.cls

代码语言:javascript
代码运行次数:0
复制
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
代码运行次数:0
复制
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
代码运行次数:0
复制
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
代码运行次数:0
复制
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 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Salesforce Batch Apex 批处理(五)AsyncApexJob情报
如下,在batch所有方法里边都有【Database.BatchableContext】Object,前边讲的的例子中,都没有用到BC,因为它是用来取得【AsyncApexJob】情报跟踪Job进程的,然后可以及时发送邮件共享信息还可以做成后台log并出力等等。
repick
2021/11/23
1.2K0
Salesforce Batch Apex 批处理(五)AsyncApexJob情报
Salesforce Batch Apex 批处理(二)Schedulable接口
定时任务调用需要单独写一个ApexClass,实现Schedulable接口,并重写execute方法,举例如下
repick
2021/11/22
9550
Salesforce Batch Apex 批处理(二)Schedulable接口
Salesforce Batch Apex 批处理(三)Database.Stateful接口
Batch处理中,根据特定需求,会有在处理中计算数量的业务,例如现在需要计算所有满足条件的Opportunity表total__c的总额。
repick
2021/11/22
8670
Salesforce Batch Apex 批处理(三)Database.Stateful接口
Salesforce Batch Apex 批处理(四)Send Mail Example
在execute方法执行完成后,调用finish方法,一般用来发送邮件和后续处理,发送邮件具体写法如下
repick
2021/11/22
7000
Salesforce Batch Apex 批处理(四)Send Mail Example
Salesforce Batch Apex 批处理(一)
Apex SOQL 每次最多只能查询50000条数据,DML 可以操作的数据更少只有10000条,如果想要处理大量数据,就要考虑使用Batch Apex功能,Batch Apex实现Database.batchable接口
repick
2021/11/20
1.7K0
Salesforce Batch Apex 批处理(一)
salesforce的功能_salesforce开发
161、【String.format escape curly braces – 转义花括号】:
全栈程序员站长
2022/11/01
7K0
salesforce的功能_salesforce开发
Salesforce JSON应用(一) 反序列化deserializeUntyped方法,序列化serialize方法
【deserializeUntyped】方法用于将JSON内容反序列化成基本数据类型的集合,不能反序列化sObject类型。
repick
2022/09/23
5160
Salesforce JSON应用(一) 反序列化deserializeUntyped方法,序列化serialize方法
Salesforce JSON应用(二) 反序列化deserializeUntyped方法,序列化serialize方法
在Apex中使用deserializeUntyped方法反序列化处理,查看具体内容。
repick
2022/09/24
3891
Salesforce JSON应用(二) 反序列化deserializeUntyped方法,序列化serialize方法
Salesforce Future method in salesforce – @future
future方法用于在系统资源可用时在单独的线程中运行进程,我们可以将future方法用于任何我们希望在其自己的线程中异步运行的操作。
repick
2021/11/23
8330
Salesforce Future method in salesforce – @future
salesforce 零基础学习(三十七) DML及Database方法简单描述
Zero-Zhang
2018/01/05
6240
salesforce 零基础开发入门学习(五)异步进程介绍与数据批处理Batchable
本篇知识参考:https://developer.salesforce.com/trailhead/force_com_dev_intermediate/asynchronous_apex/async
Zero-Zhang
2018/01/05
2.1K1
Salesforce学习 What is Trigger in Salesforce?
用于访问系统设置的字段值,并影响记录中的任何更改。换句话说,在这里一般用于更改其他Object的值。
repick
2020/12/12
8160
Salesforce学习 Lwc(九)【数据初期取得与更新】运用详解
开发自定义画面经常遇到的场景就是增删改查,关于数据更新用到的几个方法进行一下总结,常用到的有以下几种。
repick
2020/12/29
1.1K0
Salesforce学习 Schema相关知识
Schema 名前空間は、スキーマメタデータ情報を操作するためのクラスとメソッドを提供します。Schema.*
repick
2020/01/16
7080
salesforce零基础学习(七十五)浅谈SOSL(Salesforce Object Search Language)
在工作中,我们更多操作的是一个表的对象,所以我们对SOQL的使用很多。但是有时候,我们需要对几个表进行查询操作,类似salesforce的全局搜索功能,这时,使用SOQL没法满足功能了,我们就需要使用
Zero-Zhang
2018/01/05
1.1K0
salesforce零基础学习(七十五)浅谈SOSL(Salesforce Object Search Language)
Salesforce Apex Rest Callout 更新JSON形式的数据
repick
2023/11/19
1790
Salesforce Apex Rest Callout 更新JSON形式的数据
Salesforce 关于Apex中【SaveResult】
在ApexClass中使用【Database.insert(xxxList)】登录或者使用【Database.update(xxxList)】更新多条数据时,如果其中一条有错误,所有登录或者更新都不会成功
repick
2022/05/23
6820
Salesforce 关于Apex中【SaveResult】
Salesforce sendEmail 常见应用(二)
看上边log,虽然显示发送成功,但是并没有收到邮件,看来在异常处理里边无法发送出去,只能另外想办法。
repick
2022/09/11
3600
Salesforce sendEmail 常见应用(二)
salesforce 零基础开发入门学习(十一)sObject及Schema深入
Zero-Zhang
2018/01/05
8810
salesforce 零基础开发入门学习(十一)sObject及Schema深入
Salesforce学习 CommunityCloud(八)实现自定义error页面跳转
上一篇讲了自定义LogoutPage跳转,在我们正常开发过程中还会遇到需要跳转到自定义的error画面,例如当我们在Lwc中更新某个项目,但是在当前User下,没有更新权限,这样就需要跳转到自定义的Error画面,实现方法是首先做成一个VisualforcePage,用来显示error信息或者固定文言,然后在Community的Error Page装载VisualforcePage,最后在更新处理的方法实现调整,下边是具体步骤。
repick
2021/01/25
4280
Salesforce学习 CommunityCloud(八)实现自定义error页面跳转
推荐阅读
相关推荐
Salesforce Batch Apex 批处理(五)AsyncApexJob情报
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验