Author Archives: Biswajeet

About Biswajeet

Biswajeet is my Name, Success is my Aim and Challenge is my Game. Risk & Riding is my Passion and Hard Work is my Occupation. Love is my Friend, Perfection is my Habit and Smartness is my Style. Smiling is my Hobby, Politeness is my Policy and Confidence is my Power.

Links in Visualforce Page pageblocktables

<apex:pageblocktable value="{!FindContact}" var="item">
<apex:column headervalue="Name">
<apex:outputlink value="/{!item.id}">
<apex:outputtext value="{!item.Name}"></apex:outputtext>
</apex:outputlink>
</apex:column>
<apex:column headervalue="Email" value="{!item.Email}"></apex:column>
<apex:column headervalue="Phone" value="{!item.Phone}"></apex:column>
<apex:column headervalue="Birthdate" value="{!item.Birthdate}"></apex:column>
</apex:pageblocktable>

Data Import From CSV Using Visualforce Page

Sometimes we need to read a CSV file in a Visualforce Page. Here is an example to read a CSV file and display it in Visualforce Page. Below example reads a CSV file having Account records in it and displays them in a table.

CSV file format used in this example:

Controller:

Public with sharing class Sample {
    
    public Blob csvFileBody {get;set;}
    Public string csvAsString {get;set;}
    Public String[] csvFileLines {get;set;}
    Public String[] inputValues {get;set;}
    Public List<string> fieldList {get;set;}
    Public List<account> sObjectList {get;set;}
    
    public Sample(){
        csvFileLines = new String[]{};
            fieldList = New List<string>();
        sObjectList = New List<sObject>(); 
    }
    
    Public void readcsvFile(){
        csvAsString = csvFileBody.toString();
        csvFileLines = csvAsString.split('\n');
        inputValues = new String[]{};
            
            for(string st:csvfilelines[0].split(',')){
                fieldList.add(st); 
            }
        
        for(Integer i=1; i<csvfilelines.size(); i++){
            Account accRec = new Account() ;
            string[] csvRecordData = csvFileLines[i].split(',');
            accRec.Name = csvRecordData[0] ;             
            accRec.AccountNumber = csvRecordData[1];
            accRec.Type = csvRecordData[2];
            accRec.Industry = csvRecordData[3];                                                                               
            sObjectList.add(accRec);   
        }
    }
}

Visualforce Page:

<apex:page controller="Sample">
    <apex:form>
        <apex:pageBlock>
            <apex:panelGrid columns="2"> 
                <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
                <apex:commandButton value="Read CSV" action="{!readcsvFile}"/>
            </apex:panelGrid>
        </apex:pageBlock>
        <apex:pageBlock>
            <apex:pageblocktable value="{!sObjectList}" var="rec">
                <apex:column value="{!rec.Name}" />
                <apex:column value="{!rec.AccountNumber}"/>
                <apex:column value="{!rec.Type}" />
                <apex:column value="{!rec.Industry}" />
            </apex:pageblocktable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output:

Conditionally Render Textbox on Selection of a Radio Button in Visualforce Page

Controller:

public class Sample{
    
    public String selectedOption { get; set; }
    
    public Sample()
    {
        selectedOption = 'No';
    }
}

Visualforce Page:

<apex:page controller="Sample">
    <apex:form id="form">
        <apex:selectRadio value="{!selectedOption}">
            <apex:actionSupport event="onchange" rerender="form" />
            <apex:selectOption itemValue="Yes" itemLabel="Yes" />
            <apex:selectOption itemValue="No" itemLabel="No" />
        </apex:selectRadio>
        <apex:inputText rendered="{!selectedOption='Yes'}" />
    </apex:form>
</apex:page>

Output:

JSON Response Parsing in Salesforce Apex

We can use the JSONParser class methods to parse JSON-encoded content. The methods of JSONParser class enable to parse a JSON-formatted response that’s returned from a call to an external service, such as a web service callout.

Here is an example of JSON response parsing using apex.

JSON String Data:

{
"ContactList": [
		{
            "FirstName":"Biswajeet",
            "LastName": "Samal",
			"Email": "test1@test.com",
			"Mobile": "9999999999"
        },
        {
            "FirstName":"Abhijeet",
            "LastName": "Samal",
			"Email": "test2@test.com",
			"Mobile": "8888888888"
        }
    ]
}

Apex Class:

//Wrapper Class For Parsering 
public class JsonParseringClass
{
    //Method To Parse JSON Data
    public ContactList getJSONData()
    {
        ContactList conList = new ContactList();
        //JSON String
        String jsonString = '{"ContactList": [' +
            '{"FirstName":"Biswajeet", "LastName": "Samal", "Email": "test1@test.com", "Mobile": "9999999999"},'+
            '{"FirstName":"Abhijeet", "LastName": "Samal", "Email": "test2@test.com",	"Mobile": "8888888888"}]}';
        //Parse JSON to ContactList
        conList = (ContactList)System.JSON.deserialize(jsonstring, ContactList.class);
        System.debug('Respone- ' + conList);
        return conList;
    }
    
    public class ContactList
    {
        public List<ContactWrapper> ContactList;
    }
    
    public class ContactWrapper
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Mobile { get; set; }
    }
}

Debug Log:

Respone- ContactList:[ContactList=(ContactWrapper:[Email=test1@test.com, FirstName=Biswajeet, LastName=Samal, Mobile=9999999999], ContactWrapper:[Email=test2@test.com, FirstName=Abhijeet, LastName=Samal, Mobile=8888888888])]