[英]Copy Product Price into a Custom Field Object using APEX Trigger in Salesforce
Trying to just copy the Cost_Price__c field of a product into a custom object when it is updated (if possible inserted too) using an APEX trigger. 尝试使用APEX触发器将产品的Cost_Price__c字段复制到自定义对象(如果可能也插入)时,将其复制到自定义对象。
I'm so close but the error I am getting at the moment is: Illegal assignment from PricebookEntry to String 我是如此亲密,但是我现在遇到的错误是: 从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;
}
}
Am I right in thinking it's because its returning a collective group of results from the SOQL call? 我是否认为这是因为它从SOQL调用中返回了一组结果? I've tried using the sqlResult[0] which still doesn't seem to work.
我尝试使用sqlResult [0]似乎仍然无法正常工作。
The Illegal Assignmnet because you are assigning a whole Object ie PriceBook Entry to a string type of field ie Industry on Account. 非法分配网是因为您正在将整个对象(即PriceBook条目)分配给字符串类型的字段,即“帐户上的行业”。
Please use the following code for assignment. 请使用以下代码进行分配。
oAccount.Industry = sqlResult[0].Cost_Price__c; oAccount.Industry = sqlResult [0] .Cost_Price__c;
Please mark the answer if this works for you. 如果适合您,请标记答案。
Thanks, Tushar 谢谢,杜沙尔
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.