Showing posts with label Apex Concepts. Show all posts
Showing posts with label Apex Concepts. Show all posts

Monday, April 19, 2021

Access Modifiers

 There are 7 access modifiers in Salesforce.

1. Private:- If you declare a class as a private it is only known to the block in which it is declared.

          Note:- By default all the inner classes are private.

2. Public:- If you declare a class as a public, this class is visible throughout your application and you can access the class anywhere inside the application.

3. Global:- If you declare a class as a global in a class then it is visible to all the apex class in the application or outside the application.

         Note:- If a method or a class(inner) is declare as global then the top level class also must be declared as global.

4. With Sharing:- If you declare a class as a with sharing, sharing rules given to the current user will be taken into the consideration and the user can access and perform the operations based on the permissions given to that user on objects and fields(field level security, sharing rules).

5. Without Sharing:- If you declare a class as a without sharing then this class runs in system mode  which means apex code has access to all the objects and fields irrespective of current users sharing rules, field level security and object permissions.

      Note:- 

       i) If the class is not declared as with sharing or without sharing then the class is by default taken as without sharing.

      ii) Both inner class and outer classes can be declared as with sharing.

      iii) If innerclass is declared as with sharing and top level class is declared as without sharing then by default entire context will run in with sharing context.

      iv) If a class is not declared as with | without sharing and if this class is called by another class in which sharing is enforced then both the class run with with sharing.

      v) Outer class is declared as with sharing and inner class is declared as without sharing then inner class runs in without sharing context only (Inner class don't take the sharing properties from outer class).

6. Virtual:- If a class is declared with keyword Virtual then this class can be extended(inherited) or this class methods can be overridden by using a class called overridden.

7. Abstract:- This calss contains Abstract methods.

Saturday, September 22, 2018

Future Methods

1. If a transaction has any long running operations/statements, and if this operations is not having any dependency on rest of the operation then we can run the operation independent from the rest of the operation by using future method.
Ex: Long running operations like: Webservices calls, Bulk DML operations.

Best practices of future methods:
----------------------------------------
All the future methods should have @future annotation
1) Methods with the future annotation must be static methods
2) can only return a void type
3) The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types
4) Methods with the future annotation cannot take sObjects or objects as arguments.
5) You can invoke future methods the same way you invoke any other method. However, a future method can’t invoke another future method
--> When we invoke the future method they will be added queue and from the queue they will be executed.
--> If  you want to invoke webservices from the future method then define
                                    @future(callout-true)
6) No more than 50 method calls per Apex invocation
7) Asynchronous calls, such as @future or executeBatch, called in a startTest, stopTest block, do not count against your limits for the number of queued jobs
8) The maximum number of future method invocations per a 24-hour period is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater
9) To test methods defined with the future annotation, call the class containing the method in a startTest(), stopTest() code block. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously
10) Any Asynchronous job that is running in salesforce will be registered with AsyncApexJob object.
11)How to track the status of the future method
a. Write a soql query on AsyncApexJob
b. Declarative way to check the status
    Setup
       |--- Moniter
                  |--- Jobs
                          |--- Apex Jobs
12) Future methods can be used to increase the governing limits
           @future(limits = dml*2)
13) Future method will not return any JOBID in the apex code.
14) Future method will handle Mixed DML operations and Bulk DML operations

NOTE:- The reason why sObjects can’t be passed as arguments to future methods is because the sObject might change between the time you call the method and the time it executes. In this case, the future method will get the old sObject values and might overwrite them.  To work with sObjects that already exist in the database, pass the sObject ID instead (or collection of IDs) and use the ID to perform a query for the most up-to-date record. The following example shows how to do so with a list of IDs

Usecases: When we have a requirement to pass sobject as parameter to future method how will you do?
a. Pass recordid instead of record
b. Serialize the sobject as string using JSON/XML and pass string aas parameter.