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

Thursday, April 29, 2010

Visualforce File Upload - for Any SObject


How to Upload Attachment to any SObject using Visualforce

This article explains how you can upload file attachments to any SOobject using Visualforce.


Page:



<apex:page standardController="YourSObjectName" extensions="VFFileUpload">
  <apex:form>
      <apex:pageBlock title="Upload Attachment">
            <apex:inputFile style="width:100%" id="fileToUpload" value="{!fileBody}" filename="{!fileName}" />
            <apex:commandButton value="Upload Attachment" action="{!UploadFile}"/>
       </apex:pageBlock>
  </apex:form>
</apex:page>

Class:



public class VFFileUpload
{
    public Id recId
    {    get;set;    }
    
    public VFFileUpload(ApexPages.StandardController ctlr)
    {
       recId = ctlr.getRecord().Id;     
    }
    
    public string fileName 
    {    get;set;    }
    
    public Blob fileBody 
    {    get;set;    }
  
    public PageReference UploadFile()
    {
        PageReference pr;
        if(fileBody != null && fileName != null)
        {
          Attachment myAttachment  = new Attachment();
          myAttachment.Body = fileBody;
          myAttachment.Name = fileName;
          myAttachment.ParentId = recId;
          insert myAttachment;
           pr = new PageReference('/' + myAttachment.Id);
           pr.setRedirect(true);
           return pr;
        }
        return null;
    }    
}


You can use the above code for any object whether it is standard or custom. Happy coding!

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

Salesforce.com FAQ - Technical Questions

Here are more technical questions, that any new one to salesforce has to learn.

1. What are recursive triggers. How can we avoid the recursion problem
2. What are Apex Governer Limits.
3. What are the Spring'10 features
4. How do you use an actionFunction tag
5. What is the difference between apex:actionFunction and apex:actionSupport tag
6. What is actionPoller
7. How do you do FileUpload using Visualforce
8. What is the difference between a Profile and Role
9. What is appexchange? How can I host my application on appexchange
10. What are the different editions available on salesforce
11. What is batch apex.
12. When will we use batch apex and what is the best practice
13. What are webservice callouts
14. What are wrapper classes
15. When do we use wrapper classes

....More questions to be followed. Stay tuned...

You will get answers for these questions mostly form apex and visualforce documentation provided by salesforce. If you find any real trouble in getting an answer I wil defintely help you out.

'Most of them I will be covering in my blog as articles in the very near future

More General/Customization questions on Salesforce.com

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

Tuesday, April 27, 2010

Chatter or Twitter - Salesforce?


Chatter or Twitter ? :(

Salesforce has come up with another powerful (is it so?) tool called Chatter. It is a social collaboration platform where people can share anything from profile updates to documents. And of course the idea is not new. It has been taken from Twitter mainly and may be from other social networking sites like facebook.

What is the real value that chatter is going to add to your organization or sales people? your sales people can easily collaborate with each other and can track any record literally, What is happening inside the organization, what other sales reps are doing, what records are getting updated etc. This will really add a value when all the users or most of them use it extensively to track their performance against others or organizations performance. Do Sales reps really have that much time to track all of this and analyse themselves. I doubt.

Twitter is not made for professional networking and its not a performance tracking system. What i heard of from lot of tweeple (yes what they call people use twitter is tweeple :)) about why they use twitter is "No one gives a damn here on what are you doing". I really don't think this is the case with chatter.

One more biggest pain point I am seing is chatter is of no use to small scale organizations who hardly have a few sales representatives and really want to concentrate on increasing the business and not chat or tweet with other people in the orgnization.

These are all my personal opinions and What I am concerned about is, there are hell of ideas on idea exchange which have to be addressed first and these people are wasting their time on internal social networking...A surprise from salesforce.com ....... I am a big admirer of Salesforce.com and I can see the value it can bring to CRM breaking through barriers, at the same time I stick to the basics that you concentrate on increasing the business for customers and addressing their pain points. This is the first time I am getting contradictory to a salesforce.com feature.

There's plenty of comment on Twitter from folks attending Dreamforce. A sampling:
  •      "Wonder what Facebook will think of this? Could Facebook go into competition on this, providing 'private' social networks?" From @SaaSEurope, David Bradshaw.
  •      "Salesforce Chatter calling out Sharepoint 2010 - emphasizing bringing in other native apps. Also showing live tweets on SP hahaha." From @JuliaMak.
  •      "Chatter icon looks like dentures. Anyone want some Super Poligrip with that?" From @rwang0, Altimeter Group's Ray Wang.
  •      "Salesforce Dreamforce Keynote outlasts audience bladders, everyone is leaving during Benioff Chatter keynote." From @marksmithvr, Ventana Research's Mark Smith.
  •      Also from Mark Smith: "Salesforce Chatter support all Force.com native apps so that you can link collaboration across Salesforce ecosystem."
Krigsman says Chatter will need three things to succeed: ability to filter noise,accessibility to non-Salesforce customers and great performance.
reference from itbusinessedge
.

Saturday, April 24, 2010

Bulk Triggers in Salesforce


Bulk Enabled Triggers in Salesforce

When you have a need of writing a trigger, it is strongly recommended that you make it bulk enabled, so that the trigger can handle number of records whcih can be inserted/updated/deleted through data loader or any other data migration tool.
I have explained writing a basic trigger here.


I will explain how to bulk enable a trigger now in this article. Consider a scenario where you might want to update the opportunity status to "Closed-Won" depending on a boolean field CloseOpportunities__c on each of the account that has been updated (That is, if you want to close the opportunities related to accounts that are being updated). And you will be uploading hundreds of accounts using a data uploader tool.

Here is the trigger.

 


Trigger myTrigger on Account(after update)
{
   
    Set <Id> setAccId = new Set<Id>();

    for(Account a: Trigger.new)
    {
        if(a.CloseOpportunities__c)
            setAccId.add(a.Id);//set always contains distinct Ids
    }
   
    list <Opportunity> lstOpp = [select Id, StageName from Opportunity where AccountId in : setAccId];
    for(Integer i = 0; i < lstOpp.size(); i++)
    {
        lstOpp[i].StageName = "Closed-Won";
    }
    if(lstOpp.size() > 0)
        update lstOpp;

}




And a more genaralised way is something like this.


tirgger MyTrigger on SObject([your events goes here comma seperated])
{
       Set<Id> SetOfIds = new Set<Id>();

       for(SObject s : trigger.new)
       {
             SetOfIds.add(s.idField);
       }
  

       Map<Id,SObject> objMap = new Map<Id,SObject>([select Id, [Other Fields] from SObject where Id in : SetOfIds]);

       for(SObject s : trigger.new)
       {
              //do your processing with map
       }
}



 
Hope this is useful.


Thanks
Srinivas,
www.trekbin.com