1

I'm struggling to understand why my code isn't working as expected. I'm not an expert, still in the learning phase.

I have a VF page enter image description here

I'm trying to do a total of the values entered in each column and display at the bottom. Code below,

public class BPSection {

private Id Brokerid;

public PageReference edit() {
    return null;
}

public BPSection(Apexpages.StandardController stdcontroller){
    BrokerId = [SELECT Broker__c FROM BD_Profile__c WHERE Id  = :ApexPages.currentPage().getParameters().get('Id') LIMIT 1].Broker__c;
    BPSection();
}

public List<BD_Profile__c> InsList{set;get;}
public List<BD_Profile__c> InsListtoShow{set;get;}
public boolean showInputTable{set;get;}
public boolean showOutputTable{set;get;}
public void BPSection(){

    showInputTable = true;
    showOutputTable = false;
    InsList = new List<BD_Profile__c>();


    BD_Profile__c b1 = new BD_Profile__c();
    b1.Insurer__c = 'ACE/Chubb';
    BD_Profile__c b2 = new BD_Profile__c();
    b2.Insurer__c = 'AIG';
    BD_Profile__c b3 = new BD_Profile__c();
    b3.Insurer__c = 'Allianz';
    BD_Profile__c b4 = new BD_Profile__c();
    b4.Insurer__c = 'Aspen';
    BD_Profile__c b5 = new BD_Profile__c();
    b5.Insurer__c = 'AVIVA';
    BD_Profile__c b6 = new BD_Profile__c();
    b6.Insurer__c = 'AXA';
    BD_Profile__c b7 = new BD_Profile__c();
    b7.Insurer__c = 'Beazley';
    BD_Profile__c b8 = new BD_Profile__c();
    b8.Insurer__c = 'C N A';
    BD_Profile__c b9 = new BD_Profile__c();
    b9.Insurer__c = 'Covea';
    BD_Profile__c b10 = new BD_Profile__c();
    b10.Insurer__c = 'Ecclesiastical';
    BD_Profile__c b11 = new BD_Profile__c();
    b11.Insurer__c = 'HCC';
    BD_Profile__c b12 = new BD_Profile__c();
    b12.Insurer__c = 'Hiscox';
    BD_Profile__c b13 = new BD_Profile__c();
    b13.Insurer__c = 'HSB';
    BD_Profile__c b14 = new BD_Profile__c();
    b14.Insurer__c = 'Liberty Mutual';
    BD_Profile__c b15 = new BD_Profile__c();
    b15.Insurer__c = 'LV=';
    BD_Profile__c b16 = new BD_Profile__c();
    b16.Insurer__c = 'Market Corp';
    BD_Profile__c b17 = new BD_Profile__c();
    b17.Insurer__c = 'NFU Mutual';
    BD_Profile__c b18 = new BD_Profile__c();
    b18.Insurer__c = 'NIG';
    BD_Profile__c b19 = new BD_Profile__c();
    b19.Insurer__c = 'NMU';
    BD_Profile__c b20 = new BD_Profile__c();
    b20.Insurer__c = 'QBE';
    BD_Profile__c b21 = new BD_Profile__c();
    b21.Insurer__c = 'RSA';
    BD_Profile__c b22 = new BD_Profile__c();
    b22.Insurer__c = 'Tokio Marine';
    BD_Profile__c b23 = new BD_Profile__c();
    b23.Insurer__c = 'Travelers';
    BD_Profile__c b24 = new BD_Profile__c();
    b24.Insurer__c = 'XL Catlin';
    BD_Profile__c b25 = new BD_Profile__c();
    b25.Insurer__c = 'Zurich';
    BD_Profile__c b26 = new BD_Profile__c();
    b26.Insurer__c = 'Other Insurers';

    InsList.add(b1);
    InsList.add(b2);
    InsList.add(b3);
    InsList.add(b4);
    InsList.add(b5);
    InsList.add(b6);
    InsList.add(b7);
    InsList.add(b8);
    InsList.add(b9);
    InsList.add(b10);
    InsList.add(b11);
    InsList.add(b12);
    InsList.add(b13);
    InsList.add(b14);
    InsList.add(b15);
    InsList.add(b16);
    InsList.add(b17);
    InsList.add(b18);
    InsList.add(b19);
    InsList.add(b20);
    InsList.add(b21);
    InsList.add(b22);
    InsList.add(b23);
    InsList.add(b24);
    InsList.add(b25);
    InsList.add(b26);



}
public void Save(){
     Decimal PackagesTotal = 0;
     Decimal FleetTotal = 0;
     Decimal MotorTradeTotal = 0;
     Decimal PCCombinedTotal = 0;
     Decimal ConstructionTotal = 0;
     Decimal PropertyOwnerTotal = 0;
     Decimal PITotal = 0;
     Decimal DOTotal = 0;
     Decimal MarineTotal = 0;
     Decimal EngineeringInsTotal = 0;
     Decimal EngineeringInspTotal = 0;
     Decimal OtherCommLinesTotal = 0;
     Decimal PersMotorTotal = 0;
     Decimal HouseholdTotal = 0;
     Decimal OtherPersLinesTotal = 0;

    InsListtoShow = new List<BD_Profile__c>();

    For(BD_Profile__c p:InsList){
        if(PackagesTotal != null){
            if(p.Packages__c != null){
                PackagesTotal+=p.Packages__c;
                System.Debug('>>>>>>> the value of p is' +p);
            }
        }
        if(FleetTotal != null){
            if(p.Fleet__c != null){
                FleetTotal+=p.Fleet__c;
            }
        }
        if(MotorTradeTotal != null){
            if(p.Motor_Trade__c != null){
                MotorTradeTotal+=p.Motor_Trade__c;
            }
        }
        if( PCCombinedTotal != null){
            if(p.P_C_Combined__c != null){
                PCCombinedTotal+=p.P_C_Combined__c;
            }
        }
        if( ConstructionTotal != null){
            if(p.Construction__c != null){
                ConstructionTotal+=p.Construction__c;
            }
        }    
        if( PropertyOwnerTotal != null){
            if(p.Property_Owner__c != null){
                PropertyOwnerTotal+=p.Property_Owner__c;
            }
        }        
        if( PITotal != null){
            if(p.PI__c != null){
                PITotal+=p.PI__c;
            }
        }            
        if( DOTotal != null){
            if(p.D_O__c != null){
                DOTotal+=p.D_O__c;
            }
        }                
        if( PITotal != null){
            if(p.PI__c != null){
                PITotal+=p.PI__c;
            }
        }            
        if( MarineTotal != null){
            if(p.Marine__c != null){
                MarineTotal+=p.Marine__c;
            }
        }            
        if( EngineeringInsTotal != null){
            if(p.Engineering_Insurance__c != null){
                EngineeringInsTotal+=p.Engineering_Insurance__c;
            }
        }            
        if( EngineeringInspTotal != null){
            if(p.Engineering_Inspection__c != null){
                EngineeringInspTotal+=p.Engineering_Inspection__c;
            }
        }            
        if( OtherCommLinesTotal != null){
            if(p.Other_Comm_Lines__c != null){
                OtherCommLinesTotal+=p.Other_Comm_Lines__c;
            }
        }            
        if( PersMotorTotal != null){
            if(p.Pers_Motor__c != null){
                PersMotorTotal+=p.Pers_Motor__c;
            }
        }            
        if( HouseholdTotal != null){
            if(p.Household__c != null){
                HouseholdTotal+=p.Household__c;
            }
        }         
        if( OtherPersLinesTotal != null){
            if(p.Other_Pers_Lines__c != null){
                OtherPersLinesTotal+=p.Other_Pers_Lines__c;
            }
        }         


                            p.Broker__c = BrokerId;
            InsListtoShow.add(p);
            }


         insert InsListtoShow;
         BD_Profile__c p = New BD_Profile__c();   
         p.Insurer__c = 'Total Broker Account';
         p.Packages__c = Packagestotal;
         p.Fleet__c = FleetTotal;
         p.Motor_Trade__c = MotorTradeTotal;
         p.P_C_Combined__c = PCCombinedTotal;
         p.Construction__c = ConstructionTotal;
         p.Property_Owner__c = PropertyOwnerTotal;
         p.PI__c = PITotal;
         p.D_O__c = DOTotal;
         p.Marine__c = MarineTotal;
         p.Engineering_Insurance__c = EngineeringInsTotal;
         p.Engineering_Inspection__c = EngineeringInspTotal;
         p.Other_Comm_Lines__c = OtherCommLinesTotal;
         p.Pers_Motor__c = PersMotorTotal;
         p.Household__c = HouseholdTotal;
         p.Other_Pers_Lines__c = OtherPersLinesTotal;

         InsListtoShow.add(p);
         ShowInputTable = false;
         ShowOutputTable = true;

 }                            

}

The totalling seems to work okay for the first time on a column and when I enter a value in another column for summation it seems to go back to the Inputpage for some reason. VF below,

<apex:page standardcontroller="BD_Profile__c" extensions="BPSection">   
<apex:form >
  <apex:pageblock rendered="{!showInputTable}">
      <center><apex:commandButton value="Save" action="{!Save}"/></center>
          <apex:pageblockTable value="{!InsList}" var="p" align="center">
              <apex:column headerValue="Insurer"><b><apex:OutputField value="{!p.Insurer__c}"/></b></apex:column>
              <apex:column headerValue="Packages"> <apex:inputField value="{!p.Packages__c}" style="width: 3.66vw;height: 12px;"/> </apex:column>
              <apex:column headerValue="Fleet"> <apex:inputField value="{!p.Fleet__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="Motor Trade"> <apex:inputField value="{!p.Motor_Trade__c}" style="width: 3.66vw;height: 12px" /> </apex:column>
              <apex:column headerValue="P&C Comb"> <apex:inputField value="{!p.P_C_Combined__c}" style="width: 3.66vw;height: 12px" /> </apex:column>
              <apex:column headerValue="Construction"> <apex:inputField value="{!p.Construction__c}" style="width: 3.66vw;height: 12px" /> </apex:column>
              <apex:column headerValue="Prop Owner"> <apex:inputField value="{!p.Property_Owner__c}" style="width: 3.66vw;height: 12px" /> </apex:column>
              <apex:column headerValue="PI"> <apex:inputField value="{!p.PI__c}" style="width: 3.66vw;height: 12px" /> </apex:column>
              <apex:column headerValue="D&O"> <apex:inputField value="{!p.D_O__c}" style="width: 3.66vw;height: 12px" /> </apex:column>
              <apex:column headerValue="Marine"> <apex:inputField value="{!p.Marine__c}" style="width: 3.66vw;height: 12px" /> </apex:column>
              <apex:column headerValue="Eng Ins"> <apex:inputField value="{!p.Engineering_Insurance__c}" style="width: 3.66vw;height: 12px" /> </apex:column>
              <apex:column headerValue="Eng Insp"> <apex:inputField value="{!p.Engineering_Inspection__c}" style="width: 3.66vw;height: 12px" /> </apex:column>
              <apex:column headerValue="Other CL"> <apex:inputField value="{!p.Other_Comm_Lines__c}" style="width: 3.66vw;height: 12px" /> </apex:column>
              <apex:column headerValue="Pers Motor"> <apex:inputField value="{!p.Pers_Motor__c}" style="width: 3.66vw;height: 12px" /> </apex:column>
              <apex:column headerValue="Household"> <apex:inputField value="{!p.Household__c}" style="width: 3.66vw;height: 12px" /> </apex:column>
              <apex:column headerValue="Other PL"> <apex:inputField value="{!p.Other_Pers_Lines__c}" style="width: 3.66vw;height: 12px" /> </apex:column>
          </apex:pageblockTable> 
      <center><apex:commandButton value="Save" action="{!Save}"/></center>
  </apex:pageBlock>   

  <apex:pageBlock rendered="{!showOutputTable}"> 
      <center><apex:commandButton value="Save" action="{!Save}"/><apex:commandButton value="Edit" action="{!edit}"/></center>

          <apex:pageBlockTable value="{!InsListtoShow}" var="p" align="center">
              <apex:column headerValue="Insurer"><b><apex:OutputField value="{!p.Insurer__c}" style="width: 3.66vw;height: 12px"/></b></apex:column>
              <apex:column headerValue="Packages"> <apex:inputField value="{!p.Packages__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="Fleet"> <apex:inputField value="{!p.Fleet__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="Motor Trade"> <apex:inputField value="{!p.Motor_Trade__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="P&C Comb"> <apex:inputField value="{!p.P_C_Combined__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="Construction"> <apex:inputField value="{!p.Construction__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="Prop Owner"> <apex:inputField value="{!p.Property_Owner__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="PI"> <apex:inputField value="{!p.PI__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="D&O"> <apex:inputField value="{!p.D_O__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="Marine"> <apex:inputField value="{!p.Marine__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="Eng Ins"> <apex:inputField value="{!p.Engineering_Insurance__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="Eng Insp"> <apex:inputField value="{!p.Engineering_Inspection__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="Other CL"> <apex:inputField value="{!p.Other_Comm_Lines__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="Pers Motor"> <apex:inputField value="{!p.Pers_Motor__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="Household"> <apex:inputField value="{!p.Household__c}" style="width: 3.66vw;height: 12px"/> </apex:column>
              <apex:column headerValue="Other Pers Lines"> <apex:inputField value="{!p.Other_Pers_Lines__c}" style="width: 3.66vw;height: 12px"/> </apex:column>

           </apex:pageBlockTable>
       <center><apex:commandButton value="Save" action="{!Save}"/></center>       

  </apex:pageBlock>  
</apex:form>
</apex:page>

Can someone take a look and let me know how to make this work please? Any help will be greatly appreciated, Many thanks!

0

It appears to me that when you do a save, you only want it to rerender the 2nd pageBlock where you have !showOutputTable. You need to put an Id on that pageblock and add that to both of your commandButtons to tell the visualforce to only reRender that portion of the page.

Edit

Part of your issue is that you're not actually doing a "Save" in your method. Save means something very specific to VisualForce when you use it in a CommandButton with a StandardController. It thinks that you're writing data to the database and thus need to reset the page afterward or redirect to a detail page. I recommend you rename your method (and perhaps the button too) to something like show or display records. See the VisualForce Developer's Guide for more on this topic (couldn't find the exact reference I was looking for, but this one gives you the idea). Once you do that and add the appropriate rerender tag, I suspect that should solve your issue.

| improve this answer | |
  • Many thanks! I tried added the following, <apex:pageBlock id="outputtable" rendered="{!showOutputTable}"> <center><apex:commandButton value="Save" action="{!Save}" reRender="outputtable"/> still doesn't work. thoughts? – Nikz_Kool Mar 9 '16 at 15:51
  • See my edited answer. I think the issue is related to association with Save Action. – crmprogdev Mar 9 '16 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.