【问题标题】:Copy Product Price into a Custom Field Object using APEX Trigger in Salesforce使用 Salesforce 中的 APEX 触发器将产品价格复制到自定义字段对象中
【发布时间】:2019-02-19 17:20:04
【问题描述】:

尝试在使用 APEX 触发器更新产品(如果可能也插入)时将产品的 Cost_Price__c 字段复制到自定义对象中。

我已经很接近了,但我现在遇到的错误是:Illegal assignment from PricebookEntry to String

trigger updateAccount on Account (after update) {
  for (Account oAccount : trigger.new) {
    //create variable to store product ID
    string productId = oAccount.Product__c;

    //SQL statement to lookup price of product using productID
    PricebookEntry sqlResult = [SELECT Cost_Price__c 
        FROM PricebookEntry 
        WHERE Product2Id =: productId];

    //save the returned SQL result inside the field of Industry - Illegal assignment from PricebookEntry to String
    oAccount.Industry = sqlResult;
  }     
}

我是否认为这是因为它从 SOQL 调用返回了一组集体结果?我试过使用 sqlResult[0] 似乎仍然不起作用。

【问题讨论】:

    标签: triggers salesforce apex soql


    【解决方案1】:

    非法分配,因为您将整个对象(即 PriceBook 条目)分配给字符串类型的字段,即 Account 行业。

    请使用以下代码进行赋值。

    oAccount.Industry = sqlResult[0].Cost_Price__c;

    如果这对你有用,请标记答案。

    谢谢, 图沙尔

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      相关资源
      最近更新 更多