0
Public Class controller_SoqlQuery { 
    public List<AggregateResult> medOppsCount{get;set;}
    public List<AggregateResult> nonMedOppsCount{get;set;}
    public List<AggregateResult> caseClinics  {get;set;}
    public List<AggregateResult> challengeStudies{get;set;}
    public List<AggregateResult> totalLegalOpps {get;set;}
    public List<AggregateResult> positiveCaseClinics  {get;set;}
    public List<AggregateResult> negativeCaseClinics  {get;set;}
    public List<AggregateResult> numberOfPaymentReceipts {get;set;}
    public List<AggregateResult> paymentReceiptTotalAmount {get;set;}
    public List<AggregateResult> conferenceCalls {get;set;}
    public List<AggregateResult> surveysCompleted {get;set;}
    public List<AggregateResult> surveyAvgScore {get;set;}
    public List<AggregateResult> totalOpps {get;set;}
    public List<AggregateResult> caseClinicsPrepTime  {get;set;}
    public List<AggregateResult> caseClinicsCallTime  {get;set;}
    public List<Opportunity> Opp {get; set;}
    public Integer medCount {get; set;}
    public Integer nonMedCount {get; set;}
    public Integer caseClinicsCount {get; set;}
    public Integer challengeStudiesCount {get; set;}
    public Integer legalOppsCount {get; set;}
    public Integer positiveCaseClinicsCount {get; set;}
    public Integer negativeCaseClinicsCount {get; set;}
    public Integer paymentReceiptsCount {get; set;}
    public Integer totalReceiptAmount {get; set;}
    public Integer conferenceCallsCount {get; set;}
    public Integer surveysCompletedCount {get; set;}
    public double avgSurveyScore {get; set;}
    public Integer totalOppsCount {get; set;}
    public Double prepTimeCaseClinics {get; set;}
    public Double callTimeCaseClinics {get; set;}
    public ID PreviousOppId {get; set;}

    Public controller_SoqlQuery (ApexPages.StandardController controller) {

            Opportunity opp = [SELECT Sub_Parent_Opportunity__c FROM Opportunity WHERE Id = :controller.getId() LIMIT 1];
                ID PreviousOppID = (Id) opp.get('Sub_Parent_Opportunity__c');


    if ( PreviousOppID != null){

        medOppsCount = [Select count(id) medCount FROM Opportunity WHERE (Sub_Parent_Opportunity__c = :PreviousOppID AND Subject_Matter__c = 'Medical' AND StageName = 'Closed') ]; 
            for(AggregateResult ar : MedOppsCount ){
            medCount = (Integer) ar.get('medCount');

                 }

        nonMedOppsCount = [Select count(id) nonMedCount FROM Opportunity WHERE (Sub_Parent_Opportunity__c = :PreviousOppID AND Subject_Matter__c = 'Non-Medical' AND StageName = 'Closed' ) ]; 
            for(AggregateResult nm : nonMedOppsCount ){
                nonMedCount = (Integer) nm.get('nonMedCount');

                 }

        caseClinics = [Select count(id) caseClinicsCount FROM Opportunity WHERE (Sub_Parent_Opportunity__c = :PreviousOppID AND Record_Type_Name__c = 'Case Clinic' AND StageName = 'Closed (cloned to Opp)' ) ]; 
            for(AggregateResult cc : caseClinics ){
                caseClinicsCount  = (Integer) cc.get('caseClinicsCount');

                }

        challengeStudies = [Select count(id) challengeStudiesCount FROM Opportunity WHERE (Sub_Parent_Opportunity__c = :PreviousOppID AND Record_Type_Name__c = 'Challenge Study' AND StageName = 'Closed' ) ]; 
            for(AggregateResult chs : challengeStudies){
                challengeStudiesCount = (Integer) chs.get('challengeStudiesCount');

                }           

        totalLegalOpps = [Select count(id) legalOppsCount FROM Opportunity WHERE Sub_Parent_Opportunity__c = :PreviousOppID AND Record_Type_Name__c = 'Legal']; 
            for(AggregateResult loc : totalLegalOpps){
                legalOppsCount = (Integer) loc.get('legalOppsCount');

                }

         positiveCaseClinics = [Select count(id) positiveCaseClinicsCount FROM Opportunity WHERE Sub_Parent_Opportunity__c = :PreviousOppID AND Record_Type_Name__c = 'Case Clinic' AND Case_Clinic_Outcome__c = 'Positive']; 
            for(AggregateResult pcc : positiveCaseClinics){
                positiveCaseClinicsCount = (Integer) pcc.get('positiveCaseClinicsCount');

                } 

         negativeCaseClinics = [Select count(id) negativeCaseClinicsCount FROM Opportunity WHERE Sub_Parent_Opportunity__c = :PreviousOppID AND Record_Type_Name__c = 'Case Clinic' AND Case_Clinic_Outcome__c = 'Positive']; 
            for(AggregateResult ncc : negativeCaseClinics){
                negativeCaseClinicsCount = (Integer) ncc.get('negativeCaseClinicsCount');

                }

          numberOfPaymentReceipts = [Select count(id) paymentReceiptsCount FROM Opportunity WHERE Sub_Parent_Opportunity__c = :PreviousOppID AND Receipt_Amount__c > 0]; 
            for(AggregateResult npr :  numberOfPaymentReceipts){
                paymentReceiptsCount= (Integer) npr.get('paymentReceiptsCount');

                }       

          paymentReceiptTotalAmount = [Select Sum(Receipt_Amount__c) totalReceiptAmount FROM Opportunity WHERE Sub_Parent_Opportunity__c = :PreviousOppID AND Receipt_Amount__c > 0]; 
            for(AggregateResult nra :  paymentReceiptTotalAmount){
                totalReceiptAmount = (Integer) nra.get('totalReceiptAmount');

                } 

          surveysCompleted = [Select Count(id) surveysCompletedCount FROM Survey__c WHERE Sub_Parent_Opportunity_id__c = :PreviousOppID ]; 
            for(AggregateResult sc :  surveysCompleted){
                surveysCompletedCount = (Integer) sc.get('surveysCompletedCount');

                }

          surveyAvgScore = [Select Avg(Average_Rating__c) avgSurveyScore FROM Survey__c WHERE Sub_Parent_Opportunity_id__c = :PreviousOppID ]; 
            for(AggregateResult sc :  surveyAvgScore){
                avgSurveyScore = (Double) sc.get('avgSurveyScore');

                }   

          totalOpps = [Select count(id) totalOppsCount FROM Opportunity WHERE Sub_Parent_Opportunity__c = :PreviousOppID ]; 
            for(AggregateResult toc : totalOpps){
                totalOppsCount = (Integer) toc.get('totalOppsCount');

                }      

         caseClinicsPrepTime = [Select SUM(Prep_Time_Value__c) prepTimeCaseClinics FROM Opportunity WHERE Sub_Parent_Opportunity__c = :PreviousOppID AND Record_Type_Name__c = 'Case Clinic' AND Prep_Time_Value__c != 0.0]; 
            for(AggregateResult pcc : caseClinicsPrepTime){
                prepTimeCaseClinics= (double) pcc.get('prepTimeCaseClinics');

                }   

         caseClinicsCallTime = [Select SUM(Call_Time_Value__c) callTimeCaseClinics FROM Opportunity WHERE Sub_Parent_Opportunity__c = :PreviousOppID AND Record_Type_Name__c = 'Case Clinic' AND Call_Time_Value__c != 0.0 ]; 
            for(AggregateResult pcc : caseClinicsCallTime){
                callTimeCaseClinics= (Double) pcc.get('callTimeCaseClinics');

                }             
        } 
                else {

                        medCount = 0; nonMedCount = 0; caseClinicsCount = 0;challengeStudiesCount=0;legalOppsCount=0;positiveCaseClinicsCount=0;negativeCaseClinicsCount=0;paymentReceiptsCount=0;totalReceiptAmount=0;surveysCompletedCount=0;avgSurveyScore=0.0;totalOppsCount=0;prepTimeCaseClinics=0.0;callTimeCaseClinics=0.0;}



      }
}

marked as duplicate by Jayant Das, Adrian Larson apex Apr 15 at 19:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Browse other questions tagged or ask your own question.