1

In apex I have a method that return an wrapper object like this :

public class WrapperCalcul{
    public String nb;
    public String coutTot;
}

@RemoteAction
public static WrapperCalcul submitForm(String nb, String montant){
   WrapperCalcul wrapCalc = new WrapperCalcul();
    wrapCalc.nb = nb;
    wrapCalc.coutTot = montant;

    return wrapCalc;
}

I want to display the values of wrapCalc into a simple VF (one value in one field), how can I do that ?

  • is it important to use RemoteAction? – Ratan Paul Jun 8 '18 at 7:53
  • yes I call this method from JS (this is a one page form, that use JS to work). Is it annoying ? – dou Jun 8 '18 at 7:55
1

Can do something like,

<script type="text/javascript">
    function sampleJSRemoteFunction() {
        var nb; //get nb value
        var coutTot; //get couTot value

        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.<Your-ControllerName>.submitForm}',
            nb, coutTot, 
            function(result, event){
                if (event.status) {
                    //handle the result. You can use js to populate these values in DOM.
                    console.log(result.nb);
                    console..log(result.coutTot);
                } else if (event.type === 'exception') {
                    console.error(event.message+"-"+event.where);
                } else {
                    console.error(event.message);
                }
            }, 
            {escape: true}
        );
    }
    </script>
1

An Apex wrapper will be returned in the result object of the callback function stated within your Visualforce Remoting call. Refer the following link to understand what, the event and result object return - Handling Visualforce Remoting Response.

The sample code to access the Apex Wrapper class values and display them in on the Visualforce page is as below.

Visualforce Code

<apex:page id="pgDisplay" controller="DisplayRemotingDataSample" standardStylesheets="false">

    <script type="text/javascript">

       function getRemoteValues() {

            Visualforce.remoting.Manager.invokeAction(
                '{!$RemoteAction.DisplayRemotingDataSample.submitForm}',
                'Sample nb', //nb, 
                'Sample montant', //montant, 
                function(result, event){

                    if(event.status){
                        console.log('Success: ' + result.nb);
                        document.getElementById('divNb').innerHTML = result.nb;
                        document.getElementById('divMontant').innerHTML = result.coutTot;
                    }
                    else if(event.type === 'exception'){
                        console.log('Callback failed ' + event.message);
                    }

                }
            );
        }

    </script>

    <!-- Clientside Controls with values returned through Visualforce Remoting -->
    <div id="divOutputClient">
        <div id="divNb"/>
        <div id="divMontant"/><br/>
    </div>

    <input id="btnGetValue" onclick="getRemoteValues()" type="button" value="Submit" />

</apex:page>

The above code passes 2 hard-coded string values to the Visualforce Remoting method as input params which are returned as a part of the Apex Wrapper. The result object returned within the callback is then used to display the values passed from the server within 2 div elements.

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.