0

I need to display a formula field in my VFP. Survey_Summary_Form_Name__c is a formula field and I need to display it on VFP.

Here is the snippet of the code. I know that I cannot override the formula field. What method can I use to display it?

if(eventRec.Activity_Junction__c != null){
        subActMasterList = [SELECT Id,
                             Name,
                             Activity_Name__c,
                             Survey_Summary_Form__c,
                             Survey_Summary_Form__r.Activity_Name__c,
                             Survey_Summary_Form__r.Survey_Summary_Form_Name__c,
                             Survey_Summary_Form__r.Inline_VF_Sort__c
                      FROM Sub_Activity__c
                      WHERE Activity_Junction__c =: eventRec.Activity_Junction__c
                      ORDER BY Survey_Summary_Form__r.Inline_VF_Sort__c ASC];
        if(subActMasterList.size()==0){
            subActMasterList= null;
        }else{

            for(Sub_Activity__c sa : subActMasterList){
                if(sa.Survey_Summary_Form__c == null){
                    subActList.add(new Sub_Activity__c(id = sa.Id,Activity_Name__c = sa.Activity_Name__c));
                }else{
                    surveyList.add(new Survey_Summary_Form__c(Id=sa.Survey_Summary_Form__c, Survey_Summary_Form_Name__c = sa.Survey_Summary_Form_Name__c  ));
                } 
            }

            if(subActList.size() == 0){
                subActList = null;
            }

            if(surveyList.size() == 0){
                surveyList = null;
            }
        }
    }   

This is the snippet of VFP

 <apex:pageBlockTable value="{!surveyList}" var="s" rendered="{!surveyList != null}">
              <apex:column headerValue="Survey">
                  <apex:outputLink value="/{!s.Id}" target="_parent">{!s.Survey_Summary_Form_Name__c}</apex:outputLink>
              </apex:column>
          </apex:pageBlockTable>   
1

In your code you're assigning custom field value directly to formula field.

Survey_Summary_Form_Name__c = sa.Survey_Summary_Form_Name__c 

Please note that you are not supposed to populate the formula field value by assigning some other fields directly, it will be populated automatically.

So if you want to populate formula field value,you need to check that on what basis(other fields that your are using in your formula) your formula is calculated and based on it you need to populate those fields only then your formula field is populated.

Check this link for more details:

https://developer.salesforce.com/docs/atlas.en-us.fundamentals.meta/fundamentals/adg_simple_app_adv_fields_formula.htm

Edit Based on the formula field provided:

You need to assign populate some Value to SSF_Name_Backend__c OR Name field only then Survey_Summary_Form_Name__c will populate.Your formula field depends on the value that you select either in SSF_Name_Backend__c or Name field.

For this you need to modify your code a little bit.

In your Query add the field 'SSF_Name_Backend__c' only if the field is present on Survey_Summary_Form__c object.

subActMasterList = [SELECT Id,
                             Name,
                             Activity_Name__c,
                             Survey_Summary_Form__c,
                             Survey_Summary_Form__r.Activity_Name__c,
                             Survey_Summary_Form__r.SSF_Name_Backend__c,                                Survey_Summary_Form__r.Survey_Summary_Form_Name__c,
                             Survey_Summary_Form__r.Inline_VF_Sort__c
                      FROM Sub_Activity__c
                      WHERE Activity_Junction__c =: eventRec.Activity_Junction__c
                      ORDER BY Survey_Summary_Form__r.Inline_VF_Sort__c ASC];

then in your else condition,you need to populate the value of SSF_Name_Backend__c or Name field.

if you want to set some default value then you can set like this:

else{
surveyList.add(new Survey_Summary_Form__c(Id=sa.Survey_Summary_Form__c, SSF_Name_Backend__c  = 'Some Value' ));
}

if your are providing the value from UI i.e.Sub_Activity__c object then you need to assign then you can assign like this.

else{
surveyList.add(new Survey_Summary_Form__c(Id=sa.Survey_Summary_Form__c, SSF_Name_Backend__c  = sa.SSF_Name_Backend__c,Name=sa.Name ));
}

when you select the value from UI then your else part will assign the value to SSF_Name_Backend__c or Name field and based on this your formula field will calculate automatically.

Let me know if it helps.

6
  • exactly, I need to display the value of the formula field in my VFP, do I still need to add it on my new Survey_Summary_Form__c since I already assigned the Id or just call it on my vfp? – sfdclearner Nov 9 '17 at 8:05
  • You can display formula field in your VF page but you need to assign the value in those field that you are using in your formula.Can you please share the formula that you've written.So that I can tell you what needs to change.. – Dhananjay Patil Nov 9 '17 at 8:09
  • this is the formula if( isblank(SSF_Name_Backend__c ), Name ,SSF_Name_Backend__c ) – sfdclearner Nov 9 '17 at 8:10
  • Please check the updated answer.. – Dhananjay Patil Nov 9 '17 at 9:06
  • The fields used in formula field is not populated in the VFP. Can I achieve this using wrapper? Can I just add the formula field in the list? – sfdclearner Nov 9 '17 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.