Salesforce Interview with Salesforce.com


String s = ‘smims’
Given string is palindrome or not ?

Palindrome is a string that reads the same backward as well as forward and can be of odd or even length. A palindrome number is a number which is equal to its reverse.

String original= 'smims';
String reverse = ' ';

for (Integer i = original.length()-1; i >= 0; i--)
{
reverse += original.substring(i, i+1);
}
system.debug(reverse.equals(original)); //true

When a opportunity is created then billing address of accounts should get updated into the opportunity? What are the different ways to do it? What are pros and cons of each one of them?

We can do this in various ways

1. Formula Field

Pros 
Fast & easy to implement and does not require APEX

Can be configured on the fly

Cons
Field is updated everytime you load the page.

It is read- only field

We can't fire a trigger or a workflow based on the change of a formula field

2. Workflow

Pros 
They aren't subject to governor limits and behave well during mass transactional DML.

Cons
Workflow rules invalidate previously valid fields so that is always a demerit of using them

3. Process builder

Pros 
Process builder allows for a one process per object design pattern hence encouraging more consolidation and better control over number of workflows/processes per object limits

Process builder has the capability to accommodate a formula field  
We can develop directly in production.

DML Operations(insert, update, delete, etc.) are supported.

Cons
Process would fail if a validation rule is encountered.

Process frequently runs into bulkification issues and if there are too many transactions happening, a data load might cause the Process to fail.

4.Flows

Pros 

Cons

5.Trigger

Pros 
Users have more control of process.

Cons
Need to do coding taking more time 


When a opportunity is created then billing address of accounts should get updated into the opportunity? Write a trigger for it?

trigger oppTrigger on Opportunity (before insert) { Set<String> ids = new Set<String>(); for(Opportunity opp : trigger.new){ ids.add(opp.AccountId) ; } // Add all fields to SOQL Map<Id, Account> m = new Map<Id, Account>([SELECT Id, Name ,BilingAddress__c FROM Account Where Id IN: ids]); for(Opportunity opp : trigger.new){ Account a = m.get(opp.AccountId) ; opp.BilingAddress__c = a.BilingAddress__c ; } }

What are the best practices of writing test classes?

1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .

  • Single Action -To verify that the the single record produces the correct an expected result .
  • Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
  • Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
  • Negative Testcase :-Not to add future date , Not to specify negative amount.
  • Restricted User :-Test whether a user with restricted access used in your code .
10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method  is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't  send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true)
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method  and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .


What is batch apex?

Batch Apex operates over small batches of records. It breaks the processes into manageable chunks. The class implements Database.Batchable interface and class must be global. It contains three methods that must be implemented:

Start method

The start method is called at the beginning of a batch Apex job. Use the start method to collect the records or objects to be passed to the interface method execute.

Syntax: global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {}

 This method returns either a Database.QueryLocator object or an iterable that contains the records or objects being passed into the job.


Execute Method

 The execute method is called for each batch of records passed to the method. Use this method to do all required processing for each chunk of data.

Syntax: global void execute(Database.BatchableContext BC, list<P>){}


This method takes the following:
o    A reference to the Database.BatchableContext object.
o    A list of sObjects, such as List<sObject>, or a list of parameterized types. If you are using a Database.QueryLocator, the returned list should be used.
Batches of records execute in the order they are received from the start method.

Finish Method

Syntax: global void finish(Database.BatchableContext BC){}

The finish method is called after all batches are processed. Use this method to send confirmation emails or execute post-processing operations.
Each execution of a batch Apex job is considered a discrete transaction.

Example 

A batch Apex job that contains 1,000 records and is executed without the optional scope parameter from Database.executeBatch is considered five transactions of 200 records each.

Note

The Apex governor limits are reset for each transaction. If the first transaction succeeds but the second fails, the database updates made in the first transaction are not rolled back.

Minimum size for Batch Apex is 1.

Default size of Batch Apex is 200

The maximum size for Batch Apex is 2000.

Which method of batch is synchronous?

start method, finish method is the synchronous method in batch apex

How to test a batch class?

We should always test batch class with good amount of data (minimum 200 records) in our test class. As best practice, we must execute the batch class between Test.StartTest() and Test.StopTest(). We should sure that your start method is returning at least one record from Database.getQueryLocator query Example Test.stopTest(); Database.executeBatch(new yourBatchClass()); Test.stopTest();

How to add quick actions into lightning?

Quick Action/Lightning Actions is an Object-specific action. These actions let users create records that have automatic relationships to other records, make updates to specific records, and interact with records in ways that you define.

Quick Action can be used to send an email, create child record,submit record for approval

To display a lighting component as a Quick action to an object, It must be implemented withforce:lightningQuickAction OR force:lightningQuickActionWithoutHeader.

Example
<aura:component implements="force:lightningQuickAction"> 
<center><b>This is a Lightning component </b></center>
</aura:component>

Add Lighting component as a Quick Action to an object
 Add Quick Action to Object Page Layout




Select a record >> Click on the actions dropdown >> Select your Quick Action/Lightning Action which have use Lightning Component. 



Result



Points to remember:
  • Components that implement the force:lightningQuickActionWithoutHeader interface display in a panel without additional controls and are expected to provide a complete user interface for the action.
  • We cannot add both force:lightningQuickAction and force:lightningQuickActionWithoutHeader action to a single lightning component.
  • We cannot add lightning application as a quick action.

How to do exception handling in lightning?

Exception is an event which occur during execution of  program instruction and disrupts the normal flow of program instructions. Exception handling is a mechanism to handle exception so that normal flow of program should be maintained. 

To overcome such situations, we can make use of AuraHandledException. Its use gives us a chance to handle the exception more gracefully in our client code. System exceptions do not contain important details for security purposes, and always result in the dreaded “An internal server error has occurred…” message.If we make use of AuraHandledException, we can handle the exception occurring at the server side as well as return a more customized message to be displayed at the client side.

How to pass one component into another in lightning?

Suppose I have 2 lightning components :
the first display a list of products, for each product there is a button that redirect to the other component that display some information about the selected product


Solution
Send the ID in event and in doInit function of second controller , call again the apex class to retrieve the data for that particular ID

Give some of the governor limits?

Total heap size6 MB/12 MB
Total number of DML statements issued150
Total number of records retrieved by a single SOSL query2000
Total number of SOSL queries issued20
Total number of records retrieved by Database.getQueryLocator10000
Total number of records retrieved by SOQL queries50000



References
https://webkul.com/blog/exception-handling-lightning-component/ 

https://success.salesforce.com/answers?id=9063A000000pDOlQAM

https://www.jitendrazaa.com/blog/salesforce/why-to-avoid-using-workflow-rule-and-process-builder-field-update-with-trigger/

http://www.infallibletechie.com/2018/09/workflow-field-updates-vs-process.html


Share this

Related Posts

Previous
Next Post »