Monday, November 23, 2015

How to have a star rating field on custom object in salesforce


Well, in that case, you'll have to create a separate field, most probably a Picklist Field would suit the need. You can create a Picklist Field say -
Label: RatingCode
Type: Picklist
Values:
1
2
3
4
5
And then create a another Formula Field on the same object. This is to represent the Rating "pictorially". That field would look like this -
Formula Return Type: Text
Label: StarRating
Type: Formula
Formula: 
IF(
    ISBLANK(TEXT(RatingCode__c)),
    NULL,
    IMAGE(
        "/img/samples/stars_" + TEXT(RatingCode__c) + "00.gif",
        "Rating Level"
    )
)

Output:-

Saturday, November 21, 2015

How to encrypt string to Base64 in salesforce

Base64 is often used when you need to encode binary data into characters. Base64 is a good way of taking binary data and turning it into text so that it can easily be transmitted in things like HTML form data and email. Salesforce.com makes it pretty easy to perform Base-64 encoding in Apex via their EncodingUtil class. Below is an Apex code snippet with a very simple example of the base-64 encode/decode.
string before = 'Testing base 64 encode';

// create a blob from our parameter value before we send it as part of the url
Blob beforeblob = Blob.valueOf(before);

// base64 encode the blob that contains our url param value
string paramvalue = EncodingUtil.base64Encode(beforeblob);

// print out the encoded value to the debug log so we can see it before/after base64 encode
System.debug(before + ' is now encoded as: ' + paramvalue);

// take the base64 encoded parameter and create base64 decoded Blob from it
Blob afterblob = EncodingUtil.base64Decode(paramvalue);

// Convert the blob back to a string and print it in the debug log
System.debug(paramvalue + 'is now decoded as: ' + afterblob.toString());

How to show 10,000 records in visualforce page

As the number of records increases, the time required for the browser to render them increases. Paging is used to reduce the amount of data exchanged with the client. Paging is typically handled on the server side (standardsetcontroller). The page sends parameters to the controller, which the controller needs to interpret and then respond with the appropriate data set. Here is the controller which makes use of standard set controller for pagination.
public with sharing class Pagination {
    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}
    public ApexPages.StandardSetController setCon {
        get{
            if(setCon == null){
                size = 10;
                string queryString = 'Select Name, Type, BillingCity, BillingState, BillingCountry from Account order by Name';
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
            }
            return setCon;
        }set;
    }
     
    Public List<account> getAccounts(){
        List <account> accList = new List<account>();
        for(Account a : (List<account>)setCon.getRecords())
            accList.add(a);
        return accList;
    }
     
    public pageReference refresh() {
        setCon = null;
        getAccounts();
        setCon.setPageNumber(1);
        return null;
    }
     
    public Boolean hasNext {
        get {
            return setCon.getHasNext();
        }
        set;
    }
    public Boolean hasPrevious {
        get {
            return setCon.getHasPrevious();
        }
        set;
    }
  
    public Integer pageNumber {
        get {
            return setCon.getPageNumber();
        }
        set;
    }
  
    public void first() {
        setCon.first();
    }
  
    public void last() {
        setCon.last();
    }
  
    public void previous() {
        setCon.previous();
    }
  
    public void next() {
        setCon.next();
    }
}
 
Using the above controller methods we can define the pagination.

    
        
            
                
                
                
                
                
           
            
                
                
                
                 
                {!(pageNumber * size)+1-size}-{!IF((pageNumber * size)>noOfRecords, noOfRecords,(pageNumber * size))} of {!noOfRecords}