Saturday, 9 February 2013

Print VF page on button click


VF page Code
============

<apex:page controller="bhadraPieChartController" title="Pie Chart" showHeader="false">
<script>
   //this is only the code for printing the page
    function fun(){
           document.getElementById('prnBtn').style.display='none'; 
           //document.getElementById('winBtn').style.display='none';    
           window.print();
            document.getElementById('prnBtn').style.display='block'; 
    }
</script>
<apex:form >
  <div  style="padding-left: 484px;padding-top: 27px;"><input type="button"   onClick="fun();" id="prnBtn"  value="Print This Page" style="background: green;"/></div>
</apex:form>
    <apex:chart height="350" width="450" data="{!pieData}">
        <apex:pieSeries dataField="data" labelField="name"/>
        <apex:legend position="right"/>
    </apex:chart>
</apex:page>
==========
Controller
============

public class bhadraPieChartController {
 

  public List<BhadraPieWedgeData> getPieData() {

       List<BhadraPieWedgeData> namedata = new List<BhadraPieWedgeData>();  
   

    namedata.add(new BhadraPieWedgeData('Jan', 30));
     
    namedata.add(new BhadraPieWedgeData('Feb', 15));
       
    namedata.add(new BhadraPieWedgeData('Mar', 10));
       
    namedata.add(new BhadraPieWedgeData('Apr', 20));
       
    namedata.add(new BhadraPieWedgeData('May', 20));
       
    namedata.add(new BhadraPieWedgeData('Jun', 5));
       
    return namedata;
   
  }
   
 
   
   
    public class BhadraPieWedgeData {

       
        public String name { get; set; }
       
        public Integer data { get; set; }
       
        public BhadraPieWedgeData(String name1, Integer data1) {
               this.name = name1;          
               this.data = data1;
        }

 
    }

}
=============
Output:
==========


Print page



Thursday, 7 February 2013

Piechart on VFpage with dynamic data


Page:
======

<apex:page controller="PieChartController1" title="Pie Chart" sidebar="false">
  <center>  <apex:chart height="350" width="450" data="{!pieData}">
        <apex:pieSeries dataField="data" labelField="name"/>
        <apex:legend position="right"/>
    </apex:chart>
   </center>
</apex:page>


Controller
========

public class PieChartController1 {
    public List<PieWedgeData> getPieData() {
        List<PieWedgeData> data = new List<PieWedgeData>();
        integer i=0;
        for(Opportunity opp:[select id,name,stagename,amount from Opportunity limit 10]){
            data.add(new PieWedgeData(opp.name,i++));      
       }
        return data;
    }

    // Wrapper class
   
    public class PieWedgeData {

        public String name { get; set; }
        public integer data { get; set; }

        public PieWedgeData(String name, integer data) {
            this.name = name;
            this.data = data;
        }
    }
}

Output:
======



Piechart on vf page with static data(visualforce charting)

Page:
======

<apex:page controller="PieChartController" title="Pie Chart" sidebar="false">
 <center>   <apex:chart height="350" width="450" data="{!pieData}">
        <apex:pieSeries dataField="data" labelField="name"/>
        <apex:legend position="right"/>
    </apex:chart>
 </center>
</apex:page>

Controller
=========

public class PieChartController {
    public List<PieWedgeData> getPieData() {
        List<PieWedgeData> data = new List<PieWedgeData>();
        data.add(new PieWedgeData('Jan', 30));
        data.add(new PieWedgeData('Feb', 15));
        data.add(new PieWedgeData('Mar', 10));
        data.add(new PieWedgeData('Apr', 20));
        data.add(new PieWedgeData('May', 20));
        data.add(new PieWedgeData('Jun', 5));
        return data;
    }

    // Wrapper class
   
    public class PieWedgeData {

        public String name { get; set; }
        public Integer data { get; set; }

        public PieWedgeData(String name, Integer data) {
            this.name = name;
            this.data = data;
        }
    }
}
===========
Output:
======


Wednesday, 6 February 2013

Inserting dashboard on vf page

For inserting dashboard designed in salesforce on vf page we need to use <apex:iframe> tag and id of that dashboard as shown below.

vfpage  with dashboard:
=================


<apex:page sidebar="false" showHeader="false">
 <apex:iframe src="/01ZG0000000d9Qb?isdtp=nv" scrolling="true" height="1588px" width="100%"/>
</apex:page>

in the above  'src="/01ZG0000000d9Qb?isdtp=nv" '
if you are not  specifying "isdtp=nv" as a parameters it shows dashboard including header.

if you specify then it shows only dashboard with out header.


Output:
==========
======

Google charts on visualforce page

Here we are taking the url of google chart and assigning dynamic data to the google chart.
Here we are displaying account type and their count.

Controller:
=========
public class googleChartController {
private String chartData;

public String getChartData()
{
 return chartData;
}
//Contructor
public googleChartController ()
{
 //get obtain a list of picklist values
 Schema.DescribeFieldResult F = Account.Type.getDescribe();
 List<Schema.PicklistEntry> P = F.getPicklistValues();
 //where chart data should be stored.
 List<ChartDataItem> items = new List<ChartDataItem>();

 //iterate through each picklist value and get number of accounts
 // I wish we could do GROUP BY in SOQL!
 for(Schema.PicklistEntry pValue : P)
 {
    integer Count = [select count() from Account where Type = :pValue.getValue() limit 10000];
    if (Count > 0)
      items.add(new ChartDataItem(pValue.getValue()+ '-['+ Count.format() + ']' , Count.format()));
 }
 //Prepare the chart URL
 //String chartPath = 'http://chart.apis.google.com/chart?chs=600x200&cht=p3';
 String chartPath='//chart.googleapis.com/chart?chs=600x200&cht=p3&chco=7777CC|76A4FB|3399CC|3366CC';
 chartData = chartPath +  getChartData(items);
}

private String getChartData(List<ChartDataItem> items)
{
 String chd = ''; //23,34,56
 String chl = ''; //Hello|World

 for(ChartDataItem citem : items)
 {
    chd += citem.ItemValue + ',';
    chl += citem.Label + '|';
 }
 //remove the last comma or pipe
 chd = chd.substring(0, chd.length() -1);
 chl = chl.substring(0, chl.length() -1);

 String result = '&chd=t:' + chd + '&chl=' + chl;
 return result;
}// end of method

public class ChartDataItem
{
 public String ItemValue {  get;  set; }
 public String Label {   get;   set; }
 public ChartDataItem(String Label, String Value)
 {
    this.Label = Label;
    this.ItemValue = Value;
  }
 }
}// end of class
=-==================
VF page
================
<apex:page controller="googleChartController" tabStyle="Account">
<apex:sectionHeader title=" Type of Account"></apex:sectionHeader>
<apex:image url="{!chartData}"></apex:image>
</apex:page>

Output:
==========

Multiselect picklist with up and down arrows


Visualforce Component
=====================

<!--
  The MultiselectPicklist component implements a multiselect picklist similar
  to that seen when adding tabs to a Force.com application.
  
  HTML elements use the same classes as the native multiselect picklist, to
  keep visual consistency in the UI.
  
  In addition to the visible elements, the component contains two hidden input
  elements, the purpose of which is to hold a string representation of the
  contents of each listbox. As options are added, removed or moved within the
  listboxes, the content of the hidden elements is synchronized to the content
  of the listboxes. When the Visualforce page is submitted, the 
  MultiselectController updates its SelectOption[] variables from these hidden 
  elements.
 -->
<apex:component controller="MultiselectController">
  <apex:attribute name="leftLabel" description="Label on left listbox."
    type="String" required="true" />
  <apex:attribute name="rightLabel" description="Label on right listbox."
    type="String" required="true" />
  <apex:attribute name="size" description="Size of listboxes."
    type="Integer" required="true" />
  <apex:attribute name="width" description="Width of listboxes."
    type="String" required="true" />

  <apex:attribute name="leftOptions"
    description="Options list for left listbox." type="SelectOption[]"
    required="true" assignTo="{!leftOptions}" />
  <apex:attribute name="rightOptions"
    description="Options list for right listbox." type="SelectOption[]"
    required="true" assignTo="{!rightOptions}" />

  <apex:outputPanel id="multiselectPanel" layout="block" styleClass="duelingListBox">
    <table class="layout">
      <tbody>
        <tr>
          <td class="selectCell">
            <apex:outputPanel layout="block" styleClass="selectTitle">
              <!-- 
                Visualforce prepends the correct prefix to the outputLabel's 
                'for' attribute
              -->
              <apex:outputLabel value="{!leftLabel}" 
                for="multiselectPanel:leftList" />
            </apex:outputPanel>
            <select id="{!$Component.multiselectPanel}:leftList" 
              class="multilist" multiple="multiple" size="{!size}" 
              style="width: {!width};">
              <apex:repeat value="{!leftOptions}" var="option">
                <option value="{!option.value}">{!option.label}</option>
              </apex:repeat>
            </select>
          </td>
          <td class="buttonCell">
            <apex:outputPanel layout="block" styleClass="text">Add</apex:outputPanel>
            <apex:outputPanel layout="block" styleClass="text">
              <apex:outputLink value="javascript:moveSelectedOptions('{!$Component.multiselectPanel}:leftList', 
                  '{!$Component.multiselectPanel}:rightList', '{!$Component.leftHidden}', 
                  '{!$Component.rightHidden}');"
                id="btnRight">
                <apex:image value="/s.gif" alt="Add" styleClass="rightArrowIcon"
                  title="Add" />
              </apex:outputLink>
            </apex:outputPanel>
            <apex:outputPanel layout="block" styleClass="text">
              <apex:outputLink value="javascript:moveSelectedOptions('{!$Component.multiselectPanel}:rightList', 
                  '{!$Component.multiselectPanel}:leftList', '{!$Component.rightHidden}', 
                  '{!$Component.leftHidden}');"
                id="btnLeft">
                <apex:image value="/s.gif" alt="Remove"
                  styleClass="leftArrowIcon" title="Remove" />
              </apex:outputLink>
            </apex:outputPanel>
            <apex:outputPanel layout="block" styleClass="duelingText">Remove</apex:outputPanel>
          </td>
          <td class="selectCell">
            <apex:outputPanel layout="block" styleClass="selectTitle">
              <apex:outputLabel value="{!rightLabel}" for="multiselectPanel:rightList" />
            </apex:outputPanel>
            <select id="{!$Component.multiselectPanel}:rightList" 
              class="multilist" multiple="multiple" size="{!size}" 
              style="width: {!width};">
              <apex:repeat value="{!rightOptions}" var="option">
                <option value="{!option.value}">{!option.label}</option>
              </apex:repeat>
            </select>
          </td>
          <td class="buttonCell"><apex:outputPanel layout="block"
              styleClass="text">Up</apex:outputPanel>
            <apex:outputPanel layout="block" styleClass="text">
              <apex:outputLink value="javascript:slideSelectedOptionsUp('{!$Component.multiselectPanel}:rightList', 
                  '{!$Component.rightHidden}');"
                id="upBtn">
                <apex:image value="/s.gif" alt="Up" styleClass="upArrowIcon"
                  title="Up" />
              </apex:outputLink>
            </apex:outputPanel>
            <apex:outputPanel layout="block" styleClass="text">
              <apex:outputLink value="javascript:slideSelectedOptionsDown('{!$Component.multiselectPanel}:rightList', 
                  '{!$Component.rightHidden}');"
                id="downBtn">
                <apex:image value="/s.gif" alt="Down" styleClass="downArrowIcon"
                  title="Down" />
              </apex:outputLink>
            </apex:outputPanel>
            <apex:outputPanel layout="block" styleClass="text">Down</apex:outputPanel>
          </td>
        </tr>
      </tbody>
    </table>
    <apex:inputHidden value="{!leftOptionsHidden}" id="leftHidden" />
    <apex:inputHidden value="{!rightOptionsHidden}" id="rightHidden" />
  </apex:outputPanel>
  <script type="text/javascript">
    if (!buildOutputString) {
      // Create a string from the content of a listbox
      var buildOutputString = function(listBox, hiddenInput) {
        var str = '';

        for ( var x = 0; x < listBox.options.length; x++) {
          str += encodeURIComponent(listBox.options[x].value) + '&'
              + encodeURIComponent(listBox.options[x].text) + '&';
        }
        str.length--;

        hiddenInput.value = str.slice(0, -1);
      }
    }

    if (!moveSelectedOptions) {
      // Move the selected options in the idFrom listbox to the idTo
      // listbox, updating the corresponding strings in idHdnFrom and
      // idHdnTo
      var moveSelectedOptions = function(idFrom, idTo, idHdnFrom, idHdnTo) {
        listFrom = document.getElementById(idFrom);
        listTo = document.getElementById(idTo);

        for ( var x = 0; x < listTo.options.length; x++) {
          listTo.options[x].selected = false;
        }

        for ( var x = 0; x < listFrom.options.length; x++) {
          if (listFrom.options[x].selected == true) {
            listTo.appendChild(listFrom.options[x]);
            x--;
          }
        }

        listTo.focus();

        buildOutputString(listFrom, document.getElementById(idHdnFrom));
        buildOutputString(listTo, document.getElementById(idHdnTo));
      }
    }

    if (!slideSelectedOptionsUp) {
      // Slide the selected options in the idList listbox up by one position,
      // updating the corresponding string in idHidden
      var slideSelectedOptionsUp = function(idList, idHidden) {
        listBox = document.getElementById(idList);

        var len = listBox.options.length;

        if (len > 0 && listBox.options[0].selected == true) {
          return;
        }

        for ( var x = 1; x < len; x++) {
          if (listBox.options[x].selected == true) {
            listBox.insertBefore(listBox.options[x],
                listBox.options[x - 1]);
          }
        }

        listBox.focus();

        buildOutputString(listBox, document.getElementById(idHidden));
      }
    }

    if (!slideSelectedOptionsDown) {
      // Slide the selected options in the idList listbox down by one position,
      // updating the corresponding string in idHidden
      var slideSelectedOptionsDown = function(idList, idHidden) {
        listBox = document.getElementById(idList);

        var len = listBox.options.length;

        if (len > 0 && listBox.options[len - 1].selected == true) {
          return;
        }

        for ( var x = listBox.options.length - 2; x >= 0; x--) {
          if (listBox.options[x].selected == true) {
            listBox.insertBefore(listBox.options[x + 1],
                listBox.options[x]);
          }
        }

        listBox.focus();

        buildOutputString(listBox, document.getElementById(idHidden));
      }
    }
    
    // initialize the string representations
    buildOutputString(document.getElementById('{!$Component.multiselectPanel}:leftList'), 
        document.getElementById('{!$Component.leftHidden}'));
    buildOutputString(document.getElementById('{!$Component.multiselectPanel}:rightList'), 
        document.getElementById('{!$Component.rightHidden}'));
  </script>
</apex:component>

Controller for above component:
================================
/*
 * MultiselectController synchronizes the values of the hidden elements to the
 * SelectOption lists.
 */
public with sharing class MultiselectController {
    // SelectOption lists for public consumption
    public SelectOption[] leftOptions { get; set; }
    public SelectOption[] rightOptions { get; set; }
    
    // Parse &-separated values and labels from value and 
    // put them in option
    private void setOptions(SelectOption[] options, String value) {
        options.clear();
        String[] parts = value.split('&');
        for (Integer i=0; i<parts.size()/2; i++) {
            options.add(new SelectOption(EncodingUtil.urlDecode(parts[i*2], 'UTF-8'), 
              EncodingUtil.urlDecode(parts[(i*2)+1], 'UTF-8')));
        }
    }
    
    // Backing for hidden text field containing the options from the
    // left list
    public String leftOptionsHidden { get; set {
           leftOptionsHidden = value;
           setOptions(leftOptions, value);
        }
    }
    
    // Backing for hidden text field containing the options from the
    // right list
    public String rightOptionsHidden { get; set {
           rightOptionsHidden = value;
           setOptions(rightOptions, value);
        }
    }
}

=====================
Visualforce Page
=======================
<apex:page controller="MultiselectExampleController" showHeader="false">
    <apex:form >
        <apex:pageBlock title="Contacts">
            <c:MultiselectPicklist leftLabel="Available Contacts"     leftOptions="{!allContacts}"  rightLabel="Selected Contacts"
                rightOptions="{!selectedContacts}"       size="14"   width="150px"/>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
    <apex:outputText >{!message}</apex:outputText>
</apex:page>
=================================
Controller for the visualforce page
===============================public with sharing class MultiselectExampleController {

    public SelectOption[] selectedContacts { get; set; }
    public SelectOption[] allContacts { get; set; }
    
    public String message { get; set; }
    
    public MultiselectExampleController() {
        selectedContacts = new List<SelectOption>();
        
        List<Contact> contacts = [SELECT Name, Id FROM Contact];    
        allContacts = new List<SelectOption>();
        for ( Contact c : contacts ) {
            allContacts.add(new SelectOption(c.Id, c.Name));
        }
    }

    public PageReference save() {
        message = 'Selected Contacts: ';
        Boolean first = true;
        for ( SelectOption so : selectedContacts ) {
            if (!first) {
                message += ', ';
            }
            message += so.getLabel() + ' (' + so.getValue() + ')';
            first = false;
        }
        
        return null;       
    }
}
=====================
Ouput:
=========

Sunday, 3 February 2013

Passing parameters or values to flow variables

We can pass parameters or values to flow variables.

For example i have a flow with opportunity details.Here i am navigating to this flow  from a custom detail page button on account.Here i want to pass Account Id to flow variable 'accountid' opportunity flow included in vf page.

Now construct a url  in our custom button as shown below

"/apex/pagename?accountid={!Account.Id}"

then the flow included in the vf page can get the value from 'accountid' parameter to 'accountid' variable inflow.

Here we should remember one thing.
Here both url parameter name and variable name in flow should be same.

Sorting Custom Picklist values


Sorting  custom picklist values prepared from controller is very easy when we use same values for both value and label of picklist
Here i am preparing picklist with same values for both value and label of the picklist.

List<user> lstuser=[select id,name from User];
Public List<Selectoption> lstoption{get;set;}

for(User uobj:lstuser){

lstoption.add(new selectoption(u.name,u.name));

}

in the above selectoption list having user name for both value and label
so we can sort this list by using "sort()" method as below.

eg:lstoption.sort();
===


But whenever the picklist doesn't have same values for value and label of piclist then

you cannot sort directly by using sort() method.Even it is applicable but it

doesn't sort your values properly.

See here in my case i have a picklist having  user Id as value and user name as label

now i need to have sort th picklist.

List<user> lstuser=[select id,name from User];
Public List<Selectoption> lstoption{get;set;}

for(User uobj:lstuser){

lstoption.add(new selectoption(u.id,u.name));

}


when you apply sort() method for the above selectoption list it is not sorting properly.

whenever we have the situation like this we need to apply some logic as shown below.

1)Make a string with the combination of username and user id with seperation of any character for splitting.
Add the string to string list for every iteration.
2)Then sort the string list.
3)After that iterate through the string list and split it to id,name assign them to selectoption list.


eg:
===

List<SelectOption> lstoptions=new List<SelectOption>();
List<String> lststring=new List<String>();

for(User uobj:lstuser){

lststring.add(uobj.name+'#'+u.id);//(1) step

}
lststring.sort();//(2) step
//(3) step
for(String s:lststring){
  List<String> luidname=s.split('#');
lstoptions.add(new selectoption(luidname[1],luidname[0]));

}

Now you can get the values in sorting order from selectoption list "lstoptions"



Mastering Agentforce: The Power of Agent Script

Hi, In the rapidly evolving world of AI, the challenge for developers has always been balancing the creative reasoning of Large Language Mod...