无法获取standardPricebookEntry的id

问题描述 投票:0回答:1

我正在尝试编写一个 Apex 触发器和类,以便在单击产品对象上的复选框后更新价格手册中产品的标价。 这是我的全部代码:

班级:

public class UpdatePriceBook {
    public static void UpdatePrice(Id productId, Id pricebookEntryId, Decimal newPrice){
         try {
            // Retrieve the product and pricebook entry
            Product2 product = [SELECT Id, Name FROM Product2 WHERE Id = :productId];
            PricebookEntry pricebookEntry = [SELECT Id, Product2Id, Pricebook2Id, UnitPrice FROM 
            PricebookEntry WHERE Id = :pricebookEntryId];

            // Update the price
            pricebookEntry.UnitPrice = newPrice;

            // Save the updated pricebook entry
            update pricebookEntry;

            System.debug('Price updated successfully for product: ' + product.Name);
        } catch (Exception e) {
            System.debug('Error updating price: ' + e.getMessage());
        }
    }
}

触发:

trigger UpdateProductPrice on Product2 (before insert) {
    if (Trigger.isBefore) {
        if (Trigger.isInsert || Trigger.isUpdate) {
            for (Product2 product : Trigger.new) {
                if (product.ProductPriceUpdate__c) {
                    // Get the relevant PricebookEntry for this product
                    List<PricebookEntry> standardPricebookEntries = [
                    SELECT Id, UnitPrice
                    FROM PricebookEntry
                    WHERE Product2Id = :product.Id
                    AND Pricebook2.Name IN ('USA Pricebook', 'International PB')
                    AND Product2.Name = 'Cabinet'
                    AND Pricebook2.IsStandard = true
                    LIMIT 1
                     ];

//                    if (!pricebookEntries.isEmpty()) {
//                        Id pricebookEntryId = pricebookEntries[0].Id;
//                        Decimal newPrice = 1500; // Replace with your desired price
                    if (!standardPricebookEntries.isEmpty()) {
                        PricebookEntry standardPricebookEntry = standardPricebookEntries[0].id;
                        Id pricebookEntryId = pricebookEntries[0].Id;
                        Decimal newPrice = 1500; // Replace with your desired price
                        
                        standardPricebookEntry.UnitPrice = newPrice;
                        UpdatePriceBook.UpdatePrice(product.Id, pricebookEntryId, newPrice); // Replace with your desired price
                    }
                }
            }
        }
    }
}

我所坚持的是 - 我需要获取 standardPricebookEntry 的 Id 以将其传递给类中的方法,但似乎没有选项可以从

PricebookEntry standardPricebookEntry
对象获取 id。

当我开始编写代码时,我没有意识到我正在尝试修改 PricebookEntry 对象, 但这是不正确的,因为标价是 standardPricebookEntry 字段,而不是 PricebookEntry 字段,所以这就是我的这行代码:

Id pricebookEntryId = pricebookEntries[0].Id;

但是如果我想将其更改为 standardPricebookEntry 类型,如下所示:

PricebookEntry standardPricebookEntry = standardPricebookEntries[0].Id;

我收到此错误:“从 Id 到 PricebookEntry 的分配非法”。

那是因为它为 PricebookEntry 变量分配了一个 id 值。 但我确实需要 Id 值 - 传递给类方法 - 它期望获得 Id 类型参数。 我可以在代码中添加什么,或者如何修改它,以执行我在问题开头所描述的操作?

triggers apex salesforce-lightning
1个回答
0
投票

如果您想将整个 Record 作为变量获取,请删除

.Id
。所以替换:

PricebookEntry standardPricebookEntry = standardPricebookEntries[0].Id;

与:

PricebookEntry standardPricebookEntry = standardPricebookEntries[0];

但是,此代码可能无法在

Product2
时触发
before Insert
触发器,因为我相信足够的
PricebookEntry
记录需要
Product2
记录作为父级存在,而它还不存在(请参阅执行顺序)。 不幸的是,
PricebookEntry
不允许触发器 - Salesforce Idea。 您必须为此采取不同的方法,也许这篇 Stack Exchange 帖子会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.