在 salesforce 的 Apex 中连接变量?

Concatenate variable in Apex in salesforce?

在 salesforce 批处理中 class 我正在尝试执行如下操作,但出现错误

global class PurgerefundcasesBatch implements Database.Batchable<sObject> {

    private static final String refund = 'refund';

    String query = String query = 'SELECT id, SubStatus__c FROM Case ' + 
                        'WHERE SubStatus__c ='+ refund +' AND createdDate < LAST_N_DAYS:10';

    global PurgerefundcasesBatch() {

    }
    .......
    .........
}

出现以下错误..

Timestamp: Tue, 13 Feb 2018 21:59:17
   Result: [OPERATION FAILED]: classes/PurgeDraftcasesBatch.cls: Missing ';' at 'query' (Line: 12, Column: 24)
classes/PurgeDraftcasesBatch.cls: Unexpected token '='. (Line: 12, Column: 30)
classes/PurgeDraftcasesBatch.cls: Unexpected token '='. (Line: 12, Column: 30)
classes/PurgeDraftcasesBatch.cls: Unexpected token '+'. (Line: 13, Column: 41)
classes/PurgeDraftcasesBatch.cls: Unexpected token '+'. (Line: 13, Column: 41)
classes/PurgeDraftcasesBatch.cls: Variable does not exist: String (Line: 12, Column: 17)
classes/PurgeDraftcasesBatch.cls: Invalid loop variable type expected SObject was Case (Line: 26, Column: 4)`

主要问题是您以错误的方式设置了查询变量。你不能这样定义它:

String query = String query =

String query = 'SELECT id, ELC_SubStatus__c FROM Case WHERE SubStatus__c ='+ refund +' AND createdDate < LAST_N_DAYS:10';

我能够像下面那样做到这一点。动态查询注意事项请参考Dynamic SOQL

private static final String refund = 'refund';

String query = 'SELECT id, SubStatus__c FROM Case ' + 
                'WHERE SubStatus__c = :refund AND createdDate < LAST_N_DAYS:10';