[Salesforce]Only variable references are allowed in dynamic SOQL/SOSL

エラー: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);

投稿者: kinkun

保有資格 Salesforce Certified Platform App Builder T Salesforce Certified Platform Developer I Salesforce Certified Platform Developer II Salesforce Certified Administrator

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です