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
John GuthmillerJohn Guthmiller 

Print a string in a banner based on picklist value selected

I want my banner to simply say something that references the picklist option selected (I know I can do this with just visualforce, but would like to add more than the picklist option (for example: "The promotion for this opportunity is: [promo]"), but I keep on getting the  "Content cannot be displayed: SObject row was retrieved via SOQL without querying the requested field: Opportunity.Promotion_Code__c" error. Any help is appreciated. Here's my visualforce and controller code:

VF Code:
 <apex:page standardController="Opportunity" extensions="PartnerPromotionBanner">
  
 <marquee id="banner" rendered="{!Msgid}" style="box-shadow: 0px 0px 15px black; border-radius: 5px; padding: 10px; background-color: #FF0000; font-weight: bolder; font-size: 16px; margin: 10px; color: #FFFFFF font-family:'Lucida'">{!Promo}</marquee>

</apex:page>

Controller Code:
public class PartnerPromotionBanner {
    private final Opportunity opp;
    
     public String showMsg { get; set; }
     public boolean  Msgid { get; set; }
    
     public PartnerPromotionBanner(ApexPages.StandardController stdController) {
     this.opp = (Opportunity)stdController.getRecord();
    }

    
    public string getPromo() {
        if (opp.Promotion_Code__c == 'FinServ Bundle Promo')
        {
            return 'The promotion for this opportunity is: ' + opp.Promotion_Code__c;
        }
        return opp.Promotion_Code__c;
    }
 }
    
 
Steven NsubugaSteven Nsubuga
According to the official documentation (https://help.salesforce.com/articleView?amp;language=en_US&id=000170999&type=1), this behavior is to be expected. 

"The error "SObject-row-was-retrieved-via-SOQL-without-querying-the-requested-field" can occur when accessing a Visualforce page with a standard controller and a custom controller extension. 

When a standard controller queries an object for a Visualforce page, it only retrieves the fields from the object that are referenced in the page. 
This improves efficiency by not retrieving fields that are not needed by the Visualforce page. 

When a custom controller references a field that is not included in the page, the field data is not available, and the error occurs. "


Make a change to your page like this
<apex:page standardController="Opportunity" extensions="PartnerPromotionBanner">
  
 <marquee id="banner" rendered="{!Msgid}" style="box-shadow: 0px 0px 15px black; border-radius: 5px; padding: 10px; background-color: #FF0000; font-weight: bolder; font-size: 16px; margin: 10px; color: #FFFFFF font-family:'Lucida'">{!Promo}</marquee>
    <apex:outputText  label="invisible" value="{!Opportunity.Promotion_Code__c}" rendered="false"/>
</apex:page>