function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
iaestemaniaesteman 

update field with a trigger in custom object from pricebookentry

Hello,

I am new with apex triggers and I need some assistance. I tried to create a trigger that will provide me with PricebookID and Price to be populated from other fields like Bundle_Child__c and Bundle_Child_Fabric_1__c and Bundle_Child_Fabric_2__c.
So when I insert a Bundle_Child__c(lookup field) with a product value and Price_Book__c (lookup field) with a pricebook i want the Bundle_Child_PB_UnitPRice to be populated with the UnitPrice of that product and the Bundle_Child_PB_ID to be populated with the PricebookentryID field. Also I want to do the same with the Bundle_Child_Fabric_1__c and Bundle_Child_Fabric_2__c fields(lookup fields) to update the values for the Bundle_Child_Fabric_1PB_ID__c and Bundle_Child_Fabric_1PB_ID__c . I am doing this so I can get the exact price from all those products.

This is my code so far. I can't get to work with one. Also I am trying to check if there is a pricebook entry inside if not create it without pricebook. This combination needs to go for all fields if its empty to create it without it.

 

trigger setPricebookEntryID on Bundle_Line_Item__c (before update) { 
    Set<Id> setPriceBookId = new Set<Id>();
    Map<Id,PricebookEntry> mapPriceBookIdToPBE = new Map<Id,PricebookEntry>();
    for (Bundle_Line_Item__c p : Trigger.new) { 
        if (p.Price_Book__c != null) {
            setPriceBookId.add(p.Price_Book__c);    
        }
    }
    if (!setPriceBookId.isEmpty()) {
        for (PricebookEntry pbe : [SELECT Id, UnitPrice, Pricebook2Id 
                    FROM PricebookEntry
                    WHERE Pricebook2Id IN: setPriceBookId]) {
             mapPriceBookIdToPBE.put(pbe.Pricebook2Id, pbe);
        } 
        for (Bundle_Line_Item__c p : Trigger.new) {
            if (mapPriceBookIdToPBE.containskey(p.Price_Book__c) && mapPriceBookIdToPBE.get(p.Price_Book__c).Product2Id == p.Bundle_Child__c) {
                p.Bundle_Child_PB_ID__c= mapPriceBookIdToPBE.get(p.Price_Book__c).Id;
                p.Bundle_Child_PB_UnitPRice__c = mapPriceBookIdToPBE.get(p.Price_Book__c).UnitPrice;
            }
        }             
    }  
}

This is the error I am getting so far.

 

Error:Apex trigger setPricebookEntryID caused an unexpected exception, contact your administrator: setPricebookEntryID: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: PricebookEntry.Product2Id: Trigger.setPricebookEntryID: line 29, column 1


Any advices?

Thanks,

Darko

Jolly_BirdiJolly_Birdi
Hello @Darko

Change your Query in the 10 line from "SELECT Id, UnitPrice, Pricebook2Id FROM PricebookEntry WHERE Pricebook2Id IN: setPriceBookId" to "SELECT Id, UnitPrice, Pricebook2Id,Product2Id FROM PricebookEntry WHERE Pricebook2Id IN: setPriceBookId"

Please mark this as best answer if you find it positive.

Thanks
Jolly Birdi