+ Start a Discussion
AK2017AK2017 

apex method is not working for second time

Hello,

I have created a visualforce to show an opportunity pulled from SOQL in Apex class.
I have a method which should save the existing opportunity and rerun the same logic above to pull the next opportunity on the page.

When the page loads initially, there is not opportunity assigned. user clicks on the button, it pulls up the opportunity.

when trying to save the opportunity after editing and rerun the same logic by clicking the same button, the method is not called , the opportunity is not saved and it is not pulling the next opportunity.

i even had the soql logic in the constructor and save/update in a seprate method, the method was not called.

can anyone throw me some light on what i making it wrong.

Here is my VF page and apex class.
 
<apex:page Controller="OpportunityListViewController" docType="html-5.0">
<meta name="viewport" content="width=device-width, initial-scale=1"/>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  th{background: #ff9999;}
  //tr{background: #ffe6e6;}
  a{font-color:blue;}
  </style>
 <apex:form >
 <apex:pageBlock >
 <div style="width:100%" class="container">
 <h3>Lead to be contacted</h3><br></br><br></br>
 <table width="100%" class="table table-bordered table-hover">
 <th>Stage</th>
 <th>Status</th>
 <th>FullName</th>
 <th>Email</th>
 <th>Home Phone</th>
 <th>Work Phone</th>
 <th>Mobile Phone</th>
 
 <apex:repeat value="{!FirstSection}" var="New">
 <tr>
  <td>{!New.StageName}</td>
  <td>{!New.Opportunity_Status__c}</td>
  <td>{!New.Account.FirstName} {!New.Account.LastName}</td>
  <td>{!New.Account.PersonEmail}</td>
  <td>{!New.Account.Home_Phone__pc}</td>
  <td>{!New.Account.Work_Phone__pc}</td>
  <td>{!New.Account.PersonMobilePhone}</td>
  </tr>
  </apex:repeat>
  </table>
  <table>
      <apex:repeat value="{!FirstSection}" var="New">
          <tr>
              <td>
                   <apex:selectList size="1" value="{!FinalDispositioncode}">
                      <apex:selectOptions value="{!dispositioncode}"></apex:selectOptions>
                  </apex:selectList>
              </td>
              <td>
                  <apex:inputField value="{!New.Callback_Time__c}" />
              </td>
          
          </tr>
      </apex:repeat>
  
  </table>  
  
  <apex:commandButton value="save1" action="{!OpportunityListViewControlle}"/> 


 </div>
  <apex:outputPanel id="Newleaddisplay">
  <apex:outputPanel rendered="{!(NewleadPopup == true)}">
             
                 
               <div id="pwdinactiveModal" class="modal show" tabindex="-1" role="dialog" style="border:0px;" >
                          <div class="modal-dialog" style="border:0px;">
                          <div class="modal-content" style="border:0px;">
                              <div class="modal-header text-center">
                                  <p style="color:#2d4366;">You have got a new lead to be contacted. If this lead is not contacted within 60 seconds, it will be routed back to next Admission advisor.</p><br></br>
                                  <p><strong>Student Name: {!Newlead.Account.Firstname}&nbsp;{!Newlead.Account.Lastname}</strong></p>
                              </div>
                              <div class="modal-body" style="border:0px;">
                                  <div class="col-md-12" style="border:0px;">
                                        
                                            <div class="panel-body" style="border:0px;">
                                                        <fieldset>                                                
                                                           <!-- <apex:commandbutton styleclass="btn btn-lg btn-primary btn-block" value="Activate and Send My Password" action=""/> -->
                                                        </fieldset>
                                             </div>     
                                    </div>
                              </div>
                              <div class="modal-footer" style="border:0px;">
                                  <div class="col-md-12">
                                <!--  <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button> -->
                                  </div>    
                              </div> 
                          </div>
                          </div>
                  </div>
       </apex:outputPanel>
       </apex:outputPanel>
 </apex:pageBlock> 
  
 </apex:form>

</apex:page>

APEX class
 
public class OpportunityListViewController {

public List<opportunity> NewOpps{get;set;}
public List<opportunity> SecondSection{get;set;}
//public List<opportunity> SecondContactOpps {get;set;}
public Opportunity FirstSection{get;set;}
Integer count = 0;
public Boolean NewleadPopup {Get;set;}
Public Opportunity Newlead {get;set;}
public datetime callbacktime {get;set;}
public datetime FinalDispositioncode{get;set;}
public datetime Notes{get;set;}
                        
    public PageReference incrementCounter() {
        count++;
        return null;
    }
                        
    public Integer getCount() {
        return count;
    }
    
    public List<SelectOption> getdispositioncode() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Left VM','Left VM'));
        options.add(new SelectOption('Spoke with Student','Spoke with Student'));
        options.add(new SelectOption('Emailed student','Emailed student'));
        options.add(new SelectOption('Scheduled callback for interview','Scheduled callback for interview'));
        return options;
    }
    

   public pagereference OpportunityListViewControlle() {
         system.debug('getting into it');
         
         if(FirstSection != null){
             system.debug('getting into it inside');
             update FirstSection;
         }
         
         Firstsection = null;
         List<Opportunity> Allopps = new List<Opportunity>();
        //Opportunity Firstsection = new List<Opportunity>();
        SecondSection = new List<Opportunity>();
        List<Opportunity> UserScheduledopps = new List<Opportunity>();
        List<Opportunity> CommonUserEncounteropps = new List<Opportunity>();
        List<Opportunity> CommonUserNewopps = new List<Opportunity>();
        List<Opportunity> CommonUserScheduledopps = new List<Opportunity>();
        String Commonuser = System.Label.Common_Pool_user;
        Allopps = [Select id,StageName, Name, Ownerid, Encounter_Time__c, Callback_Time__c, Account.Firstname, Account.Lastname, Opportunity_Status__c, Comments__c, Account.PersonEmail, Account.Home_Phone__pc, Account.Work_phone__pc, Account.PersonMobilePhone from Opportunity where( (Ownerid =: userinfo.getuserid() 
                             AND (Encounter_Time__c != null OR Opportunity_status__c =: 'New' OR Callback_Time__c != null)) OR (Ownerid =: Commonuser AND (Encounter_Time__c != null OR Opportunity_status__c =: 'New' OR Callback_Time__c != null))) and Timestamp_Qual_Applicant__c = null and StageName != 'Stale' and StageName != 'Complete' order by Encounter_Time__c DESC, Createddate DESC, Callback_Time__c DESC limit 11]; 
       
      
       
       // code to take first Encounter in the list 
        for(Opportunity Opp:Allopps){
             if(Opp.Ownerid == userinfo.getuserid() && Firstsection == null){
                  if(Opp.Encounter_Time__c != null){
                    FirstSection = Opp;                    
                  } 
             } else if(Firstsection == null) {
                 if(Opp.Encounter_Time__c != null){
                    Opp.Ownerid = userinfo.getuserid();
                    update Opp; 
                    FirstSection = Opp;                    
                  } 
             }
         }
         
        // code to take newly created lead in the list  
        for(Opportunity Opp:Allopps){
             if(Opp.Ownerid == userinfo.getuserid() && Firstsection == null){
                  if(Opp.Opportunity_status__c == 'New'){
                    FirstSection = Opp;                    
                  } 
             } else if(Firstsection == null) {
                 if(Opp.Opportunity_status__c == 'New'){
                    Opp.Ownerid = userinfo.getuserid();
                    update Opp; 
                    FirstSection = Opp;                    
                  } 
             }
         }
        
       // code to give preference to user owned Callback scheduled leads
        Opportunity TempFirstSection = New Opportunity();
        for(Opportunity Opp:Allopps){
            if(Opp.Ownerid == userinfo.getuserid() && Opp.Callback_Time__c != null && TempFirstSection.id == null){
               
                  /* Integer Timediff1 = (Opp.Callback_Time__c.time().millisecond() - System.now().time().millisecond())*24*60;
                   //integer Timediff1 = (Opp.Callback_Time__c.timeGMT() - System.now().time())*24*60;
                   integer Timediff2 = (System.now().time().millisecond() - Opp.Callback_Time__c.time().millisecond())*24*60;
                   system.debug('Timediff1 '+Timediff1 );
                   system.debug('Timediff2 '+Timediff2 );
                    if(((Timediff1 <= 5) || (Timediff2 <= 15))){
                    
                    if(Opp.Callback_Time__c.date() == System.now().date() &&  Opp.Callback_Time__c.time() - System.now().date() )
                        system.debug('Opp name'+Opp.Name);
                        TempFirstSection = Opp; 
                    } */
                    
                    if((System.now().time() <= Opp.Callback_Time__c.time() && System.now().time().addminutes(5) >= Opp.Callback_Time__c.time()) || (System.now().time() > Opp.Callback_Time__c.time() && System.now().time().addminutes(-15) <= Opp.Callback_Time__c.time())){
                         system.debug('Opp name'+Opp.Name);
                        TempFirstSection = Opp; 
                    }
           }
            
       } 
       
      // code to give preference to common user owned Callback scheduled leads 
       for(Opportunity Opp:Allopps){
            if(Opp.Callback_Time__c != null && TempFirstSection.id == null){
                             
                   /*Integer Timediff1 = (Opp.Callback_Time__c.time().millisecond() - System.now().time().millisecond())/24/60;
                   //integer Timediff1 = (Opp.Callback_Time__c.timeGMT() - System.now().time())*24*60;
                   integer Timediff2 = (System.now().time().millisecond() - Opp.Callback_Time__c.time().millisecond())/24/60;
                    if(((Timediff1 <= 5) || (Timediff2 <= 15))){
                        system.debug('Opp name later'+Opp.Name);
                        Opp.Ownerid = userinfo.getuserid();
                        update Opp;
                        TempFirstSection = Opp; 
                    }*/
                    
                     if((System.now().time() <= Opp.Callback_Time__c.time() && System.now().time().addminutes(5) >= Opp.Callback_Time__c.time()) || (System.now().time() > Opp.Callback_Time__c.time() && System.now().time().addminutes(-15) <= Opp.Callback_Time__c.time())){
                         system.debug('Opp name'+Opp.Name);
                        TempFirstSection = Opp; 
                    }
             }
             
           if(Opp != TempFirstSection && Opp != Firstsection)  SecondSection.add(Opp);
           
           
           
               //if(Firstsection == null) Firstsection = Opp;
      } 
      
      system.debug('Temp section id'+TempFirstSection.Name);
      if(TempFirstSection.ID != null) FirstSection = TempFirstSection; 
      
      if(FirstSection == null && AllOpps.size() > 0) FirstSection = Allopps[0];   
      
      if(FirstSection == null){
       
        List<opportunity>  oldopps = [Select id,StageName, Name, Ownerid, Encounter_Time__c, Callback_Time__c, Account.Firstname, Account.Lastname, Opportunity_Status__c, Comments__c, Account.PersonEmail, Account.Home_Phone__pc, Account.Work_phone__pc, Account.PersonMobilePhone from Opportunity where (Ownerid =: userinfo.getuserid() 
                      OR Ownerid =: Commonuser) and StageName != 'Stale' and StageName != 'Complete' and Timestamp_Qual_Applicant__c = null and Callback_Time__c = null order by Createddate desc limit 11]; 
                      
          if(oldopps.size() > 0){
              FirstSection = oldopps[0];
          }            
       
       
      } 
      
     /* pagereference PR = new pagereference('/apex/OpportunityListview?id='+FirstSection.id);
      PR.setRedirect(true);
      
      
      return pr; */
      
      return null;
   } 
    
}