Pass a string variable from controller into the Javascript function

I have a method which returns a Yes or No String depending on it’s outcome.

The method is inside a standard controller extension. How do I pass the returned String into a function on the page in which the standard controller is called.

PAGE:

<apex:page extensions="CheckBtnExtension" standardController="myController" showHeader="false">

            <apex:form >
              <apex:actionFunction action="{! CheckIn }" name="checkIn" oncomplete="backToDetail(chkr)" />
            </apex:form>  
    <script>

        $(function() {
            $('#btn-check-in').click(function() {
                $.mobile.showPageLoadingMsg();
                checkIn();
            });         
        });       

        function backToDetail(chkr){
            alert(chkr)
        }  
     </script>

EXTENSION:

    public String CheckIn() {

    if (!recordList.isEmpty()) {        
        myObj record = recordList[0];
        isCheckedIn = record.Checked_In__c;

        if (isCheckedIn) {
            record.Checked_In__c = false;
            chkr = 'Yes'; 
        } else {
            record.Checked_In__c = true;
            chkr = 'No';
        }
        UPSERT record;
    } else {
        isCheckedIn = null;
    }  
   return(chkr);  
}

chkr is the String I want to pass into my JS. I just need to be able to stick it into an alert for now.

Any help would be appreciated.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can change up some of your visualforce and javascript slightly to achieve this.

First lets change up the JS a tiny bit so it directly references the checkIn apex method and is wrapped in an element you can rerender.

<apex:outputPanel id="scriptContainer">
    <script>
        //on dom ready code is fine
        function backToDetail() {
            alert('{!checkIn}');
        }            
    </script>
</apex:outputPanel>

Now just change up the actionFunction definition so it doesnt call checkIn and instead it rerenders the script tag containing the backToDetail method. The purpose of doing this is that when it is doing the rerender it will replace alert('{!checkIn}') with alert('Yes') or alert('No').

<apex:actionFunction name="checkIn" oncomplete="backToDetail();" rerender="scriptContainer" />

Method 2

On your VF page, use {!CheckIn} to access the value (even in JS).

Method 3

Try this. Define an extra string variable and create an PageReference method that will be called from the page:

Page:

<apex:actionFunction action="{!checkIn}" 
                     name="checkInFunction" 
                     oncomplete="backToDetail()" />
<script>
jQuery(function() {
    jQuery('#btn-check-in').click(function() {
        jQuery.mobile.showPageLoadingMsg();
        checkInFunction();
    });         
});       

function backToDetail(){
    alert('{!chkr}');
}  
</script>

Controller:

public String chkr { get; set; }

public PageReference checkIn() {

    if (!recordList.isEmpty()) {        
        myObj record = recordList[0];
        isCheckedIn = record.Checked_In__c;

        if (isCheckedIn) {
            record.Checked_In__c = false;
            chkr = 'Yes'; 
        } else {
            record.Checked_In__c = true;
            chkr = 'No';
        }
        UPSERT record;
    } 
    else {
        isCheckedIn = null;
    }  
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x