1

I am not getting a value in SOQL, while in system debug I am getting only this output :

cid:ali
record:()

Code:

 MVImportBatch__c record = [select Id from MVImportBatch__c limit 1];
 string cid =  string.valueOf(String.valueOf(record.Id).substring(0,3));
 System.debug('cid:'+cid);
 List<Task> record1 = new List<Task>();
 record1 = [SELECT Subject FROM Task WHERE Subject LIKE '%+cid+%'];
 System.debug('record:'+record1);

2 Answers 2

6

Format your input variable with like operator and then use it in SOQL. Refer below code:

MVImportBatch__c record = [select Id from MVImportBatch__c limit 1];
string cid =  string.valueOf(String.valueOf(record.Id).substring(0,3));
System.debug('cid:'+cid);


List<Task> record1 = new List<Task>();
cid = '%' + cid + '%';
record1 = [SELECT Subject FROM Task WHERE Subject LIKE :cid];
System.debug('record:'+record1);
0

You can also use dynamic SOQL: MVImportBatch__c record = [select Id from MVImportBatch__c limit 1]; string cid = string.valueOf(String.valueOf(record.Id).substring(0,3)); System.debug('cid:'+cid);

List record1 = new List(); record1 = Database.query('SELECT subject FROM task WHERE subject like \'%cid%\''); System.debug('record:'+record1);

1
  • They're not trying to match the literal string 'cid'.
    – Adrian Larson
    Jun 22, 2017 at 12:38

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.