anusha(salesforce developer)

Calling Methods from Javascript and Calling methods from controller

Q 1> Calling controller method using Javascript in VF page
Solution:
- ActionFunction (http://www.cloudforce4u.com/2013/06/actionfunction-in-apex.html)
- Using JavaScript remoting 


Q 2> Different ways to call Apex methods from VF page
Solution:
a. There are components using "action" keyword like mentioned below, They can call apex methods from VF page
- <apex:Page>  Refer Example 3
- <apex: commandbutton> Refer Example 4
- <apex :commandLink> Refer Example 5

b. Also AJax methods like mentioned below can call apex methods from VF page
- actionSupport Refer Example 6 and (http://www.cloudforce4u.com/2013/06/difference-between-action-support-and.html)
- ActionFunction (fRom javascript) Refer Example 7

c. Also using Getter methods we can call Apex methods from VF page Refer Example 8
- Having a getter methods in controller and caling that method name in the VF page component

Example 3:
VF Page:
<apex:page standardController="Account" extensions="OrderPadController"
    action="{!openPage}">
</apex:page>
OR
<apex:page standardController="Account" action="/apex/Gantt?aid={!$CurrentPage.parameters.id}">
    Look Mom, two hands!
</apex:page>

Controller:


Example 7:
Visualforce page:

<apex:page controller="sample">
    <!-- Calling Controller Method from Visualforce page -->   
    <!-- Javascript -->
    <script type="text/javascript">
        function jsfind()
        {
            callfind();
        }
    </script>   
    <apex:pagemessages />   
    <apex:form >   
    <apex:actionFunction name="callfind" action="{!searchAndfind}" reRender="a" />   
    <apex:pageBlock >
        <apex:pageblockSection >
            <apex:outputLabel >Query:</apex:outputLabel>
            <apex:inputText value="{!qry}"/>       
        </apex:pageblockSection>
      
        <apex:pageBlockButtons >
            <apex:commandButton value="Query" onclick="jsfind()"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>   
    </apex:form>
</apex:page>

Controller:
public class sample
{
    public String qry{get;set;}
    public pageReference searchAndfind()     {
        pageReference pg = new pageReference('http://www.google.com/search?q='+qry);
        pg.setRedirect(true);
        return pg;
    }     
}

Example 8:
VF Page: <apex:outputText value="{!ContactFromEmail}" />
Controller:
public String getContactFromEmail()
    {
        Contact con = [SELECT id, Email, Name, Title FROM Contact where Email = 'email@awesomeemail.com' limit 1];
        contactID = con.id;
        return  con.Name + ' ' + con.Email + ' ' + 'Title: ' + con.Title;

1 comment:

  1. I liked your blog I do have doubts please mail me kalyani.selam@gmail.com

    ReplyDelete