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
Ben MorchBen Morch 

Getting formula field values for a new object

Hello All,

I am creating a couple of lists so that I can display values of a custom object onto the Account layout through a VF page.  It is basically a rollup or sum of data that is associated with that account.  One of the fields that I want to show is a formula field.  I am doing this sum per month of data and comparing Current Year vs Prior Year.

My issue comes in with the Current Year data when the month has no data yet.  For example, we are currently in the month of April which means that May has no data.  In the Apex Class I am adding the missing months data to the list that is being displayed.  When I look at the VF page though, the data associate with the formula field is blank.  Obviously I can't force a value into that field since it is ready only.  Is there another way to force the formula field to have a value before the VF page grabs it?

VF Page results

Partial code from Apex Class
integer missingMonths = 12-maxMonth;
        for (integer i=1; i<=missingMonths; i++){
            zeroMonth = new Order_Summary__c();
            If (i+maxMonth > 9){
                zeroMonth.Account__c = AccountId;
                zeroMonth.Delivery_Month__c = string.valueOf(i+maxMonth);
                zeroMonth.Delivery_Year__c = String.valueOf(thisYear);
                zeroMonth.Revenue__c = 0;
                zeroMonth.Paid__c = 0;
                zeroMonth.Load_Count__c = 0;
            } else {
                zeroMonth.Account__c = AccountId;
                zeroMonth.Delivery_Month__c = string.valueOf('0' + (i+maxMonth));
                zeroMonth.Delivery_Year__c = String.ValueOf(thisYear);
                zeroMonth.Revenue__c = 0;
                zeroMonth.Paid__c = 0;
                zeroMonth.Load_Count__c = 0;
            }
            currentYear.add(zeroMonth);

Partial VF Page code
<table border="1" >
    <tr>
    <td colspan="4" align="center"><h1> 
        Current Year</h1></td> 
    </tr>
    
    <tr bgcolor="#0f5e9d">
    <td align="center"><headerTxt>Month</headerTxt></td>
    <td align="center"><headerTxt>Rev $</headerTxt></td>    
    <td align="center"><headerTxt>GM $</headerTxt></td>
    <td align="center"><headerTxt>Loads</headerTxt></td>
    </tr>
      <apex:repeat value="{!currentYear}" var="cy"> 
   
          <tr onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#FFFFFF';">        
              
        <td width="75"><monthTxt>{!cy['Month_Name__c']}</monthTxt></td>
        <td width="75" align="center"><apex:outputText value="{0,number,$#,###}"> <apex:param value="{!cy['Revenue__c']}"/></apex:outputText></td>
        <td width="75" align="center"><apex:outputText value="{0,number,$#,###}"> <apex:param value="{!cy['GM__c']}"/></apex:outputText></td>
        <td width="75" align="center"><apex:outputText value="{0,number,###}"> <apex:param value="{!cy['Load_Count__c']}"/></apex:outputText></td>

        

           </tr>
     
         </apex:repeat>        
        <apex:repeat value="{!currentYearTotal}" var="cyt"> 
         <tr bgcolor="#0f5e9d">
             <td align="center"><headerTxt>Total</headerTxt></td>
             <td align="center"><headerTxt><apex:outputText value="{0,number,$#,###}"> <apex:param value="{!cyt['Revenue']}"/></apex:outputText></headerTxt></td>
             <td  align="center"><headerTxt><apex:outputText value="{0,number,$#,###}"> <apex:param value="{!cyt['GM']}"/></apex:outputText></headerTxt></td>
             <td  align="center"><headerTxt><apex:outputText value="{0,number,###}"> <apex:param value="{!cyt['LoadCount']}"/></apex:outputText></headerTxt></td>
         </tr>
         </apex:repeat> 
   
    </table>

 
Best Answer chosen by Ben Morch
bob_buzzardbob_buzzard
There's no way to write data into a formula field, as these are generated by the database when a record is retrieved.  The best you can do is to put the sobject into a wrapper class and store the value that you want as a property of the class.  Thus you would iterate instances of the wrapper class and output the property instead of the field.