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
Adam PietraszekAdam Pietraszek 

Trouble with setting two field values in Apex Trigger

Having an issue with two lines of my code:
pbe.Reference__c = 'donkey'; -- I'm just trying to write a value to Reference which is a plain text box.

pbe.Franchise__c = prd.Franchise__c; -- I'm trying to set a picklist inside 'pbe' based on a picklist of the same name from 'prd'

Not sure why these aren't working. There are other fields that are referenced in my code and they seem to work but these two do not work. No compile errors and no runtime errors but it's like those lines are being skipped. Any ideas what I could be missing?

trigger ProductCreateEntries on Product2 (after insert) {

List<Pricebook2> pricebooks = [SELECT id,Name,Franchise__c FROM Pricebook2 WHERE IsStandard = false];
Pricebook2 standardPricebook = [SELECT id,Name,Franchise__c FROM Pricebook2 WHERE IsStandard = true LIMIT 1];
List<PricebookEntry> newEntries = new List<PricebookEntry>();
List<PricebookEntry> standardEntries = new List<PricebookEntry>();
    for (Product2 prd : Trigger.new) {
        if (prd.Franchise__c == 'ENT') {
        //create standard pricebook entries
        PricebookEntry stdPbe = new PricebookEntry();
        stdpbe.Product2Id = prd.Id;
        stdpbe.Pricebook2Id = standardPricebook.Id;
        stdpbe.IsActive = true;
        stdpbe.UseStandardPrice = false;               
        if(prd.Standard_Price__c != null) {
            stdpbe.UnitPrice = prd.Standard_Price__c;
        }
        else {
            stdpbe.UnitPrice = 0;
        }     
        standardEntries.add(stdpbe);
        //create entries for all non-standard pricebooks   
        for(Pricebook2 pb : pricebooks) {
            if (prd.IsActive == true) {
                PricebookEntry pbe = new PricebookEntry();
                pbe.Product2Id = prd.Id;
                pbe.Pricebook2Id = pb.Id;
                pbe.IsActive = true;
                pbe.UseStandardPrice = true;
                pbe.Reference__c = 'donkey';
                pbe.Franchise__c = prd.Franchise__c;
                if(prd.Standard_Price__c != null) {
                pbe.UnitPrice = prd.Standard_Price__c;
                }
                else {
                    pbe.UnitPrice = 0;
                }
                newEntries.add(pbe);
            }
        }   
    }
    upsert standardEntries;    
    upsert newEntries;
    }
}