FREQUENTLY ASKED SALESFORCE INTERVIEW QUESTIONS WITH ANSWERS 


1) Write a Apex trigger to update related Contacts phone number whenever Accounts phone number is updated.

TRIGGER TO UPDATE RELATED RECORDS BASED ON CHANGES IN PARENT RECORD

Requirement: Whenever, phone number gets updated in Account object, all the related Contacts should also be updated with same phone number.



Solution: This can be achieved by using Trigger.



Account "Test Account" has two contacts and phone numbers are highlighted for both Accounts and Contacts.

Changing Phone number of "Test Account" to 5555555555 and same will be updated to both contacts.

Trigger:

//Creating a Trigger on "before Update" event.

Trigger AccConUpd on Account (before Update) {

               map<id,account> accdtl = new map<id,account>();

 

              //For loop based on number of records in Trigger.New.

              //It will check if the phone field has changed or not

              //because trigger will execute even if some other fields

              //gets updated in Account object.

 

              for(integer i=0;i<trigger.new.size();i++) {

                       if(trigger.old[i].phone != trigger.new[i].phone) {

                            accdtl.put(trigger.old[i].id, trigger.new[i]);

                        } 

               }



            //Creating list for Contact object and updating the phone

            //field based on Account object's phone field.

 

           list<contact> updcont = new list<contact>();

           for(contact c: [select accountid, phone from contact

                 where accountid in :accdtl.keyset()]) {

                       account accupd = accdtl.get(c.accountid);

                       c.phone = accupd.phone;

                       updcont.add(c);

             }

     update updcont;

}



2) Write a Visualforce page to display the Account information on entering Account Name as input by user


3) What is Sandbox and explain different types of sandboxes?

Sandboxes are the environment where we develop and test our code before implementing it into the production. There are four types of Sandboxes.



Developer Sandbox :

It can copy only the Metadata from production

Data storage is limited to 200 MB

Refresh interval is 1 day.

Developer Pro Sandbox:

It can also copy only the metadata from production

Data storage is limited to 1 GB.

Refresh interval is 1 day.

Partial Copy Sandbox:

It can copy copy both data and metadata from production.

Data storage is limited to 5 GB and can copy maximum of 10000 records from a single object.

Refresh interval is 5 days.

Full Copy Sandbox:

It is similar to production and everything from production can be copied.

No limit on data storage.

Refresh interval is 29 days.

4) What is the difference between Trigger.New, Trigger.Old, Trigger.NewMap and Trigger.OldMap?



Trigger.New: It returns the list of new records which are updated or inserted on objects

Trigger.Old: It returns the list of old records which are updated or deleted on and from objects.

Trigger.NewMap: It returns the Map of IDs of new records. Available only for Update and Insert.

Trigger.OldMap: It returns the Map of IDs of old records. Available only for Update and Delete.



5) What are the different ways to make a field required in an object?

While creating the field

Using Page Layout

Using Validation Rule

With the help of Triggers

Using Field Level Security

6) Write an apex code to send a mail.

APEX CODE TO SEND MAIL

Requirement: Apex code to send mails.



Solution: This can be achieved by writing Apex code.



Apex Code:



public class EmailManager {

 // Public method



      public void sendMail(String address, String subject, String body) {



          // Create an email message object



          Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();



          String[] toAddresses = new String[] {address};



          mail.setToAddresses(toAddresses);



          mail.setSubject(subject);



          mail.setPlainTextBody(body);



          // Pass this email message to the built-in sendEmail method



         // of the Messaging class



          Messaging.SendEmailResult[] results = Messaging.sendEmail(



                                   new Messaging.SingleEmailMessage[] { mail });

     }

}



Go to Developer console ===> Debug ===> Open Execute Anonymous Window:



EmailManagers Email = new EmailManagers();

Email.Sendmail('abc@xyz.com', 'Salesforce Blog!', 'Welcome to my Blog');

7) Explain Deployment through Force.com migration tool using Ant

There are three files associated with ANT:



build.properties: This file will have username, password, server URL

build.xml: This file will have the command which has to be executed. example, Deploycode or Removecode or retrievecode.

package.xml: This will have the metadata which should be deployed, retrieved or removed.



8) What will happen when we load data from data loader where required field is empty?

If the fields are marked Mandatory on page layout level, all the records would be inserted.

If the fields are marked Mandatory except page layout, all the records will be inserted apart from records which don't have data in mandatory field.

9) What is SOSL, SOQL and DML in salesforce?



SOQL: Salesforce Object Query Language. It is used to query data from objects. It is similar to SQL but here tables are objects and columns are the object fields. Records are retrieved using SELECT keyword.



SOSL: Salesforce Object Search Language. It is similar to SOQL but it returns list of list of objects because it works on multiple objects. Records are retrieved using FIND keyword.



DML: Data Manipulation Language. This is used to manage records in salesforce. Keywords used are UPDATE, DELETE, INSERT, UPSERT,  MERGE, UNDELETE.

10) Limitation of executing queries in Salesforce:

SOQL:

Total number of queries issued in single transaction is 100.

Total number of records retrieved is 50000.

SOSL:

Total number of queries issued in single transaction is 20.

Total number of records retrieved is 2000.

DML:

Total number of DML statements in single transaction is 150.

Total number of records processed will be 10000.



11) How do you export data from Visualpage into excel and PDF?

We can export data into excel and PDF by using below content inside <apex:page>

EXCEL:contentType="application/vnd.ms-excel#SalesForceExport.xls"

PDF: renderAs="pdf"



12) Explain the order of execution in Salesforce.

System Validation Rule

Before Triggers

Custom Validation rules

After Triggers

Assignment rules

Auto-Response rules

Workflow Rules

Before and After triggers are executed again if workflow rules updates or inserts any field

Escalation rules

Formula fields

Sharing Rules

Post commit logic

13) What happens when Lead is converted?
Whenever a Lead is converted, Salesforce creates accounts, contacts and optionally opportunity using the details from Lead.
14) Write a query to find duplicate Email ID associated with Contacts.

Select Email from Contact Group by Email having Count(Email) > 1;
15) Difference between Data Loader and Import Wizard
Data Loader:

It can load 5 million records.

All standard and custom objects are supported.

Schedule regular data imports.

It can both Export and Import data.

We can delete data using data loader

Can be operated either using Use Interface or Command Line.

Import Wizard:

It is an internal tool.

It can only import data.

It can import upto 50 thousand records.

Supports all custom objects and few standard objects ( Contacts, Leads, Accounts,Solution and Campaign)

Cannot delete records using Import Wizard.

16) How to insert null values using Data loader?

By checking "Insert Null Values" at Data loader setting page. This option is not available for Bulk API is enabled.



17) Can we delete records using Workflow?

No. We cannot delete records using workflow.



18) How do you know that Lead is converted?

By checking Lead Status = Closed-Converted. Also, the converted lead wont be searchable unless admin has given View and Edit converted Lead permission.



19) How to generate single report for multiple objects?

This can be achieved by using Joined Reports which can use multiple report types.



20) What is Custom Label?

Custom Label enables developers to create multilingual apps by presenting information in user's native language.These are custom texts, where all the information is pre-loaded by developers which will be displayed based on the users language). It can be used in both Apex and Visualforce pages.

Create Custom Label from setup. let the name of custom label be blog.

Use them in Visualforce page : {!$ label.blog}

Use them in Apex : System.label.blog

21) How to delete data from production using Ant?

To delete components, use the same procedure as with deploying components, but also include a delete manifest file that’s named destructiveChanges.xml and list the components to delete in this manifest.



22) What is Test Class and why do we need it?

A Test class is an apex class that tests your logic written in either apex class or trigger programatcally. A test class actually ensurs that your code is working fine as expected.



This is an important thing when you are doing deployment in production as salesforce allows deployment only when your 75% of the code is covered through test classes.



23) Can we delete user? Explain why.

No, we cant delete user and can only deactivate. Users cannot be deleted because any thing created in Salesforce has "created by" field and if we delete the user, those fields data will also get deleted.



24) How do you expose class to REST webservice?

Making your Apex class available as a REST web service is straightforward. Define your class as global, and define methods as global static. Add annotations to the class and methods. For example, this sample Apex REST class uses one method. The getRecord method is a custom REST API call. It’s annotated with @HttpGet and is invoked for a GET request.



@RestResource(urlMapping='/Account/*')

global with sharing class MyRestResource {

    @HttpGet

    global static Account getRecord() {

        // Add your code

    }

}