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
NickHockingNickHocking 

Dynamically display formula field value based on Picklist choice

Hi,

 

I'm trying to create a VF page that dynamically displays the currency result of a formula field, based on the picklist choice.

 

So there is a picklist in User object called Job_Grade__c. It has values A, B, C, D, E. THere is then a formula field in User object called Job_Grade_Amount__c. This bsaically does a simple if job grade is A then = 50, B=100, else 200.

 

So in my VF page i literally want to be able to use the picklist to pick a Job Grade, and it automatically display what the value of the Job Grade is... dynamically changing as the picklist option is changed.

 

I've got this code:

 

<apex:page standardController="User" id="page">
<apex:form id="form">
<apex:pageBlock id="block">
<apex:pageBlockSection id="blockSection" columns="2">
<apex:inputField value="{!User.Job_Grade__c}">
<apex:actionSupport event="onchange" reRender="blockSection" />
</apex:inputField>
<apex:outputText value="{!User.Job_Grade_Amount__c}" rendered="{!User.Job_Grade__c!= NULL}" id="output" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

Thanks,

 

N

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

For some scenario, Formula fields are not displayed directly using the field name when using Standard Controller of the object in VF page. So, that it is recommended assigning a variable value for the field in extensions of that page.

 

Try the following, 

VF page:

<apex:page standardController="User" extensions="UserController" id="page" action="{!showFormula}">
<apex:form id="form">
<apex:pageBlock id="block">
<apex:pageBlockSection id="blockSection" columns="2">
<apex:inputField value="{!User.Job_Grade__c}">
<apex:actionSupport event="onchange" reRender="blockSection" />
</apex:inputField>
<apex:outputText value="{!jobAMT}" rendered="{!User.Job_Grade__c!= NULL}" id="output" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

Controller:

public class UserController{

    public Decimal jobAMT{get;set;}

    public User u{get;set;}

 

    public UserController(Apexpages.StandardController con){

        u = (User)con.getRecord();   //Used to bind the record details of the standard object refered     

    }

    

    public void showFormula(){

         User utemp = [select id, name, job_grade__c, job_grade_Amount__c from User where id=:u.id limit 1];

         jobAMT = utemp.job_grade_Amount__c;

    }

}

 

 

The above code has highlighted lines to explain the variable is refered in VF page to display formula value.

 

Hope this will help you...!

 

Please give kudos and mark this as a solution, If you found this answer as useful to others as well.

 

NickHockingNickHocking

Thanks Kamatchi, however i get the following:

 

Visualforce Error
 


System.QueryException: List has no rows for assignment to SObject

Error is in expression '{!showFormula}' in component <apex:page> in page quotecalculator

 

 

Class.UserController.showFormula: line 10, column 1

 

 

NickHockingNickHocking

I think i see the problem - does your class assume that I'm accessing the data for a particular record?

 

I'm not - I am trying to create a kind of calculator - so use of it would be that for each row, the user can select a job grade from the drop down, and it would simply, automatically display the job grade amount for the job grade. I then want to go on to be able to multiply that job grade amount for a number of input hours to come up with a quote... x hours @ job grade amount £x = total £xxx for the job.... Does that make sense? It's not particularly referencing one user/record, it's generic for a job grade...

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

The error is because it u.id is reffered in the query that is binded instance id through Standard controller, this will work if you are having a inline visualforce page in your user object.

 

public void showFormula(){

         User utemp = [select id, name, job_grade__c, job_grade_Amount__c from User where id=:u.id limit 1]; // here is what no id was there to get the values in the query.

         jobAMT = utemp.job_grade_Amount__c;

    }

 

Try again keeping this in a inline VF page.

 

Please give kudos and mark this as a solution, If you found this answer as useful to you..!.

 

NickHockingNickHocking
Hi,

Thanks again for you reply, however, I don't want to use this inline on the User page. I want it to either just be a tool on a new tab, or inline in the Opportunity page.

It is purely a calculator, not to be specifcally related to any one id. So, as a sales user, i want to be able to just enter any Job Grade from the drop down, and it just shows me what Job Grade Amount it is for the grade. Then if i change the picklist value, i want the amount to change corresponding to the new value in the picklist....that simple. Is there a way to do this.

If it helps, I've actually created a new field, Job_Grade_Value__c, which is a picklist, which is dependant on the Job_Grade__c picklist. So not using formula anymore, but using dependant picklist instead....
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan
Hi,

Of course, then you can definitely go for a dependent Pick list.
I thought you were using User object.

NickHockingNickHocking
Ahh ok cool. The problem is now though....i can get it to show the dependant picklist, however, I want it to display as text - there is only one value.... for example:
Job Grade = A, Job Grade value = 50
Job Grade = B, Job Grade value = 100
Job Grade = C, Job Grade value = 150
Job Grade = D, Job Grade value = 200

The dependant ppicklists are therefore not necessary to be picklists....is there a way to just display the single value in the picklist as text?

Thanks,

N
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

If you want your dependency with Text alone and not a picklist, then you need to go for VF page and controller.

 

Because, dependency can be made between Picklist,Multipicklist and checkboxes only for the following scenarios.

 

Controlling field - Checkbox / Picklist

Dependent field - picklist / Multi-Picklist