0

I'm writing a batch class that takes in a string, does the query and then passes the results to a method to process. I want to use the string in the query, but when I add it in I get the error:

Variable does not exist: versionFY

I set it above the string and in the load method but I'm not sure where I'm going wrong.

Example:

global class NPD_Batch_BudgetToolLoad implements Database.Batchable<sObject>
{
private String versionId;   
private String versionFY;
private Map<String, Id> pStructureMap;   
private Map<String, Id> practiceMap;

private static final String QUERY = 
    'SELECT Id,' +  
    .....           
    'FROM   Revenue_Pipeline__c ' +
    'WHERE  Year__c = ' + versionFY;

public NPD_Batch_BudgetToolLoad(Map<String, Id> pStructureMap, Map<String, Id> practiceMap, String versionId, String versionFY) 
{
    this.versionId = versionId;
    this.versionFY = versionFY;
    this.pStructureMap = pStructureMap;
    this.practiceMap = practiceMap;
}       

global Database.QueryLocator start(Database.BatchableContext BC) 
{
    return Database.getQueryLocator(QUERY);
}

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

1 Answer 1

2

If you want to merge in a variable, the proper approach would be:

String soql = '... WHERE Field__c = :mergeVariableName';
//                                  ^ you are missing the bind character
//                                    you also want the name of the variable, not its value

You could alternatively merge in the literal string instead of binding, but then you need to wrap it in escaped single quotes:

String soql = '... WHERE Field__c = \'' + String.escapeSingleQuotes(myVariable) + '\'';

Don't neglect escaping, as doing so would open up an injection vulnerability.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .