Annotations used in Salesforce

Annotations:

Apex supports the following annotations:
  • @Deprecated
  • @Future
  • @IsTest
  • @ReadOnly
  • @RemoteAction


  • Apex REST annotations:
    • @RestResource
    • @HttpDelete
    • @HttpGet
    • @HttpPatch
    • @HttpPost
    • @HttpPut

@Deprecated:

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. This is useful when you are refactoring code in managed packages as the requirements evolve. New subscribers cannot see the deprecated elements, while the elements continue to function for existing subscribers and API integrations.
Note the following rules when deprecating Apex identifiers:
  • Unmanaged packages cannot contain code that uses the deprecated keyword.
  • When an Apex item is deprecated, all global access modifiers that reference the deprecated identifier must also be deprecated. Any global method that uses the deprecated type in its signature, either in an input argument or the method return type, must also be deprecated. A deprecated item, such as a method or a class, can still be referenced internally by the package developer.
  • webService methods and variables cannot be deprecated.
  • You can deprecate an enum but you cannot deprecate individual enum values.
  • You can deprecate an interface but you cannot deprecate individual methods in an interface.
  • You can deprecate an abstract class but you cannot deprecate individual abstract methods in an abstract class.
  • You cannot remove the deprecated annotation to undeprecate something in Apex after you have released a package version where that item in Apex is deprecated.

@Future:

Use the future annotation to identify methods that are executed asynchronously. When you specify future, the method executes when Salesforce has available resources.
For example, you can use the future annotation when making an asynchronous Web service callout to an external service. Without the annotation, the Web service callout is made from the same thread that is executing the Apex code, and no additional processing can occur until the callout is complete (synchronous processing).
Methods with the future annotation must be static methods, and can only return a void type.

@Istest:

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.

@ReadOnly:

The @ReadOnly annotation allows you to perform unrestricted queries against the Force.comdatabase. All other limits still apply. It's important to note that 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 to System.schedule, calls to methods annotated with @future, and sending emails.
The @ReadOnly annotation is available for Web services and the Schedulable interface. To use the @ReadOnly annotation, the top level request must be in the schedule execution or the Web service invocation. For example, if a Visualforce page calls a Web service that contains the @ReadOnly annotation, the request fails because Visualforce is the top level request, not the Web service.
@RemoteAction:
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.
@RestResource:
The @RestResource annotation is used at the class level and enables you to expose an Apex class as a REST resource.
These are some considerations when using this annotation:
  • The URL mapping is relative to https://instance.salesforce.com/services/apexrest/.
  • A wildcard character (*) may be used.
  • The URL mapping is case-sensitive. A URL mapping for my_url will only match a REST resource containing my_url and not My_Url.
  • To use this annotation, your Apex class must be defined as global.

@HTTPDelete:

The @HttpDelete annotation is used at the method level and enables you to expose an Apex method as a REST resource. This method is called when an HTTP DELETE request is sent, and deletes the specified resource.

@HTTPGet:

The @HttpGet annotation is used at the method level and enables you to expose an Apex method as a REST resource. This method is called when an HTTP GET request is sent, and returns the specified resource.
These are some considerations when using this annotation:
  • To use this annotation, your Apex method must be defined as global static.
  • Methods annotated with @HttpGet are also called if the HTTP request uses the HEAD request method.

@HTTPPatch:

The @HttpPatch annotation is used at the method level and enables you to expose an Apex method as a REST resource. This method is called when an HTTP PATCH request is sent, and updates the specified resource.
To use this annotation, your Apex method must be defined as global static.
@HTTPPost:
The @HttpPost annotation is used at the method level and enables you to expose an Apex method as a REST resource. This method is called when an HTTP POST request is sent, and creates a new resource.To use this annotation, your Apex method must be defined as global static.
@HTTPPut:
The @HttpPut annotation is used at the method level and enables you to expose an Apex method as a REST resource. This method is called when an HTTP PUT request is sent, and creates or updates the specified resource.
To use this annotation, your Apex method must be defined as global static.

Comments

Popular posts from this blog

List of Key Prefixes in Salesforce

SFDX Install CPQ in Scratch org