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
Corey Edwards 11Corey Edwards 11 

Variable not being passed to Visualforce page

Hi Guys,

I'm trying to convert my logic from jQuery to an Apex controller. Based on the changing values of my Visualforce page I'd want to return the correct value for my variable. Here is my code:
 
public with sharing class getPriceExtension {
    //Set Standard Controller
    ApexPages.standardController m_sc = null;
    
    //Set Quote Instance
    public Quote quo{get;set;} 
    
    //Set Global Variables
    String preHos = 'Premium Hospital',
            amb = 'Ambulance Only',
            firSta = 'First Start Hospital',
            fitHel = 'Fit & Healthy Hospital',
            pubHos = 'Public Hospital',
            smaHos = 'Smart Hospital',
            steUp = 'Step Up Hospital',
            valHos = 'Value Hospital',
            preExt = 'Premium Extras',
            smaExt = 'Smart Extras',
            valExt = 'Value Extras',
            fitExt = 'Fit & Healthy Extras',
            noHos = 'No Hospital',
            vicCor = 'Ambulance Victoria Corporate',
            vicRet = 'Ambulance Victoria Retail';
            Public Integer finalValue{get;set;}
    
    //Constructor
    public getPriceExtension(ApexPages.standardController sc)
    {
        m_sc = sc;
        quo = (Quote)sc.getRecord();
    }
    
    public Integer getHospitalId() {
        //Declare Locals
        String hospitalValue = quo.Hospital_Cover__c;
        String excess = quo.Excess__c;
        String levy = quo.Levy_Exempt__c;
        Integer f;
        
        //Logic
        //Premium Hospital
        if(hospitalValue == preHos) {
            if(excess == '$500'){
                if(levy == 'Yes') {
                     f = 30;
                 } else{
                     f = 29;
                 }
            }
            if(excess == '$250') {
                if(levy == 'Yes') {
                    f = 9;
                } else {
                    f = 5;    
                }
            }
            if(excess == '$0') {
                if(levy == 'Yes') {
                    f = 8;
                } else {
                    f = 4;
                }
            }
        }
        //Smart Hospital
        if(hospitalValue == smaHos) {
            if(excess == '$500'){
                if(levy == 'Yes') {
                    f = 22;
                } else {
                    f = 21;
                }
            } 
            if(excess == '$250') {
                if(levy == 'Yes'){
                    f = 28;
                } else {
                    f = 27;
                }
            }
        }
        //Step Up Hospital
        if(hospitalValue == steUp) {
            if(excess == '$500'){
                if(levy == 'Yes') {
                    f = 41;
                } else {
                    f = 40;
                }
            }
            if(excess == '$250'){
                if(levy == 'Yes') {
                    f = 39;
                } else {
                    f = 38;
                }
            }
        }
        //First Start Hospital
        if(hospitalValue == firSta) {
            if(excess == '$500'){
                if(levy == 'Yes') {
                    f = 45;
                } else {
                    f = 44;
                }
            }
            if(excess == '$250'){
                if(levy == 'Yes') {
                    f = 43;
                } else {
                    f = 42;
                }
            }
        }
        //Value Hospital
        if(hospitalValue == valHos) {
            if(excess == '$500'){
                if(levy == 'Yes') {
                    f = 26;
                } else {
                    f = 25;
                }
            }
            if(excess == '$250'){
                if(levy == 'Yes') {
                    f = 24;
                } else {
                    f = 23;
                }
            }
        }
        //Public Hospital
        if(hospitalValue == pubHos) {
            f = 3;
        }
        //No Hospital
        if(hospitalValue == noHos) {
            f = 0;
        } 
        //Ambulance
        if(hospitalValue == amb) {
            f = 10;
        }
        //Fit & healthy
        if(hospitalValue == fitHel) {
            f = 6;
        }
        //Victoria Retail
        if(hospitalValue == vicRet) {
            f = 15;
        }
        //Victoria Corporate
        if(hospitalValue == vicCor) {
            f = 14;
        }
        finalValue = f;
        return finalValue;
    }
}

When I call this value in Visualforce using an actionFunction it doesn't return a value. I've tested assigning values through the controller and this method works. I understand that the state of my Quote object has something to do with this but am struggling to find a solution.

Cheers
Nayana KNayana K
Please post VF code
Corey Edwards 11Corey Edwards 11
At this point I'm doing something like this:
 
<apex:actionFunction name="call" action="{!getHospitalId}" oncomplete="testFunction('{!finalValue}')" />

and calling this test function but it's not working:
 
function testFunction(test){
                console.log("Test: " + test);
                }

Any help would be greatly appreciated :)
Nayana KNayana K
public with sharing class getPriceExtension {
    //Set Standard Controller
    ApexPages.standardController m_sc = null;
    
    //Set Quote Instance
    public Quote quo{get;set;} 
    
    //Set Global Variables
    String preHos = 'Premium Hospital',
            amb = 'Ambulance Only',
            firSta = 'First Start Hospital',
            fitHel = 'Fit & Healthy Hospital',
            pubHos = 'Public Hospital',
            smaHos = 'Smart Hospital',
            steUp = 'Step Up Hospital',
            valHos = 'Value Hospital',
            preExt = 'Premium Extras',
            smaExt = 'Smart Extras',
            valExt = 'Value Extras',
            fitExt = 'Fit & Healthy Extras',
            noHos = 'No Hospital',
            vicCor = 'Ambulance Victoria Corporate',
            vicRet = 'Ambulance Victoria Retail';
            Public Integer finalValue{get;set;}
    
    //Constructor
    public getPriceExtension(ApexPages.standardController sc)
    {
        m_sc = sc;
        quo = (Quote)sc.getRecord();
    }
    
    public void fetchHospitalId() {
        //Declare Locals
        String hospitalValue = quo.Hospital_Cover__c;
        String excess = quo.Excess__c;
        String levy = quo.Levy_Exempt__c;
        Integer f;
        
        //Logic
        //Premium Hospital
        if(hospitalValue == preHos) {
            if(excess == '$500'){
                if(levy == 'Yes') {
                     f = 30;
                 } else{
                     f = 29;
                 }
            }
            if(excess == '$250') {
                if(levy == 'Yes') {
                    f = 9;
                } else {
                    f = 5;    
                }
            }
            if(excess == '$0') {
                if(levy == 'Yes') {
                    f = 8;
                } else {
                    f = 4;
                }
            }
        }
        //Smart Hospital
        if(hospitalValue == smaHos) {
            if(excess == '$500'){
                if(levy == 'Yes') {
                    f = 22;
                } else {
                    f = 21;
                }
            } 
            if(excess == '$250') {
                if(levy == 'Yes'){
                    f = 28;
                } else {
                    f = 27;
                }
            }
        }
        //Step Up Hospital
        if(hospitalValue == steUp) {
            if(excess == '$500'){
                if(levy == 'Yes') {
                    f = 41;
                } else {
                    f = 40;
                }
            }
            if(excess == '$250'){
                if(levy == 'Yes') {
                    f = 39;
                } else {
                    f = 38;
                }
            }
        }
        //First Start Hospital
        if(hospitalValue == firSta) {
            if(excess == '$500'){
                if(levy == 'Yes') {
                    f = 45;
                } else {
                    f = 44;
                }
            }
            if(excess == '$250'){
                if(levy == 'Yes') {
                    f = 43;
                } else {
                    f = 42;
                }
            }
        }
        //Value Hospital
        if(hospitalValue == valHos) {
            if(excess == '$500'){
                if(levy == 'Yes') {
                    f = 26;
                } else {
                    f = 25;
                }
            }
            if(excess == '$250'){
                if(levy == 'Yes') {
                    f = 24;
                } else {
                    f = 23;
                }
            }
        }
        //Public Hospital
        if(hospitalValue == pubHos) {
            f = 3;
        }
        //No Hospital
        if(hospitalValue == noHos) {
            f = 0;
        } 
        //Ambulance
        if(hospitalValue == amb) {
            f = 10;
        }
        //Fit & healthy
        if(hospitalValue == fitHel) {
            f = 6;
        }
        //Victoria Retail
        if(hospitalValue == vicRet) {
            f = 15;
        }
        //Victoria Corporate
        if(hospitalValue == vicCor) {
            f = 14;
        }
        finalValue = f;
        return finalValue;
    }
}
 
<apex:actionFunction name="call" action="{!fetchHospitalId}" oncomplete="testFunction('{!finalValue}')" rerender="dummy"/>

function testFunction(test){
                console.log("Test: " + test);
                }

PLease check this once
Corey Edwards 11Corey Edwards 11
When I call() I'm not getting anything in the console. Do you have any further suggestions or reasons why this isn't working?
 
Nayana KNayana K
<apex:actionFunction name="call" action="{!fetchHospitalId}" oncomplete="testFunction('{!finalValue}')" rerender="dummy" immediate="true"/>

function testFunction(test){
                console.log("Test: " + test);
                }

Please try this once.