1

I have a pageblocktable, diplaying a list of records. Now i've added pagination using LIMIT- OFFSET in soql, inside the getter method of the list. Now when add() method inserts another record in the list, it does not getting reflected in the getter method. on the other hand if i add soql LIMIT- OFFSET elsewhere in the class, pagination is not working. Can anyone suggest any alternatives?? I want to display all the existing records as well as new records that is going to be cretaed from list, come under pagination. here is my code:

public class inline2{

//public Set<String>dupe_chk= new Set<String>();
public List<Competitor_Mapping__c>lstAcct1  = new List<Competitor_Mapping__c>();
public Boolean b{get;set;}
public Id Acc_Id {get;set;}
public Id Del_Id {get;set;}
public List<Competitor_Mapping__c>del_lst= new List<Competitor_Mapping__c>();
Public Account a{get;set;}
public integer index = 0;
public integer blockSize = 2;
public List<Competitor_Mapping__c> records=new List<Competitor_Mapping__c>();
public integer totalRecs=[select count() from Competitor_Mapping__c where Account__c=:ApexPages.currentPage().getParameters().get('id')];
//public Integer count = 1;
public List<SelectOption> getProductNames()
{
  List<SelectOption> options = new List<SelectOption>();
  List<Product_vod_c__c> productlist =[Select Id,Product_Name__c FROM Product_vod_c__c ];
  options.add(new SelectOption('--None--','--None--'));
  for (Integer j=0;j<productlist.size();j++)
  {
      options.add(new SelectOption(productlist[j].Product_Name__c,productlist[j].Product_Name__c));
  }
  return options;
}

public List<Competitor_Mapping__c> getrecords()
{
records=[select id,Competitor_Product__c,January__c,February__c,March__c,April__c,May__c,June__c,July__c,August__c,September__c,October__c,November__c,December__c from Competitor_Mapping__c where Account__c=:ApexPages.currentPage().getParameters().get('id')
LIMIT :blockSize OFFSET :index ];
return records;

}


    public void inline2()
    {  
    }
    public inline2(ApexPages.StandardController controller)
    {  
   // Acc_Id =ApexPages.currentPage().getParameters().get('id');
    records= [select id,Competitor_Product__c,January__c,February__c,March__c,April__c,May__c,June__c,July__c,August__c,September__c,October__c,November__c,December__c from Competitor_Mapping__c where Account__c=:ApexPages.currentPage().getParameters().get('id') ];
   // totalRecs=records.size();

} /*End Constructor*/
   public Pagereference Save()
    {
    Boolean okayToAdd = true;
    Set<String>dupe_chk= new Set<String>();

       lstAcct1.clear();
       dupe_chk.clear();
         for(Integer j = 0;j<records.size();j++)
        { //Boolean okayToAdd = true;
        System.debug('PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP'+ records[j].Competitor_Product__c);
        System.debug('KKKKKKKKKKKKKKKKKKKKKKKKKKK contents of s1: ' + dupe_chk); 
        System.debug('OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO: '  + dupe_chk.contains(records[j].Competitor_Product__c));

        if(records[j].Competitor_Product__c=='--None--')
        {  
            okayToAdd = false;
            System.debug('VVVVVVVVVVVVVVVVVVVVVVVVVVVVV : '+  records[j].Competitor_Product__c);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please select a competitor product!'));
        }

       if(!dupe_chk.contains(records[j].Competitor_Product__c))
       {   
           System.debug('1st dupe if ' + !dupe_chk.contains(records[j].Competitor_Product__c)); 
           dupe_chk.add(records[j].Competitor_Product__c);
           System.debug('KKKKKKKKKKKKKKKKKKKKKKKKKKK contents of s1: ' + dupe_chk); 
       }

       else
       {  
            System.debug('2nd dupe if ' + dupe_chk.contains(records[j].Competitor_Product__c)); 
           okayToAdd = false;
           System.debug('PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP : '+ records[j].Competitor_Product__c);
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'This Competitor Product Sales is already existing. Please Select another Product !'));
           continue;
       }

       if(okayToAdd)
       {
           lstAcct1.add(records[j]);
       }

    }
    System.debug('lllllllllllllllllllllllllllllllllllllllllllllllllllllll list: ' + lstAcct1); 
      if(lstAcct1.size()==records.size())
       {
           UpSert lstAcct1;
       }

//records= [select id,Competitor_Product__c,January__c,February__c,March__c,April__c,May__c,June__c,July__c,August__c,September__c,October__c,November__c,December__c from Competitor_Mapping__c where Account__c=:ApexPages.currentPage().getParameters().get('id') order by Competitor_Product__c];
 return null;
  }

  public void Add()
    {   

        addMore();      
    }

  public void AddMore()
    {
       //count = count+1;
       Competitor_Mapping__c newRec=new Competitor_Mapping__c (Account__c=Acc_Id);
 records.add(newRec);
 system.debug('@DN@'+records.size());

 //totalRecs=totalRecs+1;
}
    public void cancel()
    { records= [select id,Competitor_Product__c,January__c,February__c,March__c,April__c,May__c,June__c,July__c,August__c,September__c,October__c,November__c,December__c from Competitor_Mapping__c where Account__c=:ApexPages.currentPage().getParameters().get('id') ];
      //totalRecs=records.size();   
    }

       public void Del()
    {
    del_lst=[select id from Competitor_Mapping__c where Id =:del_id];
    Delete del_lst;

            records= [select id,Competitor_Product__c,January__c,February__c,March__c,April__c,May__c,June__c,July__c,August__c,September__c,October__c,November__c,December__c from Competitor_Mapping__c where Account__c=:ApexPages.currentPage().getParameters().get('id')  ];
 // totalRecs=records.size();
    }


    public void beginning()
    {
        index = 0;        
    }

    public void previous()
    {
        index = index - blockSize;
    }

    public void next()
    {
        index = index + blockSize;
    }

    public void end()
    {
        index = totalrecs - math.mod(totalRecs,blockSize);
    }        

    public boolean getprev()
    {
        if(index == 0)
        return true;
        else
        return false;
    }  

    public boolean getnxt()
    {
        if((index + blockSize) > totalRecs)
        return true;
        else
        return false;
    } 
}

page code :

<apex:page StandardController="Account" extensions="inline2"  id="thePage"> 
<!--apex:includeScript value="{!URLFOR($Resource.jQuery, '/jquery-1.9.1.min.js')}" /-->
 <apex:includeScript value="_https://code.jquery.com/jquery-2.1.1.min.js"/>
    <apex:form id="theForm"> 

  <apex:pageBlock id="thePageBlock"  > 
    <apex:pageMessages />
     <style>
            .headerStyle {
                font-size:17px;
               background-color:red;
            }
        </style>
      <apex:pageblockButtons location="bottom" > <apex:commandButton value="<<" rerender="thePageBlock" action="{!beginning}"  disabled="{!prev}"/>
            <apex:commandButton value="<" rerender="thePageBlock" action="{!previous}" disabled="{!prev}"/>
            <apex:commandButton value=">" rerender="thePageBlock" action="{!next}" disabled="{!nxt}"/>
            <apex:commandButton value=">>" rerender="thePageBlock" action="{!end}" disabled="{!nxt}"/>          
     </apex:pageblockButtons>    
 <apex:pageBlockSection id="section1" title="Competitor Sales" collapsible="true">
 <!--apex:actionFunction name="saveComplexity" action="{!save}" rerender="thePageBlock"/-->
  <apex:commandButton value="Save" action="{!save}"  id="saveButton"  reRender="thePageBlock" style="display: none;" rendered="{!records.size>0}" />

  <apex:commandButton action="{!cancel}" value="cancel" id="cancelButton" reRender="thePageBlock" style="display: none;" rendered="{!records.size>0}"/>

                 <!--apex:commandLink value="Save" action="{!save}" target="_parent" styleClass="btn" style="text-decoration:none;padding:4px;"  id="saveButton" /-->
                <!--apex:commandButton action="{!calledit}" value="edit" id="editButton" rendered="{!records.size>0}"/-->
                <!--apex:commandButton value="Save" action="{!save}" id="saveButton"  reRender="thePageBlock"  style="display: none;" rendered="{!records.size>0}"/-->
              <apex:commandButton value="Add New" action="{!Add}" id="AddMoreButton" reRender="thePageBlock"    />        
  <div align="center">    
            <apex:pageBlockTable value="{!records}" var="k" id="thePageBlockTable" rendered="{!records.size>0}" headerClass="headerStyle"  columnsWidth="60%,20%,20%,20%,20%,20%,20%,20%,20%,20%,20%,20%,20%"> 


               <apex:column headerValue="Competitor Product" style="border:1px solid;border-color:lightgray; font-size:13px;">
          <apex:selectList value="{!k.Competitor_Product__c}" multiselect="false" size="1" style="border-left:5px solid;border-left-color:red;" >
             <apex:selectOptions value="{!ProductNames}"/>
          </apex:selectList>

       </apex:column> 

                 <apex:column style="border:1px solid;border-color:lightgray; font-size:13px;" >
                    <apex:outputField value="{!k.January__c}" id="January" > 
                  <apex:inlineEditSupport event="ondblClick"   showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton" 
                       /> 
                       </apex:outputfield>
                    <apex:facet name="header">Jan</apex:facet>
                </apex:column>
               <apex:column style="border:1px solid;border-color:lightgray; font-size:13px;" >
                    <apex:outputField value="{!k.February__c}" id="February" >
                    <apex:inlineEditSupport event="ondblClick"   showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton" 
                       /> 
                       </apex:outputfield>  
                    <apex:facet name="header">Feb</apex:facet>
                </apex:column>
                <apex:column style="border:1px solid;border-color:lightgray; font-size:13px;" >
                    <apex:outputField value="{!k.March__c}" id="March" >
                    <apex:inlineEditSupport event="ondblClick"   showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
                       /> 
                       </apex:outputfield>  
                    <apex:facet name="header">Mar</apex:facet>
                </apex:column>

                <apex:column style="border:1px solid;border-color:lightgray; font-size:13px;" >
                    <apex:outputField value="{!k.April__c}" id="April" >
                    <apex:inlineEditSupport event="ondblClick"   showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
                       /> 
                       </apex:outputfield>  
                    <apex:facet name="header">Apr</apex:facet>
                </apex:column>

                <apex:column style="border:1px solid;border-color:lightgray; font-size:13px;" >
                    <apex:outputField value="{!k.May__c}" id="May" >
                    <apex:inlineEditSupport event="ondblClick"  showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
                       /> 
                       </apex:outputfield>  
                    <apex:facet name="header">May</apex:facet>
                </apex:column>

                <apex:column style="border:1px solid;border-color:lightgray; font-size:13px;" >
                    <apex:outputField value="{!k.June__c}" id="June" >
                    <apex:inlineEditSupport event="ondblClick"   showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
                       /> 
                       </apex:outputfield>  
                    <apex:facet name="header">Jun</apex:facet>
                </apex:column>

                <apex:column style="border:1px solid;border-color:lightgray; font-size:13px;" >
                    <apex:outputField value="{!k.July__c}" id="July" >
                    <apex:inlineEditSupport event="ondblClick"   showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
                       /> 
                       </apex:outputfield>  
                    <apex:facet name="header">Jul</apex:facet>
                </apex:column>

                <apex:column style="border:1px solid;border-color:lightgray; font-size:13px;" >
                    <apex:outputField value="{!k.August__c}" id="August" >
                    <apex:inlineEditSupport event="ondblClick"   showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
                       /> 
                       </apex:outputfield>  
                    <apex:facet name="header">Aug</apex:facet>
                </apex:column>

                <apex:column style="border:1px solid;border-color:lightgray; font-size:13px;" >
                    <apex:outputField value="{!k.September__c}" id="September" >
                    <apex:inlineEditSupport event="ondblClick"   showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
                       /> 
                       </apex:outputfield>  
                    <apex:facet name="header">Sep</apex:facet>
                </apex:column>

                <apex:column style="border:1px solid;border-color:lightgray; font-size:13px;" >
                    <apex:outputField value="{!k.October__c}" id="October" >
                    <apex:inlineEditSupport event="ondblClick"   showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
                       /> 
                       </apex:outputfield>  
                    <apex:facet name="header">Oct</apex:facet>
                </apex:column>

                <apex:column style="border:1px solid;border-color:lightgray; font-size:13px;" >
                    <apex:outputField value="{!k.November__c}" id="November" >
                    <apex:inlineEditSupport event="ondblClick"   showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton"
                       /> 
                       </apex:outputfield>  
                    <apex:facet name="header">Nov</apex:facet>
                </apex:column>

                <apex:column style="border:1px solid;border-color:lightgray; font-size:13px;" >
                    <apex:outputField value="{!k.December__c}" id="December" >
                    <apex:inlineEditSupport event="ondblClick"  showOnEdit="saveButton,cancelButton" hideOnEdit="AddMoreButton" rendered=""
                       /> 
                       </apex:outputfield>  
                    <apex:facet name="header">Dec</apex:facet>
                </apex:column>
                 <apex:column >
                 <apex:commandButton value="X" action="{!Del}" rerender="thePageBlock" rendered="{!IF(ISNULL(k.Id) , false , true)}">
                  <apex:param name="rowToBeDeleted" value="{!k.Id}" assignTo="{!del_Id}"></apex:param>
                 </apex:commandButton>
                  </apex:column>       

            </apex:pageBlockTable> 

     </div>

      </apex:pageBlockSection>
        </apex:pageBlock>

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

1 Answer 1

3

Your Add/AddMore method appends the new object to the records list but when the re-render causes getrecords to be called you overwrite the records list with the objects queried from the database which will not include the new object(s).

A quick work-around for that would be this that preserves the new objects (the ones without IDs):

public List<Competitor_Mapping__c> getrecords() {
    List<Competitor_Mapping__c> newRecords = new List<Competitor_Mapping__c>();
    for (Competitor_Mapping__c r : records) {
        if (r.Id == null) {
            newRecords.add(r);
        }
    }
    records = [select ...];
    if (...) {
        records.addAll(newRecords);
    }
    return records;
}

But you will need to consider the pagination in this i.e. do you want the new records to be appended to the last page only and you will need to take care that when multiple new objects have been added they can overflow into further pages.

1
  • Thanks Keith C ..The new records needs to appended at the begining i.e. records.add(0,newRec); and yes if there are multiple new objects added they can overflow into further pages.
    – user59759
    Dec 22, 2014 at 10:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .