Skip to main content

Interview Questions on: Calling Methods from Javascript and 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;
    }

Comments

Popular posts from this blog

SFDC scenario based interview Questions

1) Consider this scenario I  have a profile  by name 'ProvideReadAccess' and two users U1 and U2 assigned to it. And I have object X. My Question is I want to have ReadWrite access for U1 and ReadOnly access for U2  for the object X. How do I do this? Answer: Read Access for both users is common hence in the Profile settings give 'Read' access for the object 'X' By doing this User U2 will be able to read all the records( One condition satisfied) but U1 will also be able to only read the records(Second condition not satisfied). So next what do we do is we create a permission set say 'GrantWriteAccess' and in this permission set we give the object 'X' the Write access and assign the user U1 to this permission set.(2nd condition satisfied). Explanation about when and where to use permission set and profile https://success.salesforce.com/answers?id=90630000000grVPAAY 2) Consider a scenario I have not given any CRUD...
Interview Questions Q: What is the data type of Trigger.New? - Trigger.New is of Data Type List (A collection of records). Q: What is the difference between Trigger.New and Trigger.Map? - Trigger.New returns a ordered list of records but Trigger.Map returns a map(Key value pair). Q: What is the difference between User Context and System Context? /What is the difference between running in user Mode or system Mode? - In User Context/Mode the execution of class/method takes place considering the logged in users          Permission (Sharing rules, OWD, field level security...)   In System Context/Mode none of the permissions associated with the logged in user is considered.        The execution takes place as though the user has full fledged rights on everything. Q: Explain the key words With Sharing and Without Sharing. - Consider  a Custom Object  'MarksDetail' has OWD security set to 'Private',    Now l...

Custom Labels

Can we have multiple values in Custom Labels. Yes, Salesforce supports multiple values in custom Labels. How to implement that ? Create a New custom label and provide Multiple values with comma ( , ) separated. Example:   Custom Label Name: School Values: Test,Exam,Tutions How to access this Custom Label in Apex controller / Class file Syntax: System.Lable.CustomLabelName Example:System.Label.School How to access this Custom Label in Visualforce page Syntax: !$Label.CustomLabelName Example: !$Label.School How do we check if TempString is present in the custom label in a controller ? Syntax: System.Label.CustomLabelName.Contains(value) Example: System.Label.School.contains(TempString) How do we check if TempString is present in the custom label in a visualforcepage? Syntax: CONTAINS($Label.CustomLabelName,TextValue) Example: CONTAINS($Label.School,"Text")