Calling an apex method on press of enter key

Use following javascript along with action function to execute apex method on press of enter key.

Javascript:
<script type='text/javascript'>
function runOnEnter(ev) {
if (window.event && window.event.keyCode == 13 || ev.which == 13) {
searchAccRecs();
return false;
} else {
return true;
}
}
</script>

 
Action function:
<apex:actionFunction name="searchAccRecs" action="{!searchAcc}" reRender="pgblcktbl"/>


Lets see an example to demo this:
In the following example we want to search accounts with name that is being entered in the text box. This should happen when submitt button is pressed plus should also happen if user enters the text and presses Enter key. For this we should write a javascript as mentione dabove and call it on event of keypress for input text. Java script call the action function which intern calls the apex method.

Visualforce Page


<apex:page standardController="account" extensions="searchaccounts">
<apex:form >
<script type='text/javascript'>
function runOnEnter(ev) {
if (window.event && window.event.keyCode == 13 || ev.which == 13) {
searchAccRecs();
return false;
} else {
return true;
}
}
</script>
<apex:pageBlock >
<apex:inputText value="{!searchString}" onkeypress="return runOnEnter(event);"/>
<apex:commandButton value="Submit" action="{!searchAcc}" reRender="pgblcktbl"/>
<apex:pageBlockTable value="{!accList}" var="acc" id="pgblcktbl">
<apex:column value="{!acc.name}"/>
<apex:column value="{!acc.accountnumber}"/>
<apex:column value="{!acc.annualrevenue}"/>
<apex:column value="{!acc.Industry}"/>
<apex:column value="{!acc.AccountSource }"/>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:actionFunction name="searchAccRecs" action="{!searchAcc}" reRender="pgblcktbl"/>
</apex:form>
</apex:page>


Apex Class 


public with sharing class searchaccounts {
Public List<account> accList{get;set;}
Public string searchString{get;set;}
public searchaccounts(ApexPages.StandardController controller) {
}

Public void searchAcc(){
accList = new List<account>();
accList = [select id,name,accountnumber,annualrevenue,Industry,AccountSource from account where name =: searchString limit 5];
}
}












Related Articles

salesforce interview questions for developers 15


153. What is the maximum allowed time limit while making a callout to external service in apex?
Ans - maximum of 120 second time limit is enforced while making callout to external service


154. How can you expose an apex class as a REST web service in salesforce?
Ans - An apex class can be exposed as REST web service by using keyword '@RestResource'


155. What are custom controllers?
Ans - Custom controller is an apex class that implements all of the logic for a vf page without leveraging standard controller


156. Can a custom controller class accept arguments?
Ans - No. A custom controller cannot accept any arguments and must use a no argument constructor for outer class.


157. What different type of method can be defined for VF controllers?
Ans - getter methods, setter methods and action methods


158. What are getter methods, setter methods?
Ans - Getter methods retrieve data from controller while setter methods pass data from page to controller.


159.What is Organisation Wide Default in salesforce?

Ans - Organization wide default is used to set default access level for various standard objects and all custom objects. Default access can be set as Public read only, Public read write, Public read write and transfer for different objects. For example if the record access level is set to private then the records will be accessible only to owners, users above in role hierarchy and if it shared using manual sharing.


   Important Salesforce Topics Collection   

Interview questions and answers salesforce 14

Part 1 (1-15) - Part 2 (16 -30) - Part 3 (31 -45) - Part 4 (46 -55) - Part 5 (56 -66) - Part 6 (67 - 78) - Part 7 (79 - 90) - Part 8 (91 - 100) - Part 9 (101- 111) - Part 10 (112 - 120) - Part 11 (121 - 130) - Part 12 (131 - 137) - Part 13(138 - 145) - Part 14 (146 - 152) - Part 15 ( 153 - 159)- Part 16 ( 160 - 168) Part 19 (188 - 201)

146. What are the different exceptions in apex
Ans - Apex exceptions can be built in or we can also create our own custom exceptions. Exception class is the super class of all the built in exceptions. So as to create a custom exception the class name should end with string 'exception' and it should extend any of the built in exception class or other custom exception class.

147. What different access modifiers can be used for apex classes?
Ans - Apex classes can have either or these three access modifiers 1. Private 2. public 3. global

148. What different access modifiers can be used for class members?
Ans - Class members can have any of these four access modifiers 1. global 2. public 3. protected 4. private

149. What are governers limit in salesforce?
Ans - Governers limit are run time limits that are enforced by salesforce. Governers limits are reset per transaction.

150. What is an apex transaction?
Ans - An apex transaction represents set of operations that are executed as a single synchronous unit.

151. What does heap in apex mean?
Ans - Every apex transaction has its heap. Heap is nothing but the garbage collected at the end of every apex transaction.

152. How many callouts to external service can be made in a single apex transaction?
Ans - A total of 10 callouts are allowed in a single apex transaction

Part 1 (1-15) - Part 2 (16 -30) - Part 3 (31 -45) - Part 4 (46 -55) - Part 5 (56 -66) - Part 6 (67 - 78) - Part 7 (79 - 90) - Part 8 (91 - 100) - Part 9 (101- 111) - Part 10 (112 - 120) - Part 11 (121 - 130) - Part 12 (131 - 137) - Part 13(138 - 145) - Part 14 (146 - 152) - Part 15 ( 153 - 159)- Part 16 ( 160 - 168) Part 19 (188 - 201)

SFDC interview questions and answers 13

Part 1 (1-15) - Part 2 (16 -30) - Part 3 (31 -45) - Part 4 (46 -55) - Part 5 (56 -66) - Part 6 (67 - 78) - Part 7 (79 - 90) - Part 8 (91 - 100) - Part 9 (101- 111) - Part 10 (112 - 120) - Part 11 (121 - 130) - Part 12 (131 - 137) - Part 13(138 - 145) - Part 14 (146 - 152) - Part 15 ( 153 - 159)- Part 16 ( 160 - 168) Part 19 (188 - 201)

138. What is @future annotation in apex?
Ans - It is used to indicate that code should be executed asynchronously when salesforce has available resources.


139. What are the different considerations that must be followed when using @future annotation in apex method?
Ans - 1. Method must be static 2. It must return void and 3. method can have argument of type primitives,collection of primitives or arrays of primitives
 


140. How can you execute batch apex programmatically?
Ans - Batch apex can be invokde using Database.executebatch(). Ex- Id batchprocessId = Database.executebatch(mybatchapexclass,200)

141. How can you set the batch size in batch apex ?
Ans - This can be set using optional scope parameter in database.executebatch (batchapexname,batchsize) example - Id batchprocessId = Database.executebatch (mybatchapexclass,200) in this example batch size is set to 200


142. How can you monitor batch apex job?
Ans - Batch apex job can be monitored by navigating to your name --> setup --> monitoring --> Apex jobs

143. What is the difference between List type custom setting and Hierarchy type custom setting?
Ans - List type stores static data that can be used in apex code as it is examples could be country codes,currencies etc
          Hierarchy type stores data that may change depending on user,profile or org wide default


144. Where can custom setting data be accessed?
Ans - Custom setting data can be accessed in formula fields,validation rules,workflow rules,apex,API
145. What different return types can be returned by SOQL?
Ans - A SOQL can return List of objects, a single object or an integer
 


Part 1 (1-15) - Part 2 (16 -30) - Part 3 (31 -45) - Part 4 (46 -55) - Part 5 (56 -66) - Part 6 (67 - 78) - Part 7 (79 - 90) - Part 8 (91 - 100) - Part 9 (101- 111) - Part 10 (112 - 120) - Part 11 (121 - 130) - Part 12 (131 - 137) - Part 13(138 - 145) - Part 14 (146 - 152) - Part 15 ( 153 - 159)- Part 16 ( 160 - 168) Part 19 (188 - 201)