Pages

Friday, December 16, 2016

Salesforce Best Practices

Apex Best Practices

  • Bulkify your code.
  • Avoid SOQL Queries or DML statements inside FOR loops.
  • Bulkify your helper methods.
  • Using collections, streamlining queries and efficient for loops.
  • Use of limit apex methods to avoid hitting governor limits. Example: Limits.getLimitQueries()
  • Avoid hardcoding of ID's.

Testing Best Practices

  • In the case of conditional logic, execute each branch of code logic.
  • Make calls to methods using both valid and invalid inputs.
  • Use system.assert methods to prove that code behaves properly.
  • Use runAs method to test your application in different user contexts. 
  • Use isTest annotation.
  • Not assume that record Id's are in sequential order.
  • Create all test data before calling the starttest method.
  • Since tests don't commit, you won't need to delete any data. 

Future Method Best Practices

  • Ensure that future methods execute as fast as possible.
  • If using web service callouts, try to bundle all callouts together from the same future method, rather than using a separate method for each callout.
  • Conduct thorough testing at scale. Test that a trigger enqueuing the @future calls is able to handle a trigger collection of 200 records. This helps determine if delays may occur given the design at current and future volumes.
  • Consider using batch apex instead of future methods to process large number of records asynchronously. This is more efficient than creating future request for each record. 

Batch Job Best Practices

  • Only use Batch Apex if you have more than one batch of records. If you don't have enough records to run more than one batch, you are probably better off using Queueable Apex.
  • Tune any SOQL query to gather the records to execute as quickly as possible.
  • Minimize the number of asynchronous requests created to minimize the chance of delays.
  • Use extreme care if you are planning to invoke a batch job from a trigger. You must be able to guarantee that the trigger won't add more batch jobs  than the limit.  

Tuesday, December 13, 2016

Rest Web Services

REST: Representational State Transfer

REST is:
  • Stateless
  • Client-Server based
  • Http based
  • It is not a protocol
  • Supports both JSON and XML
In Rest Web Service we need to implement @RestResource annotation where we need to map end point URL.
Then we need to implement class with following methods
@Httppost = create
@Httpget = read
@Httpput = update
@Httppatch = update
@Httpdelete = delete
Then we need to implement the following
Restcontext: it is a container class it holds two objects
Restrequest: param,requestbody,requestpath
Restresponse: return message etc.

@RestResource(urlMapping = '/v29/account/*')
    global class restwebservices{
 
    @Httpget
    global static account getdetails(){
        Restrequest req = restcontext.request;
        Restresponse res = restcontext.response;
        string name = req.requestURI.substring(req.requestURI.lastindexof('/')+1);
        account a = [select id,name,phone,type,website,rating from account where name =: name];
        return a;
    }

    @Httppost
    global static id createaccont(string name,string phone,string website){
        Restrequest req = restcontext.request;
        Restresponse res = restcontext.response;
        account a = new account();
        a.name = name;
        a.phone = phone;
        a.website = website;
        insert a;
        return a.id;
        }

  }

Open https://workbench.developerforce.com/restExplorer.php and enter the payload as shown below


You can see that an account record has been created.

Monday, December 12, 2016

Helpful Interview Tips

Research:  Like stated above, please do research on the company you are interviewing for.  This includes browsing the company’s website, searching google for any updates about the company, and understanding what type of company you are about to interview with.  Gathering information on a company shows a difference between the great candidates from the good candidates.
Be Concise:  Be sure to listen to the questions, and provide answers to the questions the managers are asking.  Stay on topic.  Managers do not want to listen to candidates get off subject to talk about something that doesn’t answer the original question.
Provide Examples:  Prepare yourself with examples of your work.  It’s almost certain that you will be asked questions about your previous experiences, so rather than just saying that you did it, provide more information regarding how you did it, and how well you succeeded. 
Be Honest:  Do not attempt to say you’ve done something that you haven’t.  A manager will appreciate the honesty.  Many times, a manager will ask a question regarding a skill they know you do not have already, to see if whether you will honestly tell them the truth.  If you have a skill that is related to the skill in the original question, feel free to advise that information, but keep in mind, stay on subject.
Keep Your Guard Up:  Always maintain professionalism.  Do not get too comfortable to say things to the manager that may hinder your changes from getting the job.  Talking about personal hobbies, time after work, and  likes / dislikes shouldn’t be brought up unless the manager does first.  And even then, make sure to keep your answers professional.   

Traditional Job Interview Questions – Courtesy of Monster.com     
•           How would you describe yourself?
•           Why did you leave your last job?
•           What are your long range and short range goals and objectives?
•           What do you see yourself doing five years from now? Ten years from now?
•           What do you really want to do in life?
•           What are your long range career objectives?
•           Why did you choose this career?
•           Can you explain this gap in your employment history?
•           How well do you work with people? Do you prefer working alone or in teams?
•           How would you evaluate your ability to deal with conflict?
•           Have you ever had difficulty with a supervisor? How did you resolve the conflict?
•           What do you consider to be your greatest strengths and weaknesses?
•           How would a good friend describe you?
•           Describe the best job you've ever had.
•           Describe the best supervisor you've ever had.
•           What motivates you to go the extra mile on a project or job?
•           What makes you qualified for this position?
•           What qualifications do you have that make you successful in this career?
•           In what ways do you think you can make a contribution to our company?
•           Do you have any hobbies? What do you do in your spare time?
•           Have you ever been fired or forced to resign?
•           What qualities should a successful manager possess?
•           Do you consider yourself a leader?
•           What are the attributes of a good leader?
•           Describe the workload in your current (or most recent) job.
•           Which is more important: creativity or efficiency? Why?
•           What’s the most recent book you’ve read?
•           Describe the relationship that should exist between the supervisor and those reporting to him or her?
•           What two or three accomplishments have given you the most satisfaction? Why?
•           Describe the most rewarding experience of your career thus far.
•           In what kind of work environment are you most comfortable?
•           How do you work under pressure?
•           What's one of the hardest decisions you've ever had to make?
•           How well do you adapt to new situations?
•           What can you tell us about our company?
•           What interests you about our products?
•           What do you know about our competitors?
•           What major problem have you encountered and how did you deal with it?
•           What have you accomplished that shows your initiative and willingness to work?

Salesforce Interview Questions

1. Name three governor limits?
  • Total number of records retrieved by SOQL queries is 50000.
  • Total number of records retrieved by Database.getQueryLocator is 10000.
  • Total number of SOSL queries issued is 20.
  • Total number of DML statements issued is 150.
  • Total number of callouts in a transaction is 100
  • Total heap size 6 MB (Synchronous Limit), 12 MB (Asynchronous Limit).
2. When do you use before and after trigger?
Before Trigger:
  • Custom validation checks in the same object.
  • Update same record/object.
  • Setting default values
After Trigger:
  • If we need to use Record Id's
  • Inserting or updating the related records.
  • To send notification email post commit.
3. Whats the maximum batch size in a single trigger execution?
      By default size is 200.

4. Whats the differences between 15 and 18 digit record Id's?
      Internally salesforce uses a case-sensitive 15 digit Id's for all records because everything is in control of salesforce but when doing an integration with external applications like access or excel it does not recognize the 15 digit id. The reason is 0014100000ciw1q is different from 0014100000CIW1Q.
      You can get an 18 digit Id of a record by using a formula text field CASESAFEID(id). This is one way to get it.

5. Provide an example of when a custom setting would be used during development.
      Custom settings are hidden gem, they are variables that we use in our code but set and modify outside of our code.

Here's the example.,
Let us write a trigger that sets "Customer Service Rep" field on an Account every time there's a high value opportunity associated with it. Two things are now certain: (1) The CSR on duty changes every week and (2) the threshold for a "high value" opportunity changes often since the company is expanding.

A perfect use case to use Custom Settings is to set the CSR on duty and the "high value" opportunity threshold!

Benefits of using Custom Settings:

  • Change the variable through salesforce.com without deploying the code.
  • Any non-coder admin can now modify your variable and change how your code works!  

6. When should you build solutions declaratively instead of with code?
     As a salesforce best practice, if something can be done using configuration (Declarative) then its preferred over coding. The declarative framework is optimized for the platform and is always preferred.

7. What is a user?
     A user is anyone who logs in to salesforce. Users are employees at your company, such as sales reps, managers and IT specialists, who needs access to the company's records.
   
Still more to come.....