【问题标题】:Apex, SOQL, add results from database to the List, errorApex,SOQL,将结果从数据库添加到列表,错误
【发布时间】:2021-03-25 15:25:41
【问题描述】:

我想将数据库中的结果添加到列表中。但是有一个错误。

String str = '\'AOC\',\'BPD\',\'CRE\'';
List<String> lstString = str.split(',');
List<Shop_Product__c> productList = new List<Shop_Product__c>();
Integer i = 0;
for (String record: lstString) {
    System.debug('record[' + i + ']: ' + record);
    if ((record != null) && (productList != null)) {
      productList.add([SELECT Id, Brand__c 
                       FROM Shop_Product__c 
                       WHERE Brand__c = :record
                       LIMIT 10]);
      
      System.debug('productList[' + i + ']: ' + productList);
      System.debug('Here in for ----------------------------------------');
    }
    ++i;
}

错误是 System.QueryException: List 没有分配给 SObject 的行Here是解释,但我不明白我该怎么做。

【问题讨论】:

    标签: salesforce apex soql


    【解决方案1】:

    List 方法 add() 采用单个 sObject。这就是您获得QueryException 的原因,就像您链接到的示例中一样。

    您可以使用addAll() 方法创建List&lt;sObject&gt; 上下文,这样可以避免没有响应记录时的异常。

    【讨论】:

    • 使用 addAll() 方法没有错误。谢谢。
    猜你喜欢
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多