Annotations
An Apex annotation modifies the way a method
or class is used, similar to annotations in Java.
Annotations are
defined with an initial @ symbol,
Apex
annotations:
·
@Future
§ @IsTest
§ Apex REST annotations:
o @HttpGet
o @HttpPut
@depreceated annotation
Use the deprecated annotation to identify methods, classes, exceptions, enums,
interfaces, or variables that can no longer be referenced in subsequent
releases of the managed package in
which they reside.
@deprecated
// This method is deprecated. Use myOptimizedMethod(String a,
String b) instead.
global void myMethod(String a) {
global void myMethod(String a) {
}
@future annotation
Use the future annotation to identify methods that are executed
asynchronously. When you specify future, the method executes when Salesforce has available
resources.
Methods with the future annotation must be static methods, and can only return a void
type.
global class MyFutureClass {
@future
static void myMethod(String a, Integer i) {
System.debug('Method called with: ' + a + ' and ' + i);
//do callout, other long running code
}
}
No more than 10 method calls per Apex invocation
§ Salesforce also
imposes a limit on the number of future method invocations: 200 method calls per
full Salesforce user license or Force.com App
Subscription user license, per 24 hours.
§ The specified parameters must be primitive data
types, arrays of primitive data types, or collections of primitive data types.
§ Me thods with the future annotation cannot take sObjects or objects as
arguments.
§ Methods with the future annotation cannot
be used in Visualforce controllers in either getMethodName or setMethodName methods, nor in
the constructor.
@istest annotation
Use the isTest annotation to define classes or individual methods that only
contain code used for testing your application. The isTest annotation is similar to creating methods declared as testMethod.
Classes defined
with the isTest annotation don't count against your organization limit of 3 MB for all Apex code.
Classes and
methods defined as isTest can be either private or public. Classes defined as isTest must be top-level classes.
@isTest
private class MyTestClass {
// Methods for testing
@isTest static void test1() {
// Implement test code
}
@isTest static void test2() {
// Implement test code
}
}
IsTest(SeeAllData=true) Annotation
For Apex code saved using Salesforce.com API version 24.0
and later, use the isTest(SeeAllData=true) annotation to grant test classes and individual test methods
access to all data in the organization, including pre-existing data that the
test didn’t create.
@ReadOnly annotation
The @ReadOnly annotation allows you to perform unrestricted queries against
the Force.comdatabase.
All other limits still apply. this annotation, while removing the limit of the
number of returned rows for a request, blocks you from performing the following
operations within the request: DML operations, calls toSystem.schedule, calls to methods annotated with @future, and sending emails.
@Remote
action annotation
The RemoteAction annotation provides support for Apex methods used in Visualforce to be called via JavaScript. This
process is often referred to as JavaScript remoting. Methods with the RemoteAction annotation must be static and either global or public.
To use JavaScript
remoting in a Visualforce page, add the request as a
JavaScript invocation with the following form:
[namespace.]controller.method(
[parameters...,]
callbackFunction,
[configuration]
);
In your
controller, your Apex method declaration is preceded with the @RemoteAction annotation like this:
@RemoteAction
global static
String getItemId(String objectName) { ... }
Execution Governors and Limits
Because Apex runs in a multitenant
environment, the Apex runtime engine strictly
enforces a number of limits to ensure that runaway Apex does not monopolize shared
resources. These limits, or governors,
track and enforce the statistics outlined in the following table. If some Apex code ever exceeds a limit, the
associated governor issues a runtime exception that cannot be handled.
Description
|
Limit
|
Total number of SOQL
queries issued1
|
|
Total number of SOQL
queries issued for Batch Apex and future methods1
|
|
Total number of
records retrieved by SOQL queries
|
|
Total number of
records retrieved by Database.getQueryLocator
|
|
Total number of SOSL
queries issued
|
|
Total number of
records retrieved by a single SOSL query
|
|
Total number of DML
statements issued2
|
|
Total number of
records processed as a result of DML statements, Approval.process, or database.emptyRecycleBin
|
|
Total number of
executed code statements
|
|
Total number of
executed code statements for Batch Apex and future methods
|
|
Total heap size for
Batch Apex and future methods
|
|
Total stack depth for
any Apex invocation that recursively fires triggers due to insert, update, or delete statements4
|
16
|
For loop list batch
size
|
|
Total number of
callouts (HTTP requests or Web services calls) in a request
|
|
Maximum timeout for
all callouts (HTTP requests or Web services calls) in a request
|
|
Default timeout of
callouts (HTTP requests or Web services calls) in a request
|
|
Total number of
methods with the future annotation allowed per Apex invocation5
|
|
Maximum size of
callout request or response (HTTP request or Web services call)6
|
|
Total number of sendEmail methods allowed
|
|
Total number of
describes allowed7
|
|
Total number of
classes that can be scheduled concurrently
|
25
|
Remote Action Example
<apex:page
controller="MyJSRemoting">
<script
type="text/javascript">
function getAccountJS() {
var accountNameJS = document.getElementById('accountname').value;
vignan.MyJSRemoting.getAccount( accountNameJS, function(result, event)
{
if (event.status)
{
document.getElementById("{!$Component.theBlock.pbs.pbsi2.accId}")
.innerHTML =
result.Id;
document.getElementById("{!$Component.theBlock.pbs.pbsi1.name}")
.innerHTML =
result.Name;
document.getElementById("{!$Component.theBlock.pbs.pbsi3.phone}")
.innerHTML =
result.Phone;
document.getElementById("{!$Component.theBlock.pbs.pbsi4.type}")
.innerHTML =
result.Type;
document.getElementById("{!$Component.theBlock.pbs.pbsi5.noofemployees}")
.innerHTML =
result.NumberOfEmployees;
}
}, {escape:true});
}
</script>
<input id="accountname" type="text"/>
<button onclick="getAccountJS();">Get Account</button>
<apex:pageblock id="theBlock">
<apex:pageblocksection columns="3" id="pbs">
<apex:pageblocksectionitem id="pbsi1">
<apex:outputtext
id="name" ></apex:outputtext>
</apex:pageblocksectionitem>
<apex:pageblocksectionitem id="pbsi2">
<apex:outputtext
id="accId"> </apex:outputtext>
</apex:pageblocksectionitem>
<apex:pageblocksectionitem id="pbsi3">
<apex:outputtext
id="phone"> </apex:outputtext>
</apex:pageblocksectionitem>
<apex:pageblocksectionitem id="pbsi4">
<apex:outputtext
id="type"> </apex:outputtext>
</apex:pageblocksectionitem>
<apex:pageblocksectionitem id="pbsi5">
<apex:outputtext
id="noofemployees"> </apex:outputtext>
</apex:pageblocksectionitem>
</apex:pageblocksection>
</apex:pageblock>
</apex:page>
global with sharing class MyJSRemoting
{
public static Account account { get; set; }
@RemoteAction
global static Account getAccount(String accountName) {
account = [select id, name, phone, type, NumberOfEmployees from
Account where name = :accountName limit 1];
return account;
}
}
-------------
No comments:
Post a Comment