function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JosephJJosephJ 

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 />
     </apex:page>

     ==== 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>

        <script>
            pieOrdinate = {!X};
           // pieOrdinate = ServerStr.split(',');
             $(function () {
             $('#container').highcharts({
             title: {
             text: 'Chart showing opportunities'
        },
        xAxis:{
                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}
            //data:[2,3,4,5,6]
        },
         {
            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:23,
                y :parseInt(pieOrdinate[0]),
                sliced:true,
                selected:true,
                color: Highcharts.getOptions().colors[1] // Opp's Lost color
              },
               {
                name: 'Won',
                y:parseInt(pieOrdinate[1]),
                color: Highcharts.getOptions().colors[2] // Opp's won color
              }],
               center: [100, 80],
               size: 100,
               showInLegend: false,
               dataLabels:
               {
                 enabled:true
               }
             }]
         });
      });
      </script>
     </apex:component>


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:component>
  <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]}" />
  </apex:repeat>
</apex:component>
Below is the Visualforce page that calls the component:

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

Hope that helps,
- Another James
JosephJJosephJ
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;
            //SOQL 
            res = '[';
            for(i=1;i<13;i++)
            res +=  '' + (i+1) +  ',';
            res += 12;
            res += ']';
            return  res;
        } 
        
      public String bar;
      public String getBar()
      {
          String str = '';
            Integer i;
            //SOQL 
            str = '[';
            for(i=1;i<13;i++)
            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);
         X.add(Integer.valueof(agr[0].get('expr0')));
         X.add(Integer.valueof(agr[1].get('expr0')));
         return X;
    }
    public void setX()
    {}
    
    
  }

Thanks again.

JosephJJosephJ
Still struggling and banging my head . Any idea please ?