+ Start a Discussion
Sangeetha TSangeetha T 

*CRITICAL* -- using variable in where condition "LIKE"

Hi All 

I have a batch class as follows 
 
Integer FiscalYear = getCurrentFY();
    Integer FYPlusOne = FiscalYear - 1;

    global database.querylocator start(Database.BatchableContext BC){   
        Integer lmt = Limits.getLimitQueryRows();
        String query = 'Select Org_SFID__r.Fiscal_Year_SFID__c,fAccount_Brand_ID__c,Id,CFY_Dealer_Account_SFID__r.Brand_Group_Name__c,Integration_Key__c,';     
        query += 'Org_SFID__c,Org_SFID__r.Fiscal_Year_SFID__r.Year__c,Sales_Focus_Descr__c';
        query += ' FROM Account_Org_Relationship__c';
        query += ' WHERE Sales_Focus_Descr__c =\'Assigned\' and is_Active__c = True and fFiscal_Year__c = False and fAccount_Brand_Name__c =: National';

I need to add a condition to the query like Inte__key__c like 'FYPlusOne%' 

Intekey field value will be in the format of 2017|39238shdshdhkjsoskok. I have to fetch the records of the previous fiscal year so the inteley should contain 2017(Currentfiscalyear - 1) 

Please help me how to achieve this in where condition. I have a declared a variable on top 

Thanks 
Sangeetha T
Best Answer chosen by Sangeetha T
Alisha Mehta 9Alisha Mehta 9
Hi Sangeetha,

Do the below: 
Integer FiscalYear = getCurrentFY();
    Integer FYPlusOne = FiscalYear - 1;
    string yearCheck = FYPlusOne + '%' ;
    global database.querylocator start(Database.BatchableContext BC){   
        Integer lmt = Limits.getLimitQueryRows();
        String query = 'Select Org_SFID__r.Fiscal_Year_SFID__c,fAccount_Brand_ID__c,Id,CFY_Dealer_Account_SFID__r.Brand_Group_Name__c,Integration_Key__c,';     
        query += 'Org_SFID__c,Org_SFID__r.Fiscal_Year_SFID__r.Year__c,Sales_Focus_Descr__c';
        query += ' FROM Account_Org_Relationship__c';
        query += ' WHERE Sales_Focus_Descr__c =\'Assigned\' and is_Active__c = True and fFiscal_Year__c = False and fAccount_Brand_Name__c =: National and Inte__key__c like yearCheck ';

All Answers

Alisha Mehta 9Alisha Mehta 9
Hi Sangeetha,

Do the below: 
Integer FiscalYear = getCurrentFY();
    Integer FYPlusOne = FiscalYear - 1;
    string yearCheck = FYPlusOne + '%' ;
    global database.querylocator start(Database.BatchableContext BC){   
        Integer lmt = Limits.getLimitQueryRows();
        String query = 'Select Org_SFID__r.Fiscal_Year_SFID__c,fAccount_Brand_ID__c,Id,CFY_Dealer_Account_SFID__r.Brand_Group_Name__c,Integration_Key__c,';     
        query += 'Org_SFID__c,Org_SFID__r.Fiscal_Year_SFID__r.Year__c,Sales_Focus_Descr__c';
        query += ' FROM Account_Org_Relationship__c';
        query += ' WHERE Sales_Focus_Descr__c =\'Assigned\' and is_Active__c = True and fFiscal_Year__c = False and fAccount_Brand_Name__c =: National and Inte__key__c like yearCheck ';
This was selected as the best answer
Sangeetha TSangeetha T
Hi Alisha 

Thanks so much it worked 
Sangeetha TSangeetha T
Hi Alisha

Can you please have a look at these --->>> https://developer.salesforce.com/forums/ForumsMain?id=9060G0000005MaRQAU