+ Start a Discussion
smith indiasmith india 

how to swap 2 selected records ?

this program is not swapping the records ..


<apex:page controller="swapcls" >
<apex:form >
<apex:pageblock >
<apex:pageblockTable value="{!wrap}" var="w">
<apex:column >
<apex:selectCheckboxes value="{!w.check}"/>
<apex:column value="{!w.name.name}"/>
<apex:commandButton value="swap" action="{!swaprec}" />
<apex:pageMessage summary="Select any Two Records" severity="warning" strength="3" rendered="{!pgmsg}" />
<apex:pageMessages />



public with sharing class swapcls

public boolean pgmsg { get; set; }

public PageReference swaprec()
             list<swap_record__c> lstswap = new list<swap_record__c>();
             for(wrapper s:wrap)
             if(s.check == true)
  if(lstswap.size()>2 || lstswap.size()==1)
 pgmsg = true;
swap_record__c a;
a = lstswap[0];
lstswap[0] = lstswap[1];
lstswap[1] = a;

pagereference ref = new pagereference('/apex/swaprecords');
return ref;

public list<wrapper> wrap { get; set; }

public class wrapper
public boolean check{get; set;}
public swap_record__c name{get; set;}
public integer num{get;set;}
public wrapper(boolean b, swap_record__c n, integer nbr)
check = b;
name = n;
num = nbr;
public swapcls()
wrap = new list<wrapper>();
list<swap_record__c> lst = [select name from swap_record__c];

integer i=0;
for(swap_record__C c:lst)
wrap.add(new wrapper(false,c,i));

Suresh RaghuramSuresh Raghuram

here I am having a question when you are swapping a record 1 with record2, how does it recognises that you are sending record1.name to record2.name 


so you should handle it with their ids.


smith indiasmith india

in the vf page i have displayed record names with check boxes . so after selecting 2 records those should be swapped .

here i want to give position index for records .

so after selecting the records if we change position index those records will be swapped right .

but i'm not getting how to do this ...

Anoop AsokAnoop Asok


You're performing swap in the local list lstswap. I believe you should be doing it on your public list wrap.