Wednesday, 29 January 2014

Salesforce.com Reports Explained

 There are 3 kinds of reports in salesforce.com
1) Tabular Reports
2) Summary Reports
3) Matrix Reports
Each Report has a specific use and I am listing down the scenario's in which each type of Reports will be used:-
Tabular Reports: - These kinds of reports are used when the requirement is just to view the data.
Some of the examples are:-
  • Show me all Open Opportunities
  • Show me List of all Accounts which do not have any closed opportunity
  • Show me top 10 Opportunities by revenue
Dashboards cannot be created on Tabular Reports: - Salesforce.com ADM 201 certification question
Summary Reports: - These reports are used when the requirement is to summarize only X Axis. In short if you need to do the sum or calculate the average on even one parameter then summary report is the answer.
Some of the examples are:-
  • Show sum of all Open Opportunities
  • Show opportunities subtotal by my Team
Matrix Reports: - These reports are used when the requirement is to summarize both the Axis i.e. when requirement is to group both Rows as well as Columns.
Some of the examples are:-
  • Show Accounts grouped as Customer or Prospect depending on the opportunity stage
  • Show monthly performance of Salesteam on closing opportunity by Geography for current year
Note:- Charts cannot be made on Tabular Reports as no grouping of data is available

Changing the colors for each cell in the pageblocktable

Here,

I have an object Employeetest__c with fields Name(Text),Status__c(check box) and Salary__c(Number)

Now  i want to change the color of each cell under the Salary column based on condition(for eg:if "salary<5000 then red color otherwise blue etc..) and placing the String literal for each cell under the Status column. (when checkbox is  checked it should be "Active" otherwise "InActive").

For i am writing a controller below:


public class employeetestController{

    public List<Employeetest__c> lstemp=new List<Employeetest__c>();
 
 
    public List<Employeetest__c> getrecs(){
 
        lstemp=[select name,salary__c,status__c from Employeetest__c];
        return lstemp;
 
    }
}

I am writing visualforce page using the above controller as shown below:


<apex:page controller="employeetestController"  sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:pageblocktable value="{!recs}" var="e">
<apex:column value="{!e.name}"/>

<apex:column >
<apex:facet name="header">Salary</apex:facet>
<div style="background-color:{!If(e.Salary__c>=7000 &&    e.Salary__c<10000,'#7CFC00',If(e.Salary__c>=10000 && e.Salary__c<100000,'#DEB887','#DC143C'))}">
{!e.Salary__c}
</div>
</apex:column>
<apex:column >
<apex:facet name="header">Status</apex:facet>
<div>
     {!IF(e.Status__c==true,'Active','InActive')}
</div>
</apex:column>
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
 </apex:page>

Callouts in Batch Apex,State in Batch Apex

Using Callouts in Batch Apex
To use a callout in batch Apex, you must specify Database.AllowsCallouts in the class definition. For example:
global class SearchAndReplace implements Database.Batchable<sObject>, 
   Database.AllowsCallouts{
              //Business logic you want by implementing Batchable interface methods
}
Callouts include HTTP requests as well as methods defined with the webService keyword.
Using State in Batch Apex
Each execution of a batch Apex job is considered a discrete transaction. For example, a batch Apex job that contains 1,000 records and is executed without the optional scope parameter is considered five transactions of 200 records each.
If you specify Database.Stateful in the class definition, you can maintain state across these transactions. When using Database.Stateful, only instance member variables retain their values between transactions. Static member variables don’t and are reset between transactions. Maintaining state is useful for counting or summarizing records as they're processed. For example, suppose your job processed opportunity records. You could define a method in execute to aggregate totals of the opportunity amounts as they were processed.
If you don't specify Database.Stateful, all static and instance member variables are set back to their original values.
The following example summarizes a custom field total__c as the records are processed:
global class SummarizeAccountTotal implements 
    Database.Batchable<sObject>, Database.Stateful{
   global final String Query;
   global integer Summary;
  
   global SummarizeAccountTotal(String q){Query=q;
     Summary = 0;
   }
   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }
   
   global void execute(
                Database.BatchableContext BC, 
                List<sObject> scope){
      for(sObject s : scope){
         Summary = Integer.valueOf(s.get('total__c'))+Summary;
      }
   }
global void finish(Database.BatchableContext BC){
   }
}

Print VF page on button click

VF page Code
============

<apex:page controller="bhadraPieChartController" title="Pie Chart" showHeader="false">
<script>
   //this is only the code for printing the page
    function fun(){
           document.getElementById('prnBtn').style.display='none'; 
           //document.getElementById('winBtn').style.display='none';    
           window.print();
            document.getElementById('prnBtn').style.display='block'; 
    }
</script>
<apex:form >
  <div  style="padding-left: 484px;padding-top: 27px;"><input type="button"   onClick="fun();" id="prnBtn"  value="Print This Page" style="background: green;"/></div>
</apex:form>
    <apex:chart height="350" width="450" data="{!pieData}">
        <apex:pieSeries dataField="data" labelField="name"/>
        <apex:legend position="right"/>
    </apex:chart>
</apex:page>
==========
Controller
============

public class bhadraPieChartController {


  public List<BhadraPieWedgeData> getPieData() {

       List<BhadraPieWedgeData> namedata = new List<BhadraPieWedgeData>();
 

    namedata.add(new BhadraPieWedgeData('Jan', 30));
     
    namedata.add(new BhadraPieWedgeData('Feb', 15));
     
    namedata.add(new BhadraPieWedgeData('Mar', 10));
     
    namedata.add(new BhadraPieWedgeData('Apr', 20));
     
    namedata.add(new BhadraPieWedgeData('May', 20));
     
    namedata.add(new BhadraPieWedgeData('Jun', 5));
     
    return namedata;
 
  }
 
 
 
 
    public class BhadraPieWedgeData {

     
        public String name { get; set; }
     
        public Integer data { get; set; }
     
        public BhadraPieWedgeData(String name1, Integer data1) {
               this.name = name1;          
               this.data = data1;
        }

 
    }

}
=============
Output:
==========

Print page

Tuesday, 28 January 2014

Order of execution in salesforce

When a record is saved in org with an insert, update or upsert statement following process are executed in order:


  1. The original record is loaded from the database (or initialized for an insert statement)
  2. The new record field values are loaded from the request and overwrite the old values
  3. All before triggers execute
  4. System validation occurs, such as verifying that all required fields have a non-null value, and running any user-defined validation rules
  5. The record is saved to the database, but not yet committed
  6. All after triggers execute
  7. Assignment rules execute
  8. Auto-response rules execute
  9. Workflow rules execute
  10. If there are workflow field updates, the record is updated again
  11. If the record was updated with workflow field updates, before and after triggers fire one more time (and only one more time)
  12. Escalation rules execute
  13. All DML operations are committed to the database
  14. Post-commit logic executes, such as sending email

What is difference between SOAP and REST services

SOAP (Simple Object Access Protocol):
SOAP is a method of transferring messages, or small amounts of information, over the Internet. SOAP messages are formatted in XML and are typically sent using HTTP (hypertext transfer protocol).

REST (Representational state transfer):
REST is a simple way of sending and receiving data between client and server and it don't have any much standards defined, we can send and receive data as JSON, XML or even a Text. Its Light weighted compared to SOAP.

SOAP VS REST
SOAP:

  • SOAP builds an XML protocol on top of HTTP or sometimes TCP/IP.
  • SOAP describes functions, and types of data.
  • SOAP is a successor of XML-RPC and is very similar, but describes a standard way to communicate.
  • Binary data that is sent must be encoded first into a format such as base64 encoded.

REST:
  • REST is very lightweight.
  • Typically uses normal HTTP methods instead of a big XML format describing everything. For example to obtain a resource we use HTTP GET, to put a resource on the server we use HTTP PUT. To delete a resource on the server we use HTTP DELETE.
  • REST typically is best used with Resource Oriented Architecture (ROA).
  • Binary data or binary resources can simply be delivered upon their request.

Why we use REST?
Following are few reasons to use REST:

Since REST uses standard HTTP it is much simpler in just about ever way. Creating clients, developing APIs, the documentation is much easier to understand and there aren’t very many things that REST doesn’t do easier/better than SOAP.

REST permits many different data formats where as SOAP only permits XML. While this may seem like it adds complexity to REST because we need to handle multiple formats. JSON usually is a better fit for data and parses much faster. REST allows better support for browser clients due to it’s support for JSON.

REST has better performance and scalability. REST reads can be cached, SOAP based reads cannot be cached.


Why we use SOAP?
Following are few reasons to use SOAP:

WS-Security
While SOAP supports SSL (just like REST) it also supports WS-Security which adds some enterprise security features. Supports identity through intermediaries, not just point to point (SSL). It also provides a standard implementation of data integrity and data privacy.

WS-AtomicTransaction
Need ACID Transactions over a service, you’re going to need SOAP. While REST supports transactions, it isn’t as comprehensive and isn’t ACID compliant. Fortunately ACID transactions almost never make sense over the internet. REST is limited by HTTP itself which can’t provide two-phase commit across distributed transactional resources, but SOAP can.

WS-ReliableMessaging
Rest doesn’t have a standard messaging system and expects clients to deal with communication failures by retrying. SOAP has successful/retry logic built in and provides end-to-end reliability even through SOAP intermediaries.

Difference between Unmanaged Package and Managed Package in Salesforce.com




  • Unmanaged package editable by developer and Installer cannot be upgraded. In managed package certain components are locked and changes are not allowed.
  • Unmanaged package used for 1:1 distributions and managed package used for 1: Many distributions.
  • All editions can create unmanaged package and only developer edition can create managed package.