Saturday, 21 June 2014

Searching for a Record using Enter Key from keyboard

Searching for a Record after text type in Text box and press enter 

Page code:
-------------

<apex:page controller="SearchController">
  <apex:form >
  
<script type='text/javascript'> 
function runOnEnter(ev) { 
if (window.event && window.event.keyCode == 13 || ev.which == 13) { 
searchAccRecs(); 
return false; 
} else { 
return true; 
} 
} 
</script> 
  
    <apex:pageblock >
        <apex:inputtext value="{!inputtext}"/>
        <apex:commandbutton value=" Search " action="{!searchRecords}"/>
    </apex:pageblock>

    <apex:pageblock rendered="{!resultblock}">
      <apex:pageblocktable value="{!accList}" var="acc">
        <apex:column value="{!acc.name}"/>
        <apex:column value="{!acc.accountnumber}"/>
      </apex:pageblocktable>
    </apex:pageblock>
  </apex:form>
</apex:page>


Class code:
--------------
public class SearchController {

  public String inputtext { get; set; }
  
  public boolean resultblock { get; set; }
  
  Public List<account> accList{get;set;}
  
  Public SearchController(){
     resultblock = false;
    }    

   public void searchRecords() {
      resultblock = true;
      accList = database.Query('select name,accountnumber from account         where name like '+'\''+'%'+inputtext+'%'+'\'');
    }
}

Multiple Attachments of your choice to particular Record

Upload multiple attachments of your choice value selected from picklist 


Here you can upload attachments to particular account, pass record id from URL and uplaod the attachments:
https://ap1.salesforce.com/0019000000tMrl6

Page code:
-------------

<apex:page standardController="Account" extensions="MultipleUploadController">
  <apex:form >
   <apex:pageBlock title="Upload Multiple Attachment to Object">
     <apex:pageBlockButtons >
      <apex:commandButton value="Upload" action="{!SaveAttachments}"/>
     </apex:pageBlockButtons>
     
     <apex:pagemessages id="MSG"/>
     <apex:actionFunction name="ChangeCount" action="{!ChangeCount}"/> 
     
     <apex:pageBlockSection >
      <apex:pageBlockSectionItem >
       <apex:outputlabel value="How many files you want to upload?"/>
         
       <apex:selectList onchange="ChangeCount() ;" multiselect="false" size="1" value="{!FileCount}">
                        <apex:selectOption itemLabel="--None--" itemValue=""/>
                        <apex:selectOptions value="{!filesCountList}"/>
                    </apex:selectList>
      </apex:pageBlockSectionItem>
     </apex:pageBlockSection>
     
     <apex:pageBlockSection title="Select Files" rendered="{!IF(FileCount != null && FileCount != '', true , false)}">
                <apex:repeat value="{!allFileList}" var="AFL">
                 <apex:inputfile value="{!AFL.body}" filename="{!AFL.Name}" />
                </apex:repeat>
            </apex:pageBlockSection>
   </apex:pageBlock>
  </apex:form>

</apex:page>

Controller code:
------------------

public with sharing class MultipleUploadController {

     //Picklist of tnteger values to hold file count
    public List<SelectOption> filesCountList {get; set;}
    //Selected count
    public String FileCount {get; set;}
    
    public List<Attachment> allFileList {get; set;}
     
    public MultipleUploadController(ApexPages.StandardController controller) {
           filesCountList = new List<SelectOption>() ;
        FileCount = '' ;
        allFileList = new List<Attachment>() ;
        
        //Adding values count list - you can change this according to your need
        for(Integer i = 1 ; i < 11 ; i++)
            filesCountList.add(new SelectOption(''+i , ''+i)) ;
    }
    public Pagereference SaveAttachments()
    {
        String accId = System.currentPagereference().getParameters().get('id');
        if(accId == null || accId == '')
            ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'No record is associated. Please pass record Id in parameter.'));
        if(FileCount == null || FileCount == '')
            ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'Please select how many files you want to upload.'));

        List<Attachment> listToInsert = new List<Attachment>() ;
        
        //Attachment a = new Attachment(parentId = accid, name=myfile.name, body = myfile.body);
        for(Attachment a: allFileList)
        {
            if(a.name != '' && a.name != '' && a.body != null)
                listToInsert.add(new Attachment(parentId = '0019000000tiPjM', name = a.name, body = a.body)) ;
                
                //(OR) listToInsert.add(new Attachment(parentId = accId, name = a.name, body = a.body)) ;
        }
        
        //Inserting attachments
        if(listToInsert.size() > 0)
        {
            insert listToInsert ;
            ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.INFO, listToInsert.size() + ' file(s) are uploaded successfully'));
            FileCount = '' ;
        }
        else
            ApexPages.addmessage(new ApexPages.message(ApexPages.Severity.ERROR,'Please select at-least one file'));
            
        return null;
    }
    public PageReference ChangeCount()
    {
        allFileList.clear() ;
        //Adding multiple attachments instance
        for(Integer i = 1 ; i <= Integer.valueOf(FileCount) ; i++)
            allFileList.add(new Attachment()) ;
        return null ;
    }

}

Standard Gridview in Visualforce page using Inputhidden

Accessing Accounts by selecting like A|B|C|D|E|F|I -- |ALL




Page code:
-------------

<apex:page controller="FilteringRecords" id="page1" sidebar="false">
  
 <script>
          function filterRecord(val){
          document.getElementById('page1:form1:hdn').value=val;
          }
          
           function filterRecord2(val){
            document.getElementById('page1:form1:hdn2').value=val;
      }
  </script>  
  
  <apex:form id="form1">

       <apex:commandLink value="A" onclick="filterRecord('A')" action="{!showVal}" rerender="out"/> | &nbsp;
      <apex:commandLink value="B" onclick="filterRecord('B')" action="{!showVal}" rerender="out"/> | &nbsp;
      <apex:commandLink value="C" onclick="filterRecord('C')" action="{!showVal}" rerender="out"/> | &nbsp;
      <apex:commandLink value="D" onclick="filterRecord('D')" action="{!showVal}" rerender="out"/> | &nbsp;
      <apex:commandLink value="E" onclick="filterRecord('E')" action="{!showVal}" rerender="out"/> | &nbsp;
      <apex:commandLink value="F" onclick="filterRecord('F')" action="{!showVal}" rerender="out"/> | &nbsp;
       <apex:commandLink value="G" onclick="filterRecord('G')" action="{!showVal}" rerender="out"/> | &nbsp;
        <apex:commandLink value="H" onclick="filterRecord('H')" action="{!showVal}" rerender="out"/> | &nbsp;
         <apex:commandLink value="I" onclick="filterRecord('I')" action="{!showVal}" rerender="out"/> | &nbsp;
          <apex:commandLink value="J" onclick="filterRecord('J')" action="{!showVal}" rerender="out"/> | &nbsp;
           <apex:commandLink value="K" onclick="filterRecord('K')" action="{!showVal}" rerender="out"/> | &nbsp;
            <apex:commandLink value="L" onclick="filterRecord('L')" action="{!showVal}" rerender="out"/> | &nbsp;
             <apex:commandLink value="M" onclick="filterRecord('M')" action="{!showVal}" rerender="out"/> | &nbsp;
              <apex:commandLink value="N" onclick="filterRecord('N')" action="{!showVal}" rerender="out"/> | &nbsp;
               <apex:commandLink value="O" onclick="filterRecord('O')" action="{!showVal}" rerender="out"/> | &nbsp;
                <apex:commandLink value="P" onclick="filterRecord('P')" action="{!showVal}" rerender="out"/> | &nbsp;
                 <apex:commandLink value="Q" onclick="filterRecord('Q')" action="{!showVal}" rerender="out"/> | &nbsp;
                  <apex:commandLink value="R" onclick="filterRecord('R')" action="{!showVal}" rerender="out"/> | &nbsp;
                   <apex:commandLink value="S" onclick="filterRecord('S')" action="{!showVal}" rerender="out"/> | &nbsp;
                    <apex:commandLink value="T" onclick="filterRecord('T')" action="{!showVal}" rerender="out"/> | &nbsp;
                     <apex:commandLink value="U" onclick="filterRecord('U')" action="{!showVal}" rerender="out"/> | &nbsp;
                      <apex:commandLink value="V" onclick="filterRecord('V')" action="{!showVal}" rerender="out"/> | &nbsp;
                       <apex:commandLink value="W" onclick="filterRecord('W')" action="{!showVal}" rerender="out"/> | &nbsp;
                        <apex:commandLink value="X" onclick="filterRecord('X')" action="{!showVal}" rerender="out"/> | &nbsp;
                         <apex:commandLink value="Y" onclick="filterRecord('Y')" action="{!showVal}" rerender="out"/> | &nbsp;
                          <apex:commandLink value="Z" onclick="filterRecord('Z')" action="{!showVal}" rerender="out"/> | &nbsp;
          
      <apex:commandLink value="ALL"  onclick="filterRecord('ALL')" action="{!showVal}" rerender="out" /> &nbsp;                  
      <apex:inputhidden value="{!hiddenVal}" id="hdn"/>
                  
       <apex:pageBlock id="out">
       <apex:pageBlockSection >
       <apex:pageBlockTable value="{!records}" var="r">
                  <apex:column headervalue="Account Name" value="{!r.Name}"/>
                  <apex:column headervalue="Account Phone" value="{!r.Phone}"/>
                  <apex:column headervalue="Industry" value="{!r.Industry}"/>
                  <apex:column headervalue="Rating" value="{!r.Rating}"/>
                  <apex:column headervalue="Website" value="{!r.Website}"/>
       </apex:pageBlockTable>
       </apex:pageBlockSection>
       </apex:pageBlock>
  </apex:form>

</apex:page>


Controller Code:
-------------------

public with sharing class FilteringRecords {

      public String hiddenVal { get; set; }
      
    public List<Account> lstDL = new List<Account>();
    String sQuery='';
    
    public integer count {get;set;}
    public List<Account> getRecords() {
    if(count==1){
    sQuery = 'select Name,Phone,Industry,Rating,Website from Account where Name LIKE ' + '\'' + hiddenVal + '%\'';
    system.debug('****Dynamic SOQL Query is ****'+sQuery);
    lstDL = database.query(sQuery); 
    }
    else{
        sQuery='select Name,Phone,Industry,Rating,Website from Account';
        system.debug('****Dynamic SOQL Query is ****'+sQuery);
          lstDL = database.query(sQuery);  
       
        }
     
    return lstDL;
    }

       public PageReference showVal() {
       system.debug('***Clicked Value is****'+hiddenVal);
        count = hiddenVal.length();
          system.debug('-------------------------------'+count); 
        return null;
    }
                
   

}

Unlimited Years For Standard Calender in Visualforce Page

Using this tip we can access the Unlimited Years of Calender using simple JQuery


Page:
------
<apex:page standardController="Contact" id="mypage">

  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>

<span id="hideThisHomePageComp"></span>

  <script type="text/javascript">
  $j = jQuery.noConflict();
   $j(document).ready(function() {
                     var startYear=1900;              
                     var endYear=2024;              
                     var htmlStr='';            
                     if(startYear<endYear){
                         for(i=startYear;i<endYear+1;i++){
                            htmlStr += "<option value=\""+i+"\">"+i+"</option>";
                                 }
                    $j('#calYearPicker').html(htmlStr);
                  }
   $j('#sidebarDiv #hideThisHomePageComp').parent().parent().hide();
                  }  );
  </script>

    <apex:form>
        Date: <apex:inputField value="{!contact.birthdate}" id="demo" onfocus="DatePicker.pickDate(false, this , false);"/>  <br/>
   
   
    </apex:form>
</apex:page>






Friday, 20 June 2014

Display Google Maps in VF-Page

Using the below page code we can display Googlemap for associated Account Address

Page:
-------

<apex:page standardController="Account">
<head>
<script type="text/javascript"
src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" 
src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() {

  var myOptions = {
    zoom: 15,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false
  }

  var map;
  var marker;

  var geocoder = new google.maps.Geocoder();
  var address = "{!Account.BillingStreet},{!Account.BillingCity}, {!Account.BillingPostalCode}, {!Account.BillingCountry}";

  var infowindow = new google.maps.InfoWindow({
    content: "<b>{!Account.Name}</b><br>{!Account.BillingStreet}<br>{!Account.BillingCity}, {!Account.BillingPostalCode}<br>{!Account.BillingCountry}"
  });

  geocoder.geocode( { address: address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK && results.length) {
      if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {

        //create map
        map = new google.maps.Map(document.getElementById("map"), myOptions);

        //center map
        map.setCenter(results[0].geometry.location);

        //create marker
        marker = new google.maps.Marker({
            position: results[0].geometry.location,
            map: map,
            title: "{!Account.Name}"
        });

        //add listeners
        google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(map,marker);
        });
        google.maps.event.addListener(infowindow, 'closeclick', function() {
          map.setCenter(marker.getPosition()); 
        });

      }

    } else {
      $('#map').css({'height' : '15px'});
      $('#map').html("Oops! {!Account.Name}'s billing address could not be found,please make sure the address is correct.");
      resizeIframe();
    }
  });

  function resizeIframe() {
    var me = window.name;
    if (me) {
      var iframes = parent.document.getElementsByName(me);
      if (iframes && iframes.length == 1) {
        height = document.body.offsetHeight;
        iframes[0].style.height = height + "px";
      }
    }
  }

});
</script>
<style>
#map {
  font-family: Arial;
  font-size:12px;
  line-height:normal !important;
  height:250px;
  background:transparent;
}
</style>
</head> 
<body>
<div id="map"></div> 
</body> 
</apex:page>


Here u can pass Account Id from the url to access Googlemap from VF-Page
like: https://c.ap1.visual.force.com/apex/Googlemaps?id=0019000000AoJ64