エラー:Only variable references are allowed in dynamic SOQL/SOSL
・エラーが起きるケース
List<String> accountIdList = new List<String>();
for(Account account : accountList){
accountIdList.add(account.Id);
}
String query = '';
query = '';
query += ' SELECT Id ';
query += ' , Name ';
query += ' , Account__c ';
query += ' , Account__r.Id ';
query += ' From Test__c ';
query += ' WHERE Account__r.Id IN : \'' + accountIdList + '\'';
system.debug('query:'+query);
List<Test__c> testLsit = Database.query(query);
・エラーが起きないケース
List<String> accountIdList = new List<String>();
for(Account account : accountList){
accountIdList.add(account.Id);
}
String query = '';
query = '';
query += ' SELECT Id ';
query += ' , Name ';
query += ' , Account__c ';
query += ' , Account__r.Id ';
query += ' From Test__c ';
query += ' WHERE Account__r.Id IN : accountIdList ';
system.debug('query:'+query);
List<Test__c> testLsit = Database.query(query);