Tuesday, May 11, 2010

Interfaces for Apex Class - Salesforce

Implementing Interfaces for Apex Classes

An interface is like a class in which none of the methods have been implemented, the method signatures are there, but the body of each method is empty. To use an interface, another class must implement it by providing a body for all of the methods contained in the interface.

public class InterfaceClass
{
    //Lets consider a Bank trnasaction interface which indeed used by 3 banks BankA, BankB and BankC
    public Interface bankTransactionInterface
    {
        double deposit();
        double withdrawal();
   }
    
    //We have to implement the two methods declared in the interface for BankA
    public class BankA implements bankTransactionInterface
    {
        public double deposit()
        {
            //process the deposit
            double depositedAmount = 250;
            return depositedAmount ;
        }
        
        public double withdrawal()
        {
            //process the withdrawal
            double withdrawalAmount = 350;
            return withdrawalAmount ;
        }
       
    }
    
    //We will take another class for BankB and declare it as virtual as it is parent of BankC which has different deposit porcess but same withdrawal process as BankB.
    //For this we have to declare the deposit method as virtual and use override keyword when overriding it for BankC like showed below
    public virtual class BankB implements bankTransactionInterface
    {
        public virtual double deposit()
        {
            //process the deposit
            double depositedAmount = 450;
            return depositedAmount ;
        }
        
        public double withdrawal()
        {
            //process the withdrawal
            double withdrawalAmount = 1000;
            return withdrawalAmount ;
        }
    }
    
    public class BankC extends BankB 
    {
        public override double deposit()
        {
            //process the deposit
            double depositedAmount = 750;
            return depositedAmount ;
        }
    }
} 
'Hope this is helpful 

Thursday, May 6, 2010

Salesforce.com Customization Questions

Salesforce.com General/Customization Questions

As I am getting requests from people to post more questions on customization also. I am posting here questions in a more general way.
With this you will get a good idea on basics of salesforce.com usage.
1. Explain how MVC architechture fit for Salesforce
2. How will you create relationships between objects
3. How many types of relationships are possible on objects
4. How many data types are supported for a Custom Object Standard Field Name
5. What are activities
6. What is the difference between Task and Event
7. List and describe the features used to set permission and data access in a custom app.
8. How will you create a User
9. What are the available editions of salesforce.
10. What is the difference between Enterprise/Professional/Unlimited/force.com/Developer editions.
11. What are Sharing Settings
12. What are Person Accounts
13. How forecasting works in salesforce
14. What are the system fields. Can you name some of them
15. What are the default components available on home page?
16. How do I change the home page layout?
17. How many types of Reports I can create in salesforce. what are they
18. What is dashboard. How it is created
19. What is Page Layout
20. What is the Related List
21. What is the difference between Page Layout and Related List
22. What is mini page lay out

As I am in hurry, I will update this post with real taste of customization questions as soon as I can.

Hope this is helpful.

Thanks
Srinivas,
Technology Evangelist,
Trekbin Technologies,
www.trekbin.com

Wednesday, May 5, 2010

Visualforce Email Template with Attachment

Visualforce Email Templates with Component as Attachment

Now Email templates can be created through a visualforce. That is you can send emails to people with salesforce features.

I will explain a Visualforce email template basic/as well as with component as attachement here.

<messaging:emailTemplate recipientType="Contact" relatedToType="Account" subject="opportunity report for Account : {!relatedTo.name}">
<messaging:htmlEmailBody >
<html>
<body>
<table >
    <tr>
        <th> Name </th>
        <th> CloseDate </th>
        <th> Stage Name </th>
    </tr>
    <apex:repeat var="o" value="{!relatedTo.Opportunities}">
        <tr>
           <td>{!o.Name}</td>
           <td>{!o.CloseDate}</td>
           <td>{!o.StageName}</td>
        </tr>
    </apex:repeat>    
</table>
   
</body>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
The above Visualforce Template sends out all the opportunities that are related to the Contact's Account.

The one more powerful usage of Visualforce Email Template is We can introduce Custom Components in the body.

I would like to attach a PDF document which shows the number of Opporutnities for that account grouping by their close date. This I can do in a component and include that component inside my email template like this after htmlEmailBody tag.
<messaging:attachment renderAs="PDF">
    <c:opportunityGrouping accountId="{!relatedTo.Id}"/>
</messaging:attachment>
component:
<apex:component controller="opportunityGroupingController" access="global">
    <apex:attribute name="accountId" assignTo="{!accId}" type="String" description="Id of the account"/>
    <table >
            <tr>
                <th> Total </th>
                <th> CloseDate </th>
            </tr>
        <apex:repeat var="opp" value="{!GroupedOpportunites}">
            <tr>
                <td>{!opp.Total}</td>
                <td>{!opp.CloseDate}</td>
            </tr>
        </apex:repeat>    
    </table>
</apex:component>
opportunityGroupingController Class:
public class opportunityGroupingController 
{
    public String accId
    {   get;set;    }
   
    public list<AggregateResult> lstAR = new list<AggregateResult>();
   
    public list<subClass> lstSC = new list<subClass>(); 

    public class subClass
    {
        public Integer Total
        {   get;set;    }

        public date closeDate
        {   get;set;    }
       
        public subClass(AggregateResult ar)
        {
            Total = (Integer)ar.get('Total');
            closeDate = (date)ar.get('CloseDate');              
        }
    }

    public list<subClass> getGroupedOpportunites()
    {  
        lstAR = [select count(Id) Total, CloseDate from Opportunity where AccountId =:accId Group By CloseDate];
        for(Integer i = 0; i < lstAR.size(); i++)
        {
            subClass objSubClass = new subClass(lstAR[i]);
            lstSC.add(objSubClass);
        }
        return lstSC;  
    }
}

Hope this is useful.

Thanks
Srinivas
Technology Evangelist
Trekbin Technologies