Thursday 7 December 2017

Salesforce Interview Questions - Part 2

23. How to delete the users data from Salesforce?
Ans : To delete the Users Data go to Setup | Administration Setup | Data Management |  Mass Delete Record, from there select the objects like Account, Lead etc and in criteria select the users name and delete all records of that user related to particular object.

24. How to restrict the user to see any record, lets say opportunity?
Ans : set up opportunity sharing to be private.  If both users are admins or have view all records on opportunity, then that overrides private sharing.

25. What is the difference between trigger.new and trigger.old in Apex – SFDC?
Ans :
Trigger.new :
Returns a list of the new versions of the sObject records.
Note that this sObject list is only available in
insert and
update triggers, and the records can only be modified in
beforetriggers.
Trigger.old :
Returns a list of the old versions of the sObject records.
Note that this sObject list is only available in
update and
delete triggers.

26. How to restrict any Trigger to fire only once ?
Ans:
Triggers can fire twice, once before workflows and once after workflows, this is documented at
“The before and after triggers fire one more time only if something needs to be updated. If the fields have already been set to a value, the triggers are not fired again.”
Workaround:
Add a static boolean variable to a class, and check its value within the affected triggers.
1
public class HelperClass {
2
   public static boolean firstRun = true;

3
}
4
trigger affectedTrigger on Account (before delete, after delete, after undelete) {

5
    if(Trigger.isBefore){
6
        if(Trigger.isDelete){

7
            if(HelperClass.firstRun){
8
                Trigger.old[0].addError('Before Account Delete Error');

9
                HelperClass.firstRun=false;
10
            }

11
        }
12
    }

13
}

27.  What is difference between WhoId and WhatId in the Data Model of Task ?
Ans :
WhoID refers to people things. So that would be typically a Lead ID or a Contact ID
WhatID refers to object type things. That would typically be an Account ID or an Opportunity ID

28. Where is the option of the report for the “Custom Object with related object” and what are the condition to generate related reports?
Ans :
If the parent object is the standard object provided by the salesforce like “Account”, “Contact” then the report will be in there section with related custom object.
If both objects are the custom then the report will be in “Other Reports” Sections.
Following are the conditions to get the report of related objects:
·                     On both the objects, Reports option must be enable.
·                     The relationship between both of them must be “Master – detail relationship”.

29. How you can provide the User Login (Authentication) in Public sites created by Salesforce.
Answer : We can provide the authentication on public sites using “Customer Portal”.

This part of the interview question mainly focus on the dynamic Apex feature of the 
salesforce.com .
30 : What is the dynamic Apex?
Ans :

Dynamic Apex enables developers to create more flexible applications by providing them with the ability to “Access sObject and field describe information”, “Write Dynamic SOQL Queries”, “Write Dynamic SOSL Queries”and “Dynamic DML”.

31 : How to get the list of all available sobject in salesforce database using Apex (Dynamic Apex)?
Ans:
1
Map<String, Schema.SObjectType> m =  Schema.getGlobalDescribe();

32 : How to create instance of sobject dynamically? Normally the sobject is created like “Account a = new Account();”. But if you are in situation that you don’t know which sobject is going to be instantiated ? Means it will be decided at runtime, how you will handle it? Hint : Use Dynamic Apex.
Ans:
1
public SObject getNewSobject(String t)
2
{

3
4
    // Call global describe to get the map of string to token.

5
    Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
6

7
    // Get the token for the sobject based on the type.
8
    Schema.SObjectType st = gd.get(t);

9

10
    // Instantiate the sobject from the token.

11
    Sobject s = st.newSobject();
12

13
    return s;
14
}

33 : How to get all the fields of sObject using dynamic Apex?
Ans:
1
Map<String, Schema.SObjectType> m  = Schema.getGlobalDescribe() ;
2
Schema.SObjectType s = m.get('API_Name_Of_SObject') ;

3
Schema.DescribeSObjectResult r = s.getDescribe() ;
4
Map<String,Schema.SObjectField> fields = r.fields.getMap() ;

34 : How to get all the required fields of sObject dynamically?
Ans:

There is no direct property available in Apex dynamic API to represent the required field. However there is another way to know about it.
If any field have below three properties then it is mandatory field.
1.                  If it is Creatable
2.                  If it is not nillable and
3.                  If it does not have any default value
1
Map<String, Schema.SObjectType> m  = Schema.getGlobalDescribe() ;
2
Schema.SObjectType s = m.get(so.apiName) ;

3
Schema.DescribeSObjectResult r = s.getDescribe() ;
4
Map<String,Schema.SObjectField> fields = r.fields.getMap() ;

5
6
for(String f : fields.keyset())

7
{
8
    Schema.DescribeFieldResult desribeResult = fields.get(f).getDescribe();

9
    if( desribeResult.isCreateable()  && !desribeResult.isNillable() && !desribeResult.isDefaultedOnCreate() )
10
    {

11
//This is mandatory / required field
12
    }

13
}

35 : How to display error messages in the visualforce page ?
Ans:
In Apex use below code to create the error message for visualforce.
1
Apexpages.addMessage( new ApexPages.Message (ApexPages.Severity.ERROR, 'Required fields are missing. '));
in Visualforce page add below tag where you want to display the error message.
<apex:pageMessages ></apex:pageMessages>

36 : What is property in Apex? Explain with advantages.
Ans: 

Apex mainly consist of the syntax from the well known programming language Java. As a practice of encapsulationin java we declare any variable as private and then creates the setters and getters for that variable.
1
private String name;
2
public void setName(String n)

3
{
4
  name = n;

5
}
6
public String getName()

7
{
8
 return name;

9
}
However, the Apex introduced the new concept of property from language C# as shown below:
1
public String name {get; set;}
As we can see how simple the code is and instead of using nearly 8 to 11 lines all done in 1 line only. It will be very useful when lots of member is declared in Apex class. It has another advantage in “number of lines of code” limit by salesforce which will drastically reduced.

37 : What is the controller extension ?
Ans:

Any apex class having a public constructor with Custom Controller or Standard Controller object as a single argument is known as controller extension.

38 : Explain the need or importance of the controller extension.
Ans:

Controller extension is very useful and important concept introduced by the salesforce recently. It gives the power to programmer to extend the functionality of existing custom controller or standard controller.
A Visualforce can have a single Custom controller or standard controller but many controller extensions.
we can say that the custom extension is the supporter of custom or standard controller.
Consider one example : If there is one controller written and used by the multiple visualforce pages and one of them needs some extra logic. Then instead of writing that logic to controller class (Which is used by many visualforce pages) we can create a controller extension and apply to that page only.

39 : How to read the parameter value from the URL in Apex?
Ans:

Consider that the parameter name is “RecordType”.
1
String recordType = Apexpages.currentPage().getParameters().get('RecordType');
40. What is Master Detail relationship and look up relationship in Salesforce?
Ans:
Master Detail relationship is the Parent child relationship. In which Master represents Parent and detail represents Child. If Parent is deleted then Child also gets deleted. Rollup summary fields can only be created on Master records which will calculate the SUM, AVG, MIN of the Child records.
Look up relationship is something like “has-a” (Containership) relationship. Where one record has reference to other records. When one record is deleted then there is no impact on other records.

41. Can we convert the lookup relationship to Master Detail relationship?
Ans:
We can convert the lookup relationship to master detail relationship if and only if all the existing record has valid lookup field.

42. In How many way we can invoke the Apex class?
Ans:
1.                  Visualforce page
2.                  Trigger
3.                  Web Services
4.                  Email Services

43. Can we create Master Detail relationship on existing records?
Ans:
No. As discussed above, first we have to create the lookup relationship then populate the value on all existing record and then convert it.

44. How validation rules executed? is it page layout / Visualforce dependent?
Ans :
The validation rules run at the data model level, so they are not affected by the UI. Any record that is saved in Salesforce will run through the validation rules.

45. What is the difference between database.insert and insert ?
Ans:
insert is the DML statement which is same as database.insert. However, database.insert gives more flexibility like rollback, default assignment rules etc. we can achieve the database.insert behavior in insert by using the method setOptions(Database.DMLOptions)
Important Difference:
·                     If we use the DML statement (insert), then in bulk operation if error occurs, the execution will stop and Apex code throws an error which can be handled in try catch block.
·                     If DML database methods (Database.insert) used, then if error occurs the remaining records will be inserted / updated means partial DML operation will be done.

46. What is the scope of static variable ?
Ans:
When you declare a method or variable as static, it’s initialized only once when a class is loaded. Static variables aren’t transmitted as part of the view state for a Visualforce page.
Static variables are only static within the scope of the request. They are not static across the server, or across the entire organization.

47. Other than SOQL and SOSL what is other way to get custom settings?
Ans:
Other than SOQL or SOSL, Custom seting have there own set of methods to access the record.
For example : if there is custom setting of name ISO_Country,
1
SO_Country__c code = ISO_Country__c.getInstance(‘INDIA’);
2
//To return a map of data sets defined for the custom object (all records in the custom object), //you would use:

3
Map<String,ISO_Country__c> mapCodes = ISO_Country__c.getAll();
4
// display the ISO code for India

5
System.debug(‘ISO Code: ‘+mapCodes.get(‘INDIA’).ISO_Code__c);
6
//Alternatively you can return the map as a list:

7
List<String> listCodes = ISO_Country__c.getAll().values();

48. What happen if child have two master records and one is deleted?
Ans :
Child record will be deleted.

49. What is Difference in render, rerender and renderas attributes of visualforce?
Ans:
render – It works like “display” property of CSS. Used to show or hide element.
rerender – After Ajax which component should be refreshed – available on commandlink, commandbutton, actionsupport etc.
renderas – render page as pdf, doc and excel.

50. What is Scheduler class in Apex?
Ans:
The Apex class which is programed to run at pre defined interval.
Class must implement schedulable interface and it contains method named execute().
There are two ways to invoke schedular :
1.                  Using UI
2.                  Using System.schedule
The class which implements interface schedulable get the button texted with “Schedule”, when user clicks on that button, new interface opens to schedule the classes which implements that interface.
To see what happened to scheduled job, go to “Monitoring | Scheduled jobs “
Example of scheduling :
1
scheduledMerge m = new scheduledMerge();
2
String sch = '20 30 8 10 2 ?';

3
system.schedule('Merge Job', sch, m);
To see how to make crone job string – refer this URL.


1 comment:

  1. 2. Hi There,

    Interesting piece!Great to see someone write Salesforce Interview Questions who is not a fanatic or a complete skeptic.

    I am trying to complete the Trailhead for Paths and Workspaces and am encountering an issue where I have to add an event via the quick action "New Event".

    This button does not exist on the page layout. I have added it to all Lead page layouts, the Lead object does not currently utilise record types and I have enabled the Lightning buttons override. Despite this, there is still no button appearing under Activity for "New Event" as the Trailhead indicates there should be.

    There are two threads on this issue that have been marked as SOLVED however the solutions posted there have no helped in resolving this issue for me.

    If anyone can help provide a solution that would be great. I would really like to complete this Trailhead!

    Appreciate your effort for making such useful blogs and helping the community.

    Many Thanks,
    Preethi.

    ReplyDelete

Batch Apex

1. What are transaction limits in apex? Total number of SOQL queries issued1 - 100 Total number of records retrieved by SOQL queries - 50...