Sunday 30 December 2012

Searching the records Based on Date Selection:-

Here we have a scenario ,i.e Create a Date Field in vf page, create one search button 
When we select the date and hit the button. Then it will search all records which is created
on selected date.


<apex:page controller="exampletest">
<apex:form >
       <script>
        function DynamicDatePicker(d_id)
        {
            DatePicker.pickDate(true,d_id.id,false);
        }
        window.onload=function() {
            // prevent autopup of the date inputfield by the default focus behavoir
            document.getElementById('focusDistraction').focus();
        }
        </script>
      <apex:pageBlock >
            <apex:pageMessages > </apex:pageMessages>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Schedule}" value="Selected records"/>
            </apex:pageBlockButtons>
               <b>Date </b><apex:inputText value="{!sdate}" id="time" onfocus="DynamicDatePicker(this);" size="20" disabled="false" style="width:80px;"/>                
         
      <apex:pageBlockSection title="Result" columns="1">
          <apex:pageBlockTable value="{!Schedule}" var="a">
              <apex:column headerValue="Account ID" value="{!a.id}"/>
              <apex:column headerValue="Account Name" value="{!a.name}"/>
          </apex:pageBlockTable>
      </apex:pageBlockSection>
   </apex:pageBlock>
</apex:form>
</apex:page>

Apex class:-
-------------
public class exampletest {
    public Date sdate{get;set;}
    Datetime sdt;
    Datetime edt;
    public string var1{get;set;}
    public string var2{get;set;}
    public List<Account> var;
    public void Schedule(){
             Time t=Time.newInstance(0,0, 0, 0);
             Time t1=Time.newInstance(23,59, 59, 50);
             if(sdate==null)
             {
                 ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'Scheduled Date/Time is Required!!'));
                 return;
             }
             sdt=Datetime.newInstance(sdate,t);
             edt=Datetime.newInstance(sdate,t1);
             System.debug(edt+'=======sddddddddddddddttttt======'+sdt);
            
              var=[select id,name,createddate from account where createddate <=:edt and createddate>=:sdt];
              System.debug('=======sixzeee======'+var.size());
             if(var.size()!=0){
                 for(integer i=0;i<var.size();i++)
                 {
                    System.debug('======nemes ============='+var[i].name);
                    var1=var[i].id;
                    var2=var[i].name;
                 }
             }
        }
        public List<Account> getSchedule(){
            return var;
        }
    
}

Some Issues:-

--> How to call a component from VF page.

    <c:ComponentName/>



How to insert a field(record) into object from Vf Page.

VF page:-
----
<apex:page controller="commentspage" showHeader="false" sidebar="false" showChat="false" standardStylesheets="false" >
  <apex:form > <br/><br/>
        <div align="center">
         <apex:inputTextarea value="{!CommentText}"/><br/><br/>
         <apex:commandButton value="Save" action="{!save}"/>
        </div>
  </apex:form>
</apex:page>

Apex class
--------
public class commentspage {
    public String CommentText{get;set;}
    Comments__c comm=new Comments__c();
     public void save() {
        comm.Comments_Text__c=CommentText;
        insert comm;
    }
}




-->How to Print Radio buttons list in vertical formate.

<apex:selectRadio id="typical" onclick="alert(this.value);" layout="pageDirection"> <br/>
            <apex:selectOption itemValue="0" itemLabel="Increasing Revenue" />
            <apex:selectOption itemValue="1" itemLabel="Reducing Costs" />
            <apex:selectOption itemValue="2" itemLabel="Reducing Time, Effort or Cycles for Employees" />
            <apex:selectOption itemValue="3" itemLabel="Reducing Time, Effort or Cycles for Assets" />
            <apex:selectOption itemValue="4" itemLabel="Reducing RIsk" />
   </apex:selectRadio>

 
Here Layout=”pageDirection” prints in Vertical formate.
Alert(this.value) gives the alert box popup without.
 


รจ  -->How To make the radio buttons by default true.


We have to mention the lable value on constructor. like see below code.

Vf page:-
    <apex:selectRadio value="{!etime}" layout="pagedirection">
                <apex:selectOptions value="{!etimeslot}"/>
    </apex:selectRadio><p/>
Apex class:-
             public with sharing class Test5 {
     public String etime{get;set;}
     public Test5(ApexPages.StandardController controller) {
        etime='0.30';
    }
public List<SelectOption> getetimeslot() {
         List<selectOption> options = new List<selectOption>();
             options.add(new selectOption('0.30','Half-An-Hour'));
             options.add(new selectOption('1.0','One-Hour'));
         return options;
    } 
                }





Dynamic Query and Dynamic class-(12/31/12)


Here we have a scenario -->Create a Textbox(object name) and search button: 
if the value entered in textbox is equal to object name then display that object records
    in a Pageblocktabke below it  else display error.


VF Page:-

<apex:page controller="practice4">
    <apex:form >
        <apex:pageBlock >
            <apex:pagemessages ></apex:pagemessages>
               
            <apex:pageBlockSection title="Object Name">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Enter Object Name"/>
                    <apex:inputText value="{!objname}"/>
                </apex:pageBlockSectionItem>
                <apex:commandButton value="Search" action="{!search}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="List of Records" rendered="{!show}">
                <apex:pageBlockTable value="{!innervar}" var="a">
                    <apex:column headerValue="Record ID" value="{!a.str1}"/>
                    <apex:column headerValue="Record Name" value="{!a.str2}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:-
------

public class practice4 {
    public String objname { get; set; }
    public List<myclass> innervar{get;set;}
    public boolean show{get;set;}
    public practice4 (){
        show=true;
        innervar=new List<myclass>();
    }
    public class myclass{
        public Id str1{get;set;}
        public String str2{get;set;}
        public myclass(ID id,String name){
            str1=id;
            str2=name;
        }
    }
    public void search() {
        try{
            Schema.SObjectType t  = Schema.getGlobalDescribe().get(objname);
            if(t!=null){
                show=true;
                String SOQL = 'SELECT id,Name FROM ' + t;
                for (List<SObject> sobjs : Database.query(SOQL)) {
                    for (SObject sobj : sobjs) {
                        innervar.add(new myclass(sobj.id,String.valueOf(sobj.get('name'))));
                    }
                }
            }
            else{
                show=false;
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,' Please Enter Existing Object Name!'));
            }
        }
        catch(Exception e){
        }
    }
}