English 中文(简体)
无法达到标准 价格指数
原标题:Cannot get standardPricebookEntry s id

I m trying to write an Apex trigger and class, to update a list-price of a Product in a Pricebook, once a checkbox is clicked on the product object. Here is all of my code:

Class:

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:

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
                    }
                }
            }
        }
    }
}

我所困的是——我需要获得标准。 缩略语 价格指数

I didn t realize when I started writing the code - that I was trying to modify the PricebookEntry object, but that isn t correct because the list price is a standardPricebookEntry field, not a PricebookEntry field, so that s how I have this line of code:

Id pricebookEntryId = pricebookEntries[0].Id;

但是,如果我想要改变,它就属于标准类型。 价格 加入:

PricebookEntry standardPricebookEntry = standardPricebookEntries[0].Id;

我发现这一错误:“从Id到PbookEntry进行非法转让”。

Thats because its assigning an id value to a PricebookEntry variable. But I do need the Id value - to pass to the class method - it s expecting to get an Id type parameter. What can I add to the code, or how can I modify it, to do what I described in the beginning of the question?

问题回答

If you want to get the whole Record as a variable, remove the .Id. So replace:

http://www.ohchr.org。 PricebookEntry = StandardPricebookEntries[0]。 Id;

with:

PricebookEntry standardPricebookEntry = standardPricebookEntries[0];

However, this code may not work on Product2 trigger while before Insert, as I believe an adequate PricebookEntry record requires Product2 record as a parent to exist, and it does not exist yet (see Order of Execution). Unfortunately, PricebookEntry does not allow triggers - Salesforce Idea. You have to come with a different approach for that, maybe this Stack Exchange post will help.





相关问题
Using Aggregate Functions Inside a Trigger In MySQL

I have a People table with several attributes including age . Each time I insert a new tuple into this table, I would like to find out the average age of all the people listed in the table. If the ...

Mysql trigger/events vs Cronjob

I have an auction website which let my users place an unlimited amount of autobiddings. To monitor these autobiddings something has to check the database every second. My question is if it is ...

creation of trigger

I have this trigger procedure CASE_A as UPDATE table1 SET field1 = (SELECT bus FROM view1 WHERE table1.document = view1.document) end; the below trigger is supposed to execute the above ...

Inserting trigger (SQL 2005)

I have a temp table (question_desc, ans1, ans2, ans3, ans4, correct_ans, marks) with say 10 entries. From this table I have to insert values in two other tables: questions (q_id(auto-generated), ...

Before trigger in SQL Server

I have 2 tables: survey (id(PK), name) and survey_to_topic (survey_id(PK,FK,not null), topic_id(PK,FK,not null)). When I try to delete from survey table, I get exception: "The DELETE statement ...

Jquery mouseover/mouseout triggering inconsistently

I have two sortable lists, one being nested, with a mouse over effect on the li elements of the nested sortable list. My problem is that the mouseover and mouseout functions are being called ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签