我是 Salesforce Apex 开发的新手,正在尝试根据从外部 API (Guidestar) 响应返回的值更新帐户对象上的某些字段(例如组织名称、“EIN”值、问题区域和目标人群),由 Salesforce 工作流程调用。但是,我没有让它工作,我想知道我的逻辑在结构上有问题吗?
public without sharing class trac_GuidestarOrgUpdateBatch implements Database.Batchable<sObject>, Database.AllowsCallouts{
private Set<String> eins;
@InvocableMethod(Label='Update Bulk Orgs' Description='Returns the list of orgs returned from guidestar.' Category='Account')
public static void getBulkOrganisations(List<String> searchKeywords){
System.debug(searchKeywords);
trac_GuidestarOrgUpdateBatch btch = new trac_GuidestarOrgUpdateBatch(searchKeywords);
Database.executeBatch(btch , 100);
}
public trac_GuidestarOrgUpdateBatch(List<String> eins){
this.eins = new Set<String>();
if(eins != null && !eins.isEmpty()){
this.eins = new Set<String>(eins);
}
}
public Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id, EIN__c, Name, Primary_Issue_Area__c, Secondary_Issue_Area__c, Target_Population__c';
query += 'FROM Account';
query += 'WHERE EIN__c IN: eins';
System.debug('Query from start method: ' + query);
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC, List<Account> scope) {
Map<String, String> regionsMap = getAccountRegionMap();
//Get 'Grantee' record type id
Id GRANTEE_RECORDTYPE = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Grantee').getRecordTypeId();
//Initialize account list to update
List<Account> accountList = new List<Account>();
//For each of these EINs, make a call to the premiere api
for (Account acct : scope) {
System.debug('Current object in scope: ' + acct);
System.debug('Acct Id: ' + acct.Id);
String ein = acct.EIN__c;
if (String.isNotBlank(ein)) {
trac_GuideStarUtility.CandidWrapper premiereResponse = new trac_GuideStarUtility.CandidWrapper();
if (!Test.isRunningTest()) {
premiereResponse = trac_GuideStarUtility.makePremierApiCall(ein);
} else {
premiereResponse = TestDataFactory.fakePremiereResponse(ein);
}
// If the result is not empty, use it to create a new account record.
if (premiereResponse?.code == 200 && premiereResponse?.data?.summary != null) {
trac_GuideStarUtility.OrgDataWrapper responseObjData = premiereResponse.data.summary;
acct.Name = responseObjData.organization_name;
acct.EIN__c = responseObjData.ein;
acct.Primary_Issue_Area__c = responseObjData.primary_issue_area;
acct.Secondary_Issue_Area__c = responseObjData.secondary_issue_area;
acct.Target_Population__c = responseObjData.target_population;
//Append current account to account list
accountList.add(acct);
}
}
}
//Push account list updates to database
Database.update(accountList);
}
public void finish(Database.BatchableContext BC){
}
private Map<String, String> getAccountRegionMap() {
Map<String, String> stateToRegionsMap = new Map<String, String>{
'WA'=>'US: West',
'OR'=>'US: West',
'CA'=>'US: West',
'AK'=>'US: West',
'HI'=>'US: West',
'ID'=>'US: Central',
'MT'=>'US: Central',
'NV'=>'US: Central',
'WY'=>'US: Central',
'UT'=>'US: Central',
'CO'=>'US: Central',
'AZ'=>'US: South',
'NM'=>'US: South',
'TX'=>'US: South',
'OK'=>'US: South',
'ND'=>'US: Central',
'SD'=>'US: Central',
'NE'=>'US: Central',
'KS'=>'US: Central',
'MN'=>'US: Central',
'IA'=>'US: Central',
'MO'=>'US: Central',
'WI'=>'US: Central',
'IL'=>'US: Central',
'MI'=>'US: Central',
'IN'=>'US: Central',
'OH'=>'US: Central',
'AR'=>'US: South',
'LA'=>'US: South',
'MS'=>'US: South',
'KY'=>'US: South',
'TN'=>'US: South',
'AL'=>'US: South',
'GA'=>'US: South',
'WV'=>'US: South',
'VA'=>'US: South',
'NC'=>'US: South',
'SC'=>'US: South',
'FL'=>'US: South',
'NY'=>'US: East',
'NH'=>'US: East',
'PA'=>'US: East',
'MD'=>'US: East',
'DE'=>'US: East',
'NJ'=>'US: East',
'VT'=>'US: East',
'ME'=>'US: East',
'MA'=>'US: East',
'RI'=>'US: East',
'CT'=>'US: East',
'DC'=>'US: East'
};
return stateToRegionsMap;
}
运行时,开发者控制台/调试日志/设置 -> Apex 作业中是否出现任何错误?
将 Database.Stateful 添加到类签名中,看看是否有帮助。 (不应该需要,但我想知道你是否传递了
eins
好的。start()中的查询返回的内容是否正常?执行运行吗?)
最重要的是 - 你正在吞下所有因懦弱的行为而可能引发的错误
Database.update(accountList);
。勇敢地去做 update accountList;
,你应该会看到一些真正的错误抛出。然后决定你想如何继续(1次失败是否足以破坏整个事情?或者保存你能保存的东西?或者将错误处理转移到适当的“平台事件”技巧并构建报告或其他东西来跟踪问题?