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
Thakkar ParthThakkar Parth 

Display Accounts pertaining to Opportunity dynamically

How do i get the Object Name and Field Names for my code ? In the Controller , i'm getting Objects based on the IF condition which is not dynamic .Can someone please help me to make the changes . I tried really hard to get this but still no luck from last many days.Appreciate help.

Thanks,
Marc


==== Controller===
public class HighchartsController
{ 
    
    public String ObjectName {get;set;}
  
    
     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> opp;
    public List<Integer> getOpp()
    {
        If(ObjectName=='Contact')
        {
          opp=new List<Integer>();
         String q = 'Select count(Id),parthv__Level__c from Contact where parthv__Level__c  IN(\'Secondary\',\'Primary\') group by parthv__Level__c';
         AggregateResult[] agr =Database.query(q);
         opp.add(Integer.valueof(agr[0].get('expr0')));
         opp.add(Integer.valueof(agr[1].get('expr0')));
         
        
        }
        
        else if(ObjectName=='Opportunity')
        {
        
         opp=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);
         opp.add(Integer.valueof(agr[0].get('expr0')));
         opp.add(Integer.valueof(agr[1].get('expr0')));
         
       }
       
       else if(ObjectName=='Account')
        {
        
         opp=new List<Integer>();
         String q = 'Select count(Id),Rating from Account where Rating IN(\'Hot\',\'Warm\') group by Rating';
         AggregateResult[] agr =Database.query(q);
         opp.add(Integer.valueof(agr[0].get('expr0')));
         opp.add(Integer.valueof(agr[1].get('expr0')));
         
       }
       
       
       return opp;  
    }
    public void setX()
    {}
  
     
   }

=== Page ===

<apex:page standardController="Contact">
  <c:ModelChartUseComponent ObjName="Contact" />
  <!--    <c:ModelChartUseComponent sobj="{!Contact}" fieldsets="{!$ObjectType.Contact.FieldSets.ContactFields}" /> -->
</apex:page>

=== Component ===

<apex:component Controller="HighchartsController">

 <apex:attribute type="String" name="ObjName" required="true" assignTo="{!ObjectName}" description="Original poster failed to provide a useful description"/>

 <!-- Script here -->
   <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 = {!opp};   <!-- Get the Opportunities -->
           // 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>
  <!-- End Default Content REMOVE THIS -->
</apex:component>




Avidev9Avidev9
Well you can use Apex Describe to describe a particular object to get all its fields.

You can do something like

SObjectType stype = Schema.getGlobalDescribe().get('Account');
Map<String,Schema.SObjectField> mfields = stype.getDescribe().fields.getMap();