+ Start a Discussion

Using different components on Visualforce

I am trying to use a different components in a VF page.At present,the component is made only on Account object.The requirement is to make it for other standard objects like Contacts,Leads etc.

How will this go ?  Thanks in advance. James

       === Page ===
<apex:page standardController="Account">  <!-- This is for Account,how to do it for other objects ? -->
      <c:MadoleChartComponent />

     ==== Component ===

<apex:component Controller="HighchartsController">
       <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
       <script src="https://code.highcharts.com/highcharts.js"></script>
       <script src="https://code.highcharts.com/modules/exporting.js"></script>

      <div id="container" style="min-width: 310px; height: 400px; margin:0 auto"></div>

            pieOrdinate = {!X};
           // pieOrdinate = ServerStr.split(',');
             $(function () {
             title: {
             text: 'Chart showing opportunities'
                categories: ['Jan','Feb','Mar','Apr','May','Jun','July','Aug','Sept','Oct','Nov','Dec']
              labels: {
              items: [{
              html: 'Opportunities',
              style: {
                    left: '50px',
                    top: '18px',
                    color: (Highcharts.theme && Highcharts.theme.textColor) || 'black'
        series: [ {
            type: 'column',
            name: 'Indian Railways',
            data: {!str}
            type: 'spline',
            name: 'Monthly Sales', // Average
           // data: [3, 2.67, 3, 6.33, 3.33],  
           data :{!bar},
            marker: {
                lineWidth: 2,
                lineColor: Highcharts.getOptions().colors[3],
                fillColor: 'white'
            type: 'pie',
            name: 'Total consumption',
            data: [ {
                name: 'Lost',
                y :parseInt(pieOrdinate[0]),
                color: Highcharts.getOptions().colors[1] // Opp's Lost color
                name: 'Won',
                color: Highcharts.getOptions().colors[2] // Opp's won color
               center: [100, 80],
               size: 100,
               showInLegend: false,

James LoghryJames Loghry
You can at least attempt to generalize it. I say attempt, because there's usually one or two things unique across each sobject that you don't always account for.  

To generalize the component, you might add an attribute to take in a generic sobject.  Or, you could pass in a controller or an abstract class / interface as an attribute.  

I've tested the following component on my org, which takes the generic sobject and a field set to display the fields "dynamically" or in a more generic way.

  <apex:attribute type="sObject" name="sobj" required="true" description="test"/>
  <apex:attribute name="fieldsets" type="Schema.FieldSetMember[]" description="test"/>
  <apex:repeat var="field" value="{!fieldsets}">
      <apex:outputText value="{!sobj[field]}" />
Below is the Visualforce page that calls the component:

<apex:page standardController="Account">
    <c:MyVisualforceComponent sobj="{!Account}" fieldsets="{!$ObjectType.Account.FieldSets.AccountFields}" />

Hope that helps,
- Another James
I  don't know why its giving me error . The error is Error: Unknown property 'Account.fieldsets.AccountFields'  I pasted the same code and tried but no luck . Do i need to change attribute name and type ? Below is the controller code for reference .

public class HighchartsController
        public String str;
        public String getStr()
        String res = '';
            Integer i;
            res = '[';
            res +=  '' + (i+1) +  ',';
            res += 12;
            res += ']';
            return  res;
      public String bar;
      public String getBar()
          String str = '';
            Integer i;
            str = '[';
            str +=  '' + i + ',';
            str += 12;
            str += ']';
            return  str;
    public List<Integer> X;
    public List<Integer> getX()
         X=new List<Integer>();
         String q = 'Select count(Id),StageName from opportunity where StageName IN(\'Closed Won\',\'Closed Lost\') group by StageName';
         AggregateResult[] agr =Database.query(q);
         return X;
    public void setX()

Thanks again.

Still struggling and banging my head . Any idea please ?