Getting a value from controller extension to Visualforce page

  • I've been wondering about this alot. I have a VF page that has a StandardController for Contact and a custom extension that handles an external webservice call. This extension class contains a string value that the web service call sets and then I need to pass that value to the VF page for display. This value is not bound to any field in SF but is rather just a string value. I'm trying to use actionFunction to do this, since it seems easier than using JS Remoting (my JS callback function skill level is not high)

    VF Page:

     <apex:page showHeader="false" sidebar="false" standardController="Contact" extensions="CallStatus">
    
    
    <apex:form >
    <apex:actionFunction name="gomob"  action="{!GetCallStatus}" reRender="amount"/>
    <div id="black">
    
    
    <apex:outputpanel id="amount">
                <apex:outputText value="Get Status!: {!amount}"/>
                <apex:actionSupport event="onclick" 
                                    action="{! GetCallStatus}" 
                                    rerender="amount" status="whileStatus"/>
            </apex:outputpanel>
            <apex:actionStatus id="whileStatus" 
                               startText=" (fetching...)" 
                               stopText=" (done)"/>
    
    </div>
    </apex:form>
    </apex:page>
    

    My extension class:

    global class CallStatus {
    
        private final ApexPages.StandardController controller;
        public WS_GoMobileStatus(ApexPages.StandardController controller){
        this.controller = controller;
        }
    
        global String kId { get; set; }
        transient String amount {get; set;}
        Contact con;
    
        public CallStatus(){
    
    
        }
    
        Public String GetCallStatus(){
        String conId = ApexPages.currentPage().getParameters().get('Id');
        con = [SELECT Id,kid__c from Contact where Id =:conId LIMIT 1];
        ktId = con.kid__c;
    
            String endp = 'ENDPOINT_URL?action=GetCallStatus&kid=';
    
    
             HttpRequest req = new HttpRequest();
             req.setMethod('GET');
             req.setHeader('Content-Type','application/json; charset=utf-8');
             req.setHeader('User-Agent', 'force.com');
             req.setEndpoint(endp+=ktId);
    

             Http http = new Http();
             System.HTTPResponse res = new System.HTTPResponse();
    
              try {
    
            //Execute web service call here     
            res = http.send(req);       
    
    
    
            //Helpful debug messages
            System.debug(res.toString());
            System.debug('STATUS:'+res.getStatus());
            System.debug('STATUS_CODE:'+res.getStatusCode());
    
    } catch(System.CalloutException e) {
        //Exception handling goes here....
    }       
    
            amount = res.getBody();
            return amount;
    
    
        }
    }
    

    Is my use of the transient keyword wrong here? When I refer the {!amount} in the VF page, I get the error Error: Unknown property 'ContactStandardController.amount

    Is t not possible to have the extension class return the String value to the VF page?

    Contact standard controller does not have an attribute called an 'amount'. Create an getter method in the extension and use it as a variable in pageblock table or some other structure where you would need the value...Might be a wrapper class in the extension will be helpful

  • Declare your variable as -

     transient global String amount {get; set;}
    OR 
    transient public String amount {get; set;}
    

    By default, a method or variable is visible only to the Apex code within the defining class. If you do not specify an access modifier, the method or variable is private.

    Thanks for that, this worked. I decided to change the method a bit but this definitely fixed my problem.

    TIL you need to add the transient keyword as well.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM