Friday, May 23, 2014

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 quiry, with the above scenario we can insert all 99 records and gather the error log of that 1 records.
 - 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 dynamicall build SOQL queries at run time to query for all fields on a record.
 
-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]

 
 
-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

-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()

-String is a class, and name few methods in it
5 methods of String Class
- Contains
-equals
-endswith
-indexof
-isblank
-isnumeric
-substring
-compareTo

-Set is class, Name the methods in it
add
addall
size
contains

-Map is class, Name the methods in it
get
keyset
containskey
size
values
put

-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();




Interview Question on Triggers

1) What the are events of triggers?
- After insert,After Update & After Delete

2) Upsert trigger can call which all events?
-After Update & After Insert

3) What is merge operation?

4) Which events will be called during merger operation?
- Ans Delete n Update

5) In After Undelete, we cannot see trigger.old, only trigger.new can be seen.True or False?
       -TRUE

6) Static variable declarion in trigger is possible,True or false?
     -TRUE but it will not work.

7) In After update trigger event to Save a record do we need DML statement?
- Yes

8) In before insert trigger event can we have trigger.new and trigger.newmap?
- we can only have trigger.new, Trigger.newmap is not possible because still the record is not inserted             and record Id is not generated.

9) Can we have batch/asynchronous method called from a Trigger?
- YES

10) Can we manually call triggers?
- No triggers cannot be called manually. It will automatically called for Insert/delete/update DML        
          operations only.

11) Can we write trigger for Profile object?
- No, generally there will be no scenarios to call trigger on Profile Object.

12) Will trigger fire after Workflow rule?
-Only Update triggers will fire once.

13) You have picklist field with 3 values :New, Latest,Old. Now you fire a trigger which will update the picklist field with 'Deleted' value for a record. Is this possible?
- YES

14) Can you update formula field through Trigger?
- NO

15) I need to update Trigger.Old records how can i do this?
- This is not possible because Trigger.Old is read only list.

16) Can we display Validation Error messages in After events of a Trigger?
- We Can display Error messages in After events, but there will be no purpose serverd. Because after inserting/Updating the record the error message will be dispalyed.

17)Consider a User logsin by a profile which does not have eidt access to Account object. But he/she tries insert a record to Account object, which in turns fires a trigger which updates  record in the Account Object.
Will the Trigger Update the Record?
- Yes the Trigger can update the Record because Trigger always runs is System context.






3 comments:

SFDC scenario based interview Questions

1) Consider this scenario I  have a profile  by name 'ProvideReadAccess' and two users U1 and U2 assigned to it. And I have obje...