1

Disclaimer : This is an homework exercise. Sort of. I'm trying to learn Visualforce + APEX + web design in a very short term, so I'm asking my friends to put up challenging questions for me.

VF page

Controller

My aim : show an alert/message when the user presses a button. Using ActionFunction. I must use Action Function

Problems I am facing :

  1. Please look at my code VF page. I have marked the problem with a comment.
  2. I donot know enough about AJAX/JS to fulfill all requirements.
  3. The code is behaving weird.
  4. I can display the alert/message without Action Function, I dont know how WITH AF.
  5. The page refreshes after I click the button. Does a command button ALWAYS refresh after you click it?

I will expand on problem 1 :

If I remove the offending line, the code does not work. I click the button and page refreshes. NOTHING happens.

If I keep the offending line, when I click the button, it displays a JS alert and a few seconds later, it displays the Salesforce type error/message. WHY??

Is it possible to call a JS function from the controller?

My logic is like this :

  1. Click the button.
  2. It calls a JS function
  3. the JS function calls the function related to the Action Function, and THAT in turn calls the controller function associated with the AF.
  4. the controller calls back/returns value/changes a getter/setter to some value.
  5. the value is taken by some other JS function defined in the page and it triggers a condition check (I'm pulling words out of my @$$ here) and THAT displays and alert.
  6. phew

I hope this is not asking for the impossible??

I donot ask you folks to provide me a full detailed answer, just a few nudges in the right direction would be enough!! I dont need much hand holding.

Sorry for the wall of text.

1

Below is my VF page:

<apex:page controller="exampleCon">
    <script>    
function mtdOne(){  




alert('m inside javascript');

    controllermtd();

    }


    </script>
    <apex:form id="frm">


    <apex:outputText value="Clicked" id="showstate" rendered="{!flag}"/>


   <apex:commandButton value="Hit Me" onclick="mtdOne();return false;" />


    <apex:actionFunction action="{!sayhello}" name="controllermtd" rerender="frm">

    </apex:actionFunction>
    </apex:form>
</apex:page>

Below is the controller:

/*** Controller ***/
public class exampleCon {

    public boolean flag{get;set;}
    public static integer i=1;

    public examplecon(){


    flag=false;
    }



    public PageReference sayHello() {


        flag=true;



        return null;
    }



}

Hope this helps you(I am rerendering outputtext using actionfunction in my example)

| improve this answer | |
  • If this helps you, mark this as the solution – Abhijeet Jul 29 '16 at 12:21
  • This IS the answer! Im sorry Im late, I couldnt get to a PC on the weekend. You're a wizard... but can you please explain whats happening in the commandbutton onlick part? Thanks! – fraglord Aug 1 '16 at 7:01
  • in the onClick(), we basically call up the javascript method.Inside that javascript method, i am calling the controller method by referring its name.We can also pass parameters using <apex:param> Please refer to salesforce documentation for more understanding :) – Abhijeet Aug 1 '16 at 11:26

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.