在 Salesforce 中使用 APEX 触发器将产品价格复制到自定义字段对象中
Copy Product Price into a Custom Field Object using APEX Trigger in Salesforce
尝试使用 APEX 触发器在产品更新(如果可能也插入)时将产品的 Cost_Price__c 字段复制到自定义对象中。
我很接近,但我现在遇到的错误是:从 PricebookEntry 到 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],但似乎仍然不起作用。
非法分配网络,因为您正在将整个对象(即价格簿条目)分配给字符串类型的字段,即 Industry on Account。
请使用以下代码进行赋值。
oAccount.Industry = sqlResult[0].Cost_Price__c;
如果适合您,请标记答案。
谢谢,
图萨尔
尝试使用 APEX 触发器在产品更新(如果可能也插入)时将产品的 Cost_Price__c 字段复制到自定义对象中。
我很接近,但我现在遇到的错误是:从 PricebookEntry 到 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],但似乎仍然不起作用。
非法分配网络,因为您正在将整个对象(即价格簿条目)分配给字符串类型的字段,即 Industry on Account。
请使用以下代码进行赋值。
oAccount.Industry = sqlResult[0].Cost_Price__c;
如果适合您,请标记答案。
谢谢, 图萨尔