Sunday, January 29, 2012

Upload .csv file and save it as a PDF file in Document Object

In this article I am going to tell you how to upload a ".csv" file into salesforce,convert it into PDF file and storing that converted PDF file into Document Object. In order to do this task, we need to create following VF pages along with their controllers :
  1. VF page that will take .csv file as input.
  2. VF page that will be rendered as PDF to show the csv file's data.
First of all let us create VF page and its controller that would be rendered as PDF. Lets name that page as "PDFPage.page".

   
      
         {!content}
      
   


PDFPageController :
public with sharing class PDFPageController {
   public String excelData{get;set;}
   public List< String > contentList{get;set;}
 
   public PDFPageController(){
     excelData='';
     excelData=Apexpages.currentPage().getParameters().get('data');
     if(excelData!=''){
        String[] sArray=excelData.split('\n');
        contentList=new List< String >();
 
        for(String str:sArray){
           contentList.add(str);
        }
     }
   }
}
  • Whenever we will reference this page, we will send the excel data as a parameter to page. 
  • So in the constructor, we have to retrieve data that is to be displayed.
  • We cannot directly use this excel data to print its content into the PDF file because if we do so the output in PDF will be like "a,b\nc,d\ne,f\n.....". It does not understand the line break character - "\n" and prints it as it is. So the solution is to split the string based on "\n" and form a List of String.
  • In VF page, the contents are shown using <apex:repeat> tag by iterating on string list.
We are done with the PDF part. Lets proceed by creating VF page for taking csv file as input and preparing its PDF blob.
 
   
     
        
   
 
ExcelDemoController :
 public with sharing class ExcelDemoController {
   public Document document{get;set;}
   public String excelData{get;set;}
   
   public ExcelDemoController()
   {
      document= new Document();
      excelData='';  
   }
 
   public void readExcelData()
   {
      if(document!=null)
      {
         //assign data contained in csv file to String variable
         excelData=document.body.toString();
  
         //PDF page which we created in the first step
         PageReference pdfPage =  Page.PDFPage;

         //set excel data as parameter to PDFPage.
         pdfPage.getParameters().put('data',excelData); 
         pdfPage.setRedirect(true);
   
         // Take the PDF content 
         Blob b = pdfPage.getContent();
 
         //Create Document Object
         document.Body=b;
         document.name='test.pdf';
         document.FolderId=UserInfo.getUserId();

         //save the pdf file
         upsert document;
       }
   }
}
  • The document object will be created with "test.pdf" name. You can change this name with the desired name.
Thats all !!!. Now you are ready to convert a csv file into PDF.

Sunday, January 15, 2012

Displaying Images in a Visual Force Page that are stored in Document/Attachment Object


Many Times you might have ended up into a situation where you want to display an image that is stored either in Document or Attachment object. It is very easy to display an image contained in the static resource but what if you want to use Document or Attachment object instead? This article will help you to show an image into the visual force page.

The image can be displayed in the VF page by using <apex : image> tag. Image source is specified by "URL" attribute.
But the url should be in the form : " /servlet/servlet.FileDownload?file= 01590000000Itdo" where  01590000000Itdo is the the unique id of the Document/Attachment record containing the image.

Example :
I have created a sample Visual Force page along with the custom controller. For this example, I have stored an image in the Document object with name"sample pic". The code in the controller class and Visual Force is self explanatory but still I would explain some important things.

Controller :
public with sharing class ImageController {
 public String imageURL{get;set;}
  
  public ImageController()
  {
    imageURL='/servlet/servlet.FileDownload?file=';
    List< document > documentList=[select name from document where 
                                    Name='SamplePic'];
  
    if(documentList.size()>0)
    {
      imageURL=imageURL+documentList[0].id;
    }
  }
}
  • Here I have taken a String variable declared as public. I am initializing this variable in controller with  "servlet/servlet.FileDownload?file=" value. This variable will be used in VF page as a URL parameter in <apex : image> tag. 
  • The controller fetches the document record from the Document object based on the "name" field specified in the SOQL query.
  • The only thing remaining in the imageURL is the unique id of document record. So after fetching record from the object, append "id" to the imageURL variable. 
Thats it. We are done with the controller class. Now the next step is to design the Visual Force page to display an image.

Visual Force Page :


    
      
    




  • Here we are binding the imageURL ( String variable of controller class ) to the url attribute of <apex : image> tag. 
So finally now our Visual Force page is complete and ready to show an image.

I hope this article would be useful for you to solve your problem to display an image from Document / Attachment Objects.

Sunday, January 1, 2012

Introduction to Salesforce



Today the world is leaded by Cloud computing. Salesforce.com is one of the way for moving all your applications to cloud. There are lots of question running in your mind that what is salesforce? what are its features? what are the benifits of moving apps to salesforce. So to answer your questions, here is a small article which would help you find your answers. I have tried here to answer all these questions in a simple manner.

Salesforce :
Salesforce is a global enterprise software company Best known for its Customer Relationship Management (CRM) .

Customer Relationship Management
  • CRM is a widely implemented strategy for managing a company’s interactions with customers, clients and sales prospects.
  • The overall goals are to find, attract, and win new clients, nurture and retain those the company already has, entice former clients back into the fold, and reduce the costs of marketing and client service.
 Salesforce.com's CRM solution is broken down into several broad categories: 
  • Sales Cloud,
  • Service Cloud
  • Data Cloud  
  • Collaboration Cloud (including Chatter)
  • Custom Cloud (including Force.com).
  The above are the salesforce CRM's broad categories but what do they mean? Lets understand these temrs in details.

The Sales Cloud

  • This application runs in the cloud, so the user can access it anywhere through an Internet-enabled mobile device or a connected computer.
  • The Sales Cloud includes a real-time sales collaborative tool called Chatter. provides sales representatives with a complete customer profile and account history, allows the user to manage marketing campaign spending and performance across a variety of channels from a single application.
  • Automatic email reminders can be scheduled to keep teams up to date on the latest information.

The Service Cloud

  • The Service Cloud provides companies with a call center-like view that enables companies to create and track cases coming in from every channel, and automatically route and escalate what’s important.
  • The Salesforce CRM-powered customer portal provides customers the ability to track their own cases 24 hours a day, includes a social networking plug-in that enables the user to join the conversation about their company on social networking websites, provides analytical tools and other services including email services, chatting tools, Google search, and access to customers' entitlement and contracts.

The Data Cloud

  • The data cloud provides the provision to store large amount of data.
  • Data cloud for Salesforce has the most complete and accurate company and contact data which can be found in one place.
  • Sales and marketing people get instant access to every detail they need.
  • The data is always up-to-date because it’s in the cloud—and it’s only a click away because it’s inside Salesforce.

The Collaboration Cloud 

The collaboration cloud comprises of the powerfull tool chatter which we described in the sales cloud.

Custom Cloud
  • The custom cloud consist of the Force.com platform.
  • It  is Salesforce.com's PaaS product.
  • The platform allows external developers to create add-on applications that integrate into the main salesforce.com application and are hosted on salesforce.com's infrastructure.
These applications are built using Apex (a proprietary Java-like programming language for the Force.com platform) and Visualforce (an XML-like syntax for building user interfaces in HTML, Ajax or Flex).