Annotations in Apex in Salesforce.com

Annotations in Apex in Salesforce.com

An Apex annotation modifies the way a method or class is used, similar to annotations in Java.
Annotations are defined with an initial @ symbol, followed by the appropriate keyword. To add an annotation to a method, specify it immediately before the method or class definition.

Apex supports the following annotations:
1. @Deprecated
2. @Future
3. @IsTest
4. @ReadOnly
5. @RemoteAction
6. @TestVisible
Apex REST annotations:
1. @RestResource(urlMapping='/yourUrl')
2. @HttpDelete
3. @HttpGet
4. @HttpPatch
5. @HttpPost
6. @HttpPut

@future annotation in Salesforce:
   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).

Note:
    1. Methods with the future annotation must be static methods and can only return a void type.
    2. The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types.
    3. Methods with the future annotation cannot take sObjects or objects as arguments.

TestVisible annotation in Salesforce

TestVisible annotation allows test methods to access private or protected members of another class outside the test class. These members include methods, member variables, and inner classes.

Sample Class:

public class TestVisibleExample {
    // Private member variable
    @TestVisible private static Integer recordNumber = 1;

    // Private method
    @TestVisible private static void updateRec() {
    }
}  

Test Class:

@isTest
private class TestVisibleExampleTest {
    @isTest static void test1() {
        // Accessing private variable annotated with TestVisible
        Integer i = TestVisibleExample.recordNumber;
        System.assertEquals(1, i);

        // Accessing private method annotated with TestVisible
        TestVisibleExample.updateRecord();
    }

}

Comments

Popular posts from this blog

Adding a red asterisk to required fields using label in LWC

The Developer Console didn't set the DEVELOPER_LOG trace flag on your user. Having an active trace flag triggers debug logging. You have 1,978 MB of the maximum 1,000 MB of debug logs. Before you can edit trace flags, delete some debug logs.

Salesforce: Serial and Parallel Approval