Tuesday, 9 August 2016

Wrapper Class In Salesforce


A wrapper class is a class,data structure whose instance are a collections of other objects. It is a custom object defined by Salesforce developer, where he defines the properties of the wrapper class. Within Apex and Visualforce this can be extremely helpful to achieve many business scenarious within the Salesforce.

Here is a most commonly asked scenario, how we can display data within a table with checkbox and then process only the record which are selected:


Apex Code:
public class WrapperController
{
  Public list<wrapperaccount> acctlist;
    
    public List<wrapperaccount> getAccount()
    {
        if(acctlist==null)
        {
          acctlist=new List<wrapperaccount>();
          list<account> aa=[Select name from account];
          for(Integer i=0;i<aa.size();i++)
          {
           acctlist.add(new wrapperaccount(aa[i]));
          }
        }
        return acctlist;
    }
    public pageReference deleteaccount()
    {
        List<Account> selectedaccount=new List<Account>();
        for(wrapperaccount aa:getAccount())
        {
            if(aa.checkbox==true)
            {
                selectedaccount.add(aa.act);
            }
        }
        for(account act:selectedaccount)
        {
            delete act;
        }
        acctList=null;
        return null;
    } 
   public class wrapperaccount
    {
        public account act{get;set;}
        public boolean checkbox{get;set;}
        public wrapperaccount(account a)
        {
            act=a;
            checkbox=false;
        }
    }
}
Visualforce page Code:
<apex:page controller="WrapperController">
<apex:form >
    <apex:pageblock >
    <apex:pageblockButtons >
    <apex:commandButton value="Delete" action="{!deleteaccount}" />
    </apex:pageblockButtons>
    
    <apex:pageblocktable value="{!Account}" var="a">
    <apex:column >
    <apex:inputCheckbox value="{!a.checkbox}"/>
    </apex:column>
    <apex:column value="{!a.act.Name}"/>
    
    </apex:pageblocktable> 
    </apex:pageblock>
</apex:form>
</apex:page>

Thank you friends...
I hope it will help you. If you have any query, you can ask me anytime.
So keep reading and love technology..:)

Thursday, 4 August 2016

How to create Dynamic SOQL in salesforce

Dynamic SOQL means creation of  query at run-time using apex and get records after executing that query in your code. We can create dynamic query with the help of apex code easily.

Here is an example in which I will show you how to create a Dynamic Query with apex code:



Apex Code:

public class DynamicApex 
{
    public map<String,Schema.SobjectType> ofmap=Schema.getGlobalDescribe();
    public string selectedObject{get;set;}
    public list<string> selectedfield{get;set;}
    public string temp{get;set;}
    public string temp1{get;set;}
    public list<sobject> q{get;set;}
    
    public DynamicApex ()
    {
        selectedObject='none';
        q=new list<sobject>();
        selectedfield=new list<string>();
    }
    
    public List<selectoption> getobject()
    {
        list<selectoption> objectname=new list<selectoption>();
        list<string> mapkey=new list<string>(ofmap.keyset());
        objectname.add(new selectoption('None','None'));
        mapkey.sort();
        for(String s:mapkey)
        {
            objectname.add(new selectoption(s,s));
        }
        return objectname; 
    }
    
    public List<selectoption> getfields()
    {
        list<selectoption> field=new list<selectoption>();
        if(selectedObject!='none')
        {
            schema.sobjectType objtype=ofmap.get(selectedobject);
            map<String,Schema.sobjectField> fieldmap=objtype.getDescribe().fields.getmap();
            for(String F:Fieldmap.KeySet())
            {
                field.add(new selectoption(F,F));
            }
        }
        return field;
    }
    
    public void Soql()
    {       
          temp='select ';
          for(String field:Selectedfield)
          {
              if(Field!=Selectedfield.get(Selectedfield.size()-1))
              {
                  temp+= field+','+' ';
              }
              else
              {
                  temp+= field+' ';
              }
          }
          
          temp+='from '+ SelectedObject;
          q= Database.query(temp) ;
    }
}

Visualforce page Code:

<apex:page controller="DynamicApex">
<apex:form >
<apex:pageblock >
    
    <apex:pageblockButtons location="Top">
    <apex:commandButton value="Click" action="{!soql}"/>
    </apex:pageblockButtons>

    <apex:pageblockSection columns="4">
   
    <apex:pageblocksectionItem >
        <apex:outputLabel value="Object:"/>
            <apex:selectList value="{!selectedobject}" size="1">
                <apex:selectOptions value="{!object}"/>
                    <apex:actionSupport event="onchange" reRender="myfields"/>
            </apex:selectList>
      </apex:pageblocksectionItem>
     
     <apex:pageblocksectionItem >
         <apex:outputLabel value="Fields:"/>
            <apex:outputPanel id="myfields">
                <apex:selectList value="{!selectedField}" multiselect="true" size="5">
                    <apex:selectOptions value="{!fields}"/>
                </apex:selectList>
            </apex:outputPanel>
        </apex:pageblocksectionItem>
       
        <apex:outputLabel value="Dynamic Query:"/>
        <apex:inputtextarea rows="5" cols="50" value="{!temp}" />
    </apex:pageblockSection>
    
        <apex:pageblockTable value="{!q}" var="t" >
            <apex:repeat value="{!selectedField}" var="sf">
                <apex:column value="{!t[sf]}"/>
            </apex:repeat>
        </apex:pageblockTable>
        
        <apex:outputPanel rendered="{!IF(q.size < 1 , true , false)}">
           <apex:pageMessage severity="ERROR" summary="No records to display"/>
        </apex:outputPanel>
        

</apex:pageblock>
</apex:form>
</apex:page>

In this example after click on button you can create dynamic query and get the records in table of that query as shown above in the image.


Wednesday, 3 August 2016

How to use FieldSet in Visualforce

Fieldset is a group of fields, you can use fieldset in Visualforce page to display those fields on page. When you are using fieldset in Visualforce to display fields, then you can add,remove & reorder fields very easily. When you are doing work with fieldset then you no need to code modifications to the page.

How to create Fieldset:

To create a Fieldset go to setup and search the object on which you want to create fieldset.



Click on fieldset and create a new fieldset, after that select all the fields which you want to include in your fieldset and then click on save button.
Now you can use your fieldset on your visualforce page and whenever you want to change the order of the fields and want to add or remove any fields on your page you can do that easily from your fieldset for this no need of coding.



Here is the Visualforce page coding which in which we are using Fieldset:

<apex:page standardController="Account">
<apex:pageblock >
<apex:form >
<apex:pageblockSection >
<apex:repeat value="{!$objectType.Account.Fieldsets.F1}" var="f">
<apex:inputField value="{!Account[f]}" />
</apex:repeat>
</apex:pageblockSection>
</apex:form>
    </apex:pageblock>
</apex:page>

With the help of this coding we can get this result: