Salesforce Menu

Apex interview Question

1. What is Apex?
Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Force.com platform server in conjunction with calls to the Force.com​ API. Using syntax that looks like Java and acts like database stored procedures, Apex enables developers to add business logic to most system events, including button clicks, related record updates, and Visualforce pages. Apex code can be initiated by Web service requests and from triggers on objects.

2. What is difference between public and global class in Apex ?
Public class can be accessed within application or namespace. This is not exactly like public modifier in Java.
Global class visible everywhere , any application or namespace. WebService must be declared as Global and which can be accessed inside Javascript also. It is like public modifier in Java.

3. Explain Considerations for Static keyword in Apex.
Apex classes cannot be static.
Static allowed only in outer class.
Static variables not transferred as a part of View State.
Static variables and static block runs in order in which they are written in class.
Static variables are static only in scope of request.

4. What are the Primitive data type in Apex?
1. Blob – This data type represents binary data stored as a single object.
Example: Blob b1 = Blob.valueof(‘idea’);

2. Boolean – This data type represents Value that can only be assigned true, false, or null.
Example: Boolean isvar = true;

3. Date – This data type represents particular day.
Example: Date myDateVar = Date.today();
Date weekStart = myDateVar.toStartofWeek();

4. Datetime – This data type represents particular time and date.
Example: Datetime myDateTimeVar = Datetime.now();
Datetime newd = myDateTimeVar. addMonths(5);

5. Decimal – This data type represents Number that includes a decimal
point. Decimal is an arbitrary precision number.
Example:Decimal myVar = 12.4567;
Decimal divDecVar = myVar.divide(7, 2, System.RoundingMode.UP);
system.assertEquals(divDecVar,1.78);

6. Double – Represents 64-bit number that includes a decimal point. Minimum value -2^63. Maximum value of 2^63-1.
Example:Double d=3.14159;

7. ID – Represents 18-character Force.com record identifier.
Example:ID id=’00300000003T2PGAA0′;

8. Integer – 32-bit number that doesn’t include a decimal point. Minimum value
-2,147,483,648 — maximum value of 2,147,483,647
Example: Integer i = 1;

9. Long – Represents 64-bit number that doesn’t include a decimal point. minimum value of -263 — maximum value of 263-1.
Example:Long l = 2147483648L;

10. String – Represents Set of characters surrounded by single quotes.
Example:String s1 = ‘Hello’;

11. Time – Represents particular time.
Example: Time myTimeVar = Time.newInstance(18, 30, 2, 20);
Integer myMinutes = myTimeVar.
minute();

5. what are access modifiers in Apex?
Apex allows 4 types of modifiers.
public: This keyword is used to Defines a class or method or variable that can be used by any Apex in this application or namespace.
private: This key word is used to Defines a class/method/variable that is only known locally, within the section of code in which it is defined. This is the default scope for all methods and variables that do not have a scope defined.
protected: This keyword defines a method/variable that is visible to any inner classes in the defining Apex class.
global: Defines a class, method, or variable that can be used by any Apex that has access to the class, not just the Apex in the same application.

6. How many types of Email provide by Apex?
There are two types of emails provide by an Apex
1. Outbound email messaging – Used to send an email to external system using apex.
 There are two types of outbound emails.
  SingleEmailMessage: Used to send a single email message.
  Syntax :
Messaging.SingleEmailMessage varEmail = new Messaging.SingleEmailMessage();

  MassEmailMessage: By using this we can send email to a list oh recipients.
  Syntax :
Messaging.MassEmailMessage varMemail = new Messaging.MassEmailMessage();

2. Inbound email messaging -This will recieve an email from external system to Salesforce and the apex class will process the email, attachements and perform requested operations.

7. What are the database manipulation operation keyword in Apex?
insert : This keyword is used to insert one or more records. See the below example to understand how can we use this in apex code.
Example:
Lead l = new Lead(Company=’ABC’,LastName=’Rahul’);
insert l;
update: This keyword is used to update/modifications to the existing records. See the below example to understand how can we use this in apex code.
Example:
Account a = new Account(Name=’MyAcc1′);
insert a;
Account myAcct = [SELECT Id, Name, BillingCity FROM Account WHERE Name = ‘MyAcc1’ LIMIT 1];
myAcct.BillingCity = ‘Melbourne’;
try {
 update myAcct;
} catch (DmlException e) {
}

upsert: This keyword is used to creates/insert new records and updates existing records. See the below example to understand how can we use this in apex code.
Example:
Account[] acctsList = [SELECT Id, Name, BillingCity FROM Account WHERE BillingCity = ‘Bombay’];
for (Account a : acctsList)
{a.BillingCity = ‘Mumbai’;}
Account newAcct = new Account(Name = ‘Ramco’, BillingCity = ‘Hyderabad’);
acctsList.add(newAcct);
try {
upsert acctsList;
}
catch (DmlException e) {
}

delete: This key word is used to delete the records.
Example:
Account[] delAccts = [SELECT Id, Name FROM Account WHERE Name = ‘domnos’];
try {
 delete delAccts;
} catch (DmlException e) {
// Process exception here
}

undelete: This keyword is used to restore the records from the recyclebin.
Example:
Account[] accts = [SELECT Id, Name FROM Account WHERE Name = ‘myAcc’ ALL ROWS];
try {
 undelete accts;
}
catch (DmlException e) {
}
Merge: This keyword merges up to three records of the same type into one of the records, deleting the others, and re-parenting any related records.
Example:
List accList = new List{new Account(Name=’Myacc1′),new Account(Name=’MyAcc2′)};
insert accList;
Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = ‘Myacc1’ LIMIT 1];
Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = ‘MyAcc2’ LIMIT 1];
try {
 merge masterAcct mergeAcct;
} catch (DmlException e) {
}

8. Which SOQL statement can be used to get all records even from recycle bin or Achieved Activities?
We will need “ALL Rows” clause of SOQL.
Example : SELECT COUNT() FROM Contact WHERE AccountId = a.Id ALL ROWS

9. How can you lock record using SOQL so that it cannot be modified by other user.
Ans : we will need “FOR UPDATE” clause of SOQL.
Sample :
Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];

10. What is sObjects?
A sObject is any object that can be stored in the Force.com platform database. These are not objects in the sense of instances of Apex classes; rather, they are representations of data that has or will be persisted.

These persisted objects can be treated as first class citizens within the Apex language, which makes the database integration particularly intuitive and easy to use.

sObject is a generic abstract type that corresponds to any persisted object type. The generic sObject can be cast into a specific sObject type, such as an account or the Invoice_Statement__c custom object.

This creates an invoice statement, which corresponds to the Invoice_Statement__c custom object, without setting any fields and assigns the new invoice statement to an sObject.

sObject s = new Invoice_Statement__c();

11. What are the types of Collections available in Apex?
List (ordered and allow duplicates)
Set (unordered and won’t allow duplicates)
Map (Key and value pair)

12. What is the difference between List and Set ?

List

Set

List is Ordered.

Set is unordered.

List allows duplicates.

Set doesn’t allow duplicates.

We can access list elements with index.

Set elements cannot be accessed with index.

We can sort list elements with sort method (default sorting order is ascending).

sort method is not available for Set.

Contails method is not available in List.

Contains method is available for Set to search for a particular element in the set

We can process records which are stored in list using DML statements(insert, update, delete and undelete).

We cannot process records which are stored in set using DML statements.

13. What is the maximum size of the list?
No limit for the size of a list. It only depends on the heap size which is 6 MB (Synchronous) and 12 MB (Asynchronous).

14. What are the map methods available in Apex?
keyset(): To fetch only keys from the map.
values(): To fetch only values from the map.
containsKey(value): To search a key from the map.
get(key): By supplying the key we can fetch the value.
put(key,value): To add key and value in a map.

15. Governor Limits for DML statements ?
Number of DML statements per transaction: 150 (as a whole including insert, update, delete and undelete)
Number of rows processed per DML stmt: 10000

16. What is SOQL?
SOQL: Salesforce Object Query Language
SOQL Purpose: To fetch info. from an object and related objects.
We can write query on one object while querying on those objects we can fetch the child object info. or parent object info. (we cannot capture un related objects info.)
SOQL queries per transaction: 100.
SOQL query rows returned: 50000.

17. What is SOSL?
SOSL: Salesforce Object Search Language
SOSL Purpose:We can search for a value in multiple objects (no need of any relationship).
Results of SOSL query can be stored in List of List.
SOSL queries per transaction: 20.
SOSL query rows returned: 2000.

Subscribe Now