【问题标题】:Trying to convert Apex Query List to string not sure if this is correct尝试将 Apex 查询列表转换为字符串不确定这是否正确
【发布时间】:2018-06-21 16:17:33
【问题描述】:

我正在尝试执行可能返回列表的 SOQL 查询。如果列表为空,我想设置默认值。我是 apex 的新手,所以不确定这是否正确。

这是我所拥有的:

// SET USERNAME STRING
string username = '%'+UserInfo.getFirstName()+'%'; 
//SET DEFAULT PRODUCTid
Id ProductID = '01t46000000nPO0AAM'; 
//MAP  IDMAPPRODUCT2
Map<Id> idmapproduct2 = new Map<Id>([Select Id FROM Product2 WHERE ProductCode like '%LAB %' AND ProductCode like:username]); system.debug(idmapproduct2); 
//CHECK IF MAP IS NULL AND IF NOT SET IT TO PRODUCT ID
if(idmapproduct2 != null)
  {ProductID = idmapproduct2;}

【问题讨论】:

    标签: triggers salesforce apex soql


    【解决方案1】:

    在这种情况下,您没有使用 Map 并且您对 Map 的使用不正确。请改用列表。

    List<Product2> idmapproduct2 = [Select Id FROM Product2 WHERE ProductCode like '%LAB %' AND ProductCode like:username LIMIT 1]; system.debug(idmapproduct2); 
    //CHECK IF MAP IS NULL AND IF NOT SET IT TO PRODUCT ID
    if(idmapproduct2.size() != 0){
       ProductID = idmapproduct2[0].Id;
    }
    

    【讨论】:

      【解决方案2】:

      使用下面的行......

      List<Product2> prodList = new List<Product2>([Select Id FROM Product2 WHERE ProductCode like '%LAB %' AND ProductCode like:username Limit 1]);
      if(prodList.size() > 0)  ProductID = prodList[0].Id;
      

      【讨论】:

        猜你喜欢
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 2020-11-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-03
        • 1970-01-01
        相关资源
        最近更新 更多