Interview Questions
Q:
What is the data type of Trigger.New?
-
Trigger.New is of Data Type List (A collection of records).
Q:
What is the difference between Trigger.New and Trigger.Map?
-
Trigger.New returns a ordered list of records but Trigger.Map returns a map(Key
value pair).
Q:
What is the difference between User Context and System Context? /What is the
difference between running in user Mode or system Mode?
-
In User Context/Mode the execution of class/method takes place considering the
logged in users
Permission (Sharing rules, OWD, field level security...)
In System Context/Mode none of the permissions associated with the logged in
user is considered.
The execution takes place as though the user has full
fledged rights on everything.
Q:
Explain the key words With Sharing and Without Sharing.
-
Consider a Custom Object 'MarksDetail' has OWD security set to
'Private',
Now lets say we have 2 classes 'TestResult' and 'ExamResult'
Follow the below scenarios to arrive the solution
// Declaring the Class 'TestResult' with a method 'returnTestResult'
public with
Sharing Class TestResult
{
public void retunTestResult()
{
// Query to return a set of data
[select id from customobject];
}
}
Lets
consider customObject has OWD as private
ThenThe
output of this query will be the records created by the logged in user.
//
Declaring the Class 'ExamResult' with a method 'returnExamResult'
public without
Sharing Class ExamResult
{
public void returnExamResult()
{
//
Query to return a set of data
[select id from customobject];
}
TestResult obj = new TestResult();
obj.retunTestResult();
}
Lets
consider customObject has OWD as private
ThenThe
output of this query will be all the records in the custom objects.
Hence,
Without sharing will not respect the sharing rules/owd/manualsharingetc..
Q:
Why go for Dynamic SOQL quires?
-
Lets say we are inserting 100 records, all the 99 records will get inserted
without any error but 1 records fails.
Then in normal SQOl query inserting, the entire transaction will fail and
no records will be inserted.
But when we go for Dynamic SOQL query, with the above scenario we can
insert all 99 records and gather the error log of that 1 record.
-
Also Salesforce SOQL does not allow Select * from Account query (Which would
get all the fields from a object)
In order to implement this we can Salesforce 'Schema Describe' object to
dynamicalyl build SOQL queries at run time to query for all fields on a record.
Q:
System is Class, Name few methods in it
5
System Methods.
assert [Ex: System.assert]
assertequals [Ex: System.assertequals]
assertNotEqual [Ex: System.assertNotEqual]
debug [Ex: System.debug]
currentPageReference [Ex: System.currentPageReference]
now [Ex: System.now]
runas [Ex: System.runas]
Today [Ex: System.Today]
Q:
Test is Class, Name few test methods in it
The
following are methods for Test Class. All methods are static.
5
Test Methods
startTest
[Ex: Test.startTest]
stoptest
[Ex: Test.stoptest]
isrunningtest()
setCurrentPage
Q:
UserInfo is Class, Name few methods in it
-Contains
methods for obtaining information about the context user.
5
UserInfo methods
-getProfileID()
-getUserId()
-getFirstName
-getLAstName
-getName
-getsessionId()
-getUserRoleId()
-getUserType()
Q:
String is a class, and name few methods in it
5
methods of String Class
-
Contains
-equals
-endswith
-indexof
-isblank
-isnumeric
-substring
-compareTo
Q:
Set is class, Name the methods in it
add
addall
size
contains
Q:
Map is class, Name the methods in it
get
keyset
containskey
size
values
put
Q:
sObject is Class,few methods in this class are
sObject
methods are all instance methods, that is, they are called by and operate on a
particular instance of an sObject, such as an account or contact. The following
are the instance methods for sObjects.
addError
clear
get
getSObjectType()
put
-
ApexPages is a class,few methods in this class are
-
addMessage
-
currentPage
-
addMessages
Q:
How can I turn my returned List<SObject> into a Set<Id>? Is the
best option just a for loop?
Solution
a: You can use the Map constructor that accepts an SObject list to do this
without consuming a script statement for each element in the List.
For
example:
List<SObject>
results = Database.query(someSOQL);
Set<Id>
resultIds = (new Map<Id,SObject>(results)).keySet();
What
this second line does is create a new Map<Id,SObject> from the results
list using a special constructor, and then take the map's set of keys via the
keySet()
method.
Then the map falls out of scope and it's heap space is released, leaving you
with a very governor-efficient set.
Solution
b:If you are not using Dynamic SOQL,
Set<Id>
ids = (new Map<Id, Lead>([SELECT Id FROM Lead])).keySet();
This
is how you would do it with Dynamic SOQL but you must cast...
Set<Id>
ids = (new Map<Id, Lead>((List<Lead>)Database.query(query))).keySet();
I liked this article very much. The content is very good. Keep posting.
ReplyDeleteSales force Online Training
Thanks a lot for sharing.
ReplyDeleteSalesforce Online Training