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结果也能够正常表示