Tuesday, 28 January 2014

Various ways of deployment in salesforce

You can deploy your components using following ways.

1. Change set
2. Eclipse
3. Ant

Visualforce pages can be edited in production, however apex classes/trigger cannot be edited in production.

Change set can be accessed from UI.
Using eclipse it is click and deploy.
And finally ANT is the best option when you have large number of components for deployment. We have make a package.xml and then deploy the whole package.

Best practices in apex development

Best practices in apex development

Some of the best practices an apex developer should follow

1. Do not write any soql query within for loop   

2. Use collections especially map to write logic instead of iterating over for loop within for loop, this would save number of statements executed

3. Do not query for data in test class, always create data in test class

4. Use try catch for every dml executed

5. Do not fire dml within for loop

6. Use comments appropriately

7. Static strings should be stored in custom labels

8. Constants, strings likly to be changed should be managed using custom settings

9. Try to see that One object should have only one trigger 

10. No logic with in trigger, move all logic into class

11. Use assert statements in test class, test classes arent meant only for test coverage.

12. Cover positive as well as negative test cases in test class

13. Bulkify trigger, your triggershould work for bulk of a data insert update etc

Monday, 20 January 2014

sosl salesforce example

sosl salesforce example

SOSL is a select query that returns list of lists. That is, it can return list of multiple objects. It searches a parameter within fields fo multiple objects as specified in the query.
In the below example string 'test' is being searched in 4 objects Opportunity,account,Lead and contact. You need to specify in which fields it should search this string.
For example, if you want it to search in name and description fields of account then it would look like  account( name,description)
The list of lists has list of objects in the order mentioned in the query. For example if you specify account as first object in query; then the account list will be available at 0th location of list of lists.
visualforce page

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<apex:page controller="DeferenceDemoController">
  <apex:form >
    <apex:commandButton value="Show records using SOSL" action="{!soslDemo_method}"/>
    <apex:pageBlock title="Accounts">
       <apex:pageblockTable value="{!accList }" var="acc">
          <apex:column value="{!acc.name}"/>
          <apex:column value="{!acc.Type}"/>
       </apex:pageblockTable>
    </apex:pageBlock>
 <apex:pageBlock title="Contacts">
    <apex:pageblockTable value="{!conList}" var="con">
      <apex:column value="{!con.name}"/>
      <apex:column value="{!con.email}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
 <apex:pageBlock title="Leads">
    <apex:pageblockTable value="{!leaList}" var="lea">
      <apex:column value="{!lea.name}"/>
      <apex:column value="{!lea.company}"/>
    </apex:pageblockTable>
 </apex:pageBlock>
 <apex:pageBlock title="Opportunities">
    <apex:pageblockTable value="{!optyList}" var="opty">
      <apex:column value="{!opty.name}"/>
     <apex:column value="{!opty.StageName}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
  </apex:form>
</apex:page>

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Public with sharing class DeferenceDemoController {
 Public List<Opportunity> optyList {get;set;}
 Public List<Lead> leaList{get;set;}
 Public List<contact> conList{get;set;}
 Public List<account> accList{get;set;}
   Public DeferenceDemoController(){
   }
  Public void soslDemo_method(){
   optyList = New List<Opportunity>();
   leaList = New List<Lead>();
   conList = New List<contact>();
   accList = New List<account>();
   List<List <sObject>> searchList = [FIND 'test' IN ALL FIELDS RETURNING  Account (Id,Name,type),Contact(name,email),Opportunity(name,StageName),Lead(company,name,status) ];
   accList = ((List<Account>)searchList[0]);
   conList  = ((List<contact>)searchList[1]);
   optyList = ((List<Opportunity>)searchList[2]);
   leaList  = ((List<Lead>)searchList[3]);
  }
}





As of now the SOSL can return only 2000 records (Combined for all the objects that are queried in the SOSL query) thsi is as per governer's limit