Saturday, June 29, 2013

Generate JSON in Salesforce

I explore about JSON for generating pie chart in the site.I try this simple one, with hardcoded data.

Requirement:
1)Apex Controller to prepare the data
2)Visual force page to display the data in JSON

1) Apex controller
public class PieController {

     public String jsonString {get;set;}
    
    //Constructor
    public PieController ()
    {
      jsonString = prepareData();

    } 
  
    //Temp Method to prepare the Data
     private String prepareData()
    {
       List<CompanyDetail > dtlObj = new List<CompanyDetail >();
       CompanyDetail c1 =new CompanyDetail('1','Samson',30);
       CompanyDetail c2=new CompanyDetail('2','Targus',35);
       CompanyDetail c3=new CompanyDetail('3','Swan',60);
       CompanyDetail c4=new CompanyDetail('4', 'Filla',30);
       
        dtlObj.add(c1);
        dtlObj.add(c2);
        dtlObj.add(c3);
        dtlObj.add(c4);
            
        return JSON.serialize(dtlObj);
    }
     public class CompanyDetail {
     
       public String Name {get;set;}
       public Decimal share {get;set;}
       public String Id {get;set;}
       
       public CompanyDetail(String id,String Name,Decimal share){
           this.Name=name;
           this.Id=id;
           this.share=share;
       }
     
     }
 }

2) Visual force page
<apex:page contenttype="application/x-JavaScript; charset=utf-8" controller="PieController" showheader="false" sidebar="false" standardstylesheets="false">

  {!jsonString}

</apex:page>
Finally the output,this is how the JSON page will look like

[{"share":30,"Name":"Samson","Id":"1"},{"share":35,"Name":"Targus","Id":"2"},{"share":60,"Name":"Swan","Id":"3"},{"share":30,"Name":"Filla","Id":"4"}]

Friday, June 28, 2013

Using SyntaxHighlighter

Update:Sept 04 2015 (I m using hilite.me)
Notice how cool my code format here.Well I just install SyntaxHighlighter into this blog.

Let see what type of syntax do I get

Define in pre class

1) brush:html


<pre class="brush:html"><!--This is my html sample, in the pre class , I choose html-->
 <pre class="brush:html">My html code in here</pre>
</pre>
2)brush:sql


<pre class="brush:sql">SELECT columnName FROM tableName </pre>


I refer to this :
1)http://mlawire.blogspot.com/2009/07/blogger-syntax-highlighting.html 2)http://alexgorbatchev.com/SyntaxHighlighter/

Have a nice weekend