9

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.

7

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" />
  • I keep receiving the same error "Error: Unknown property 'myObjmyController.checkIn'" I think its looking for 'checkIn' in myController when it only exists in this one JS page. – Daft Oct 2 '13 at 14:44
  • 2
    Visualforce is just sorta lame and wants the method in your controller to be named getCheckIn instead of checkIn. Thats the only reason it cant find the method. – Phil Rymek Oct 2 '13 at 14:54
  • Cheers Phil. Had to fool around with it a bit more but is finally working. – Daft Oct 2 '13 at 16:19
3

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

  • Cheers for the suggestion amatorVitae! – Daft Oct 2 '13 at 16:20
2

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;
    }  
}
  • Cheers for the help mast0r! – Daft Oct 2 '13 at 16:19

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.