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
ApexDevApexDev 

Simple Apex class to fill the lookup field

Hi Everyone! :) 

On PricebookEntry object, I have a 2 lookup fields: 
- Pricebook2Id (standard field filled by Integration)
- Price_Book_2__c (custom field needed for Related list)
I want to fill the Price_Book_2__c using the value in Pricebook2Id.

I am trying to write some class, it will be my first apex class, and still I get errors...

I want to run it from Flow.

Here is code:
public class PriceBookField{
@InvocableVariable(label='Get Price Book records')
    public PriceBookEntry p;
    @InvocableMethod
    public static void populateField(List<PricebookEntry>records){
        records[0].Price_Book_2__c = records[0].Pricebook2Id;
    update records;
}
}


Can you help me?
ApexDevApexDev
I change the code for this one below, I invoke it using Flow and later Process Builder, but the field is still empty

public class PriceBookField
{
    @InvocableMethod
    public static void PriceBookField(List<Id> PBIds){
        List<PricebookEntry> XYZ = [Select Id, Pricebook2Id, Price_Book_2__c from PricebookEntry where ID in: PBIds];
        for(PricebookEntry PB : XYZ){
            PB.Price_Book_2__c = PB.Pricebook2Id;
        }
        update XYZ;
    } 
}