a.Flow调用ApexClass例
ApexClass中做成用于传递参数的内部类【UpdateOpportunityRequest】,引数必须指定成【InvocableVariable】
UpdateOpportunityProject.cls
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类的写法
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
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);
}
}
}
当Record更新时启动
BatchApexClass做成之后,调用Apex
Api名和Label名输入之后保存
当更新Opportunity表中的CloseDate时,启动flow
Apex中Flow调用的updateOpportunityProject()方法中的DebugLog能够正常输出,说明参数RecordId能够正常传入Apex中
【Database.executeBatch()】调用Batch时,start方法中的全局变量并没有正常输出,说明使用static声明的全局变量的值并没有传递到Batch的start方法中。
创建final变量,并在构造方法中重新赋值
UpdateOpportunityProjectBatch.cls
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 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有