+ Start a Discussion
Kota Uday 6Kota Uday 6 

swapping 2 records

Hi every one,

I want to swap my records using button as action ,

Can any one help me with code.
Shrikant BagalShrikant Bagal
Please refer following example:

\Visualforce Page
<apex:page controller="swapcls" id="p">
    <apex:form id="f">
        <apex:pageBlock id="pb" >
            <div align="center">       
            <apex:commandButton value="Swap" action="{!swapmethod}"/>
            <apex:pageMessage summary="Select any Two Records" severity="warning" strength="3" rendered="{!pgmsg}" />
            <apex:pageblocktable value="{!lw}" var="ll" id="pbt" >
                <apex:column headerValue="Action" width="80px" id="c">
                    <apex:inputCheckbox value="{!ll.selected}" id="icb"/>
                <apex:column value="{!ll.num}" headerValue="Index" width="100px"/>
                <apex:column value="{!ll.name.name}"/>
               <apex:column value="{!ll.name.rating}"/>
                <apex:column value="{!ll.name.phone}"/>

global with sharing class swapcls {

    public boolean pgmsg { get; set; }
    public PageReference swapmethod() {
     List<Integer> li=new List<Integer>();
    List<Account> lstacc=new List<Account>();
     la=new List<Account>();
         for(integer i=0; lw.size()>i;i++){
               lw.add(new wrapper(lstacc[0],li[1],false));
               lw.add(new wrapper(lstacc[1],li[0],false));
                 return null;
    List<Account> la;
    public List<wrapper> lw{get; set;}
    public swapcls(){
        lw=new List<wrapper>();
        la=[select id,name,rating,phone from Account limit 50];
        for(integer i=0; la.size()>i;i++){          
            lw.add(new wrapper(la[i],i,false));

    global class wrapper implements comparable{
        public Account name{get; set;}
        public boolean selected{get; set;}
        public integer num{get; set;}
        public wrapper(Account n,integer nbr,boolean c){
    global Integer compareTo(Object compareTo) {
        wrapper compareTolw = (wrapper)compareTo;
        if (num == compareTolw.num) return 0;
        if (num > compareTolw.num) return 1;
        return -1;        

If its helps, please mark as best answer so it will help to other who will serve same problem.
Kota Uday 6Kota Uday 6
Hi Shirkant,

I have button for each records in my pageblock section,what i want to do is while i press down button my record should come down and down record should take up, help me in that

NOTE: each record have button UP & down ,  
Shrikant BagalShrikant Bagal
if you are using List of record in repeat tag in VF then you can swap the Index records in List?

Kota Uday 6Kota Uday 6
Hi Shikant it only working when we select 2 records,But i need without selecting records i want to place a button on rech record when i press down buttion it should come down ,same operation on up too,can you please help me out, 
Shrikant BagalShrikant Bagal
Please post your VF and Ctrl Code:

Suraj Tripathi 47Suraj Tripathi 47

Hi Kota Uday 6

I am Writting a Program of Swap two or more fields please check it

public static void swap()
object_name ob= new object_Name();
ob=[select id ,name from object_name];

String s= null;
update ob;

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Suraj Tripathi