0

I am working on updating a batch class where I do a query for records and pass them to another class to handle the logic. I want to include a line where the record Id is not in a string set. (I am storing hardcoded ids in a custom setting that I split and add to a set which seems to be working) The problem is when I run the query in a test class it fails complaining about an unexpected token '{'. debug example with striped down fields:

23:57:44:273 USER_DEBUG [33]|DEBUG|Projected Query is:: SELECT Id, examplefield__c, examplefield__c FROM Revenue_Pipeline__c WHERE (examplefield__c = 'Booked' AND lookup__c NOT IN :{a1G000000000000000} AND FY_2018__c != 0)

How can I get the {} removed to correctly show the Id?

Example class:

global class NPD_Batch_BudgetToolLoad implements Database.Batchable<sObject>
{
String versionFY;   
String versionId; 
Date startDate;
Date endDate;
Set<String> practiceFilter;
Map<String, Id> pStructureMap;   

public NPD_Batch_BudgetToolLoad(String versionId, String versionFY, Set<String> practiceFilter, Map<String, Id> pStructureMap, Date startDate, Date endDate) 
{
    this.versionFY = versionFY;
    this.versionId = versionId;
    this.startDate = startDate;
    this.endDate = endDate;
    this.practiceFilter = practiceFilter;
    this.pStructureMap = pStructureMap;
}       

global Database.QueryLocator start(Database.BatchableContext BC) 
{
    String QUERY = getStringQuery(versionFY, practiceFilter);
    return Database.getQueryLocator(QUERY);
}

global void execute(Database.BatchableContext BC, List<sObject> scope)  
{
    NPD_BudgetingDataLoad.createProjectedRevenue(scope, versionId, pStructureMap);
}

global void finish(Database.BatchableContext BC) 
{

}

public String getStringQuery(String versionFY, Set<String> practiceFilter)
{
    String fyField = 'FY_' + versionFY + '__c != 0 ';
    String query =
    'SELECT Id,' +
            'CurrencyISOCode,' +
            'Account__c ' + 
    'FROM   Revenue_Pipeline__c ' +
    'WHERE  (NPD_Forecast_Category__c = \'Booked\' AND ' +
            'Product_Practice__c NOT IN :' + practiceFilter + ' AND ' +
            fyField)';
    return query;
}
}
2

You're mixing Apex bind variable syntax with string concatenation.

You need to change this line:

        'Product_Practice__c NOT IN :' + practiceFilter + ' AND ' +

to

        'Product_Practice__c NOT IN :practiceFilter AND ' +

or use something like NOT IN (\'' + String.join(practiceFilter, '\', \'') + '\') AND ' to construct the list of Ids as a string for inclusion. Note that you can use simple binding in dynamic SOQL.

You've got some shadowing going on with parameters named the same thing as your instance variable practiceFilter, which you don't need to pass as a parameter to getStringQuery() because it's an instance variable. This may become problematic and confusing later on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.