Sunday 29 June 2014

Serialize apex class to JSON salesforce

Developing an JSON output from apex class is always a challenging task for new comers. So, I have decided to post a simple apex class which generates JSON request. Below is the code snippet of class along with test class. You can extend this as per your requirement

Apex class
public class JSONserialize{
//constructor
public JSONserialize(){
Contact con = new contact();
JSONSerialize.JSONinner handler = new JSONSerialize.JSONinner();// creating instance for JSONinner class
// Set additional field
    handler.name = 'JSONinner name';
    handler.type = 'Example';
    handler.c = new JSONserialize.contact1();// creating instance for contact1 object in JSONinner class
    handler.c.firstname= 'Type a'; 
    handler.c.lastname ='Smith';
    String jsonstring = Json.serialize(handler); //this method serializes the provided inputs
    System.debug('my output :::::::::::'+jsonstring); 
    System.assert(jsonstring.contains('Type a') == true);
    }
    // belpw are two inner classes to hold the variables needed in generating JSON request
public class JSONinner{
public string name{get;set;}
public string type{get;set;}
public contact1 c{get;set;}//the line here relates JSONinner class with contact1 class.. 
 }                           //That means i can access contact1 variables from JSONinner class

public class contact1{
public string firstname{set;get;}
public string lastname{set;get;}
}
}
Test Class
@istest
private class testJSONserialize{
    static testMethod void main(){
        JSONserialize o = new JSONserialize();
    }
}
Result
{
"type" : "Example",
"name" : "JSONinner name",
  "c" : {
       "lastname" : "Smith",
       "firstname" : "Type a"
   }
}

Friday 27 June 2014

Radio Buttons Usage in Salesforce Passing only selected row values


 <apex:page controller="radioButtons" showHeader="false">  
 <Apex:form >  
 <script>  
 function myFunction( val){  
 var x = val.value;  
 callfunc(x);  
 }  
 </script>  
  <apex:pageBlock >  
  <apex:actionFunction name="callfunc" action="{!addaction}" reRender="text">  
  <apex:param value="" name="number"/>  
  </apex:actionFunction>  
 <apex:pageBlockTable value="{!rows}" var="aa">  
 <apex:column >  
  <apex:selectRadio value="{!SelectedValue}" onclick="myFunction(this);" >  
   <apex:selectOptions value="{!items}" />  
  </apex:selectRadio>  
 </apex:column>  
 </apex:pageBlockTable>  
  </apex:pageBlock>  
  <Apex:pageblock >  
  <font size="4">Result : <apex:outputText id="text" value="{!num}"> </apex:outputText></font>  
  </Apex:pageblock>  
 </Apex:form>  
 </apex:page>  
Apex Controller
 public class radioButtons {  
 public string SelectedValue{set;get;}  
 //integer to store the Result  
 public integer num{set;get;}  
 //list which stores the size of pageblock table to iterates radio buttons based on list size  
  public List<row> rows { get; set; }  
   //constructor  
   public radioButtons(){  
   num = 0;  
    rows = new List<Row>();  
    for(integer i=0;i<=4;i++){  
     rows.add(new row('5', '5'));  
     }  
   }  
   //method which adds the selected value to num integer  
   public void addaction(){  
   string number1 = system.currentPageReference().getParameters().get('number');  
   system.debug('Number::::::::::'+number1);  
   num = num+integer.valueof(number1);  
   }  
   //method which return the values and labels into apex select options  
    public List<SelectOption> getItems() {  
     List<SelectOption> options = new List<SelectOption>();  
     options.add(new SelectOption('5','5'));  
     options.add(new SelectOption('10','10'));  
     return options;  
   }  
   //inner class  
    public class Row {  
     public String Value { get; set; }  
     public String Label { get; set; }  
     public Boolean isChecked { get; set; }  
     public Row(String Value, String Label) {  
       this.Value = Value;  
       this.Label = Label;  
     }   
   }  
 }  
Screen Shot