+ Start a Discussion
anil Kumaranil Kumar 

Custom Label value not working in SOQL Query

Hi All,

I am trying to use custom label value in SOQL Query. Query is not accepting custom label value. it is expecting number. 

Integer num_days = Integer.valueOf(System.Label.Num_of_Days);
Select id, name FROM contact WHERE LastModifiedDate >= LAST_N_DAYS :num_days 

Thanks,
Anil Kumar
Best Answer chosen by anil Kumar
SwethaSwetha (Salesforce Developers) 
Hi Anil,
You'll need dynamic SOQL to make it work.

See similar post in the context of date field: https://salesforce.stackexchange.com/questions/195205/how-to-use-custom-label-inside-soql-query-for-date-value. You need to customize for number as per your requirement

Related: https://developer.salesforce.com/forums/?id=906F0000000BWNnIAO

If this information helps, please mark the answer as best. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
Hi Anil,
You'll need dynamic SOQL to make it work.

See similar post in the context of date field: https://salesforce.stackexchange.com/questions/195205/how-to-use-custom-label-inside-soql-query-for-date-value. You need to customize for number as per your requirement

Related: https://developer.salesforce.com/forums/?id=906F0000000BWNnIAO

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
anil Kumaranil Kumar
Hi Swetha,

Thank you for the responce,,,dynamic SOQL worked for me. 

        Integer num_days = Integer.valueOf(System.Label.Num_of_Days);
        string Query='Select id, name FROM contact WHERE LastModifiedDate >= LAST_N_DAYS :'+num_days;
        system.debug('Query...'+Query);

Thanks,
Anil Kumar