function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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}"/>
            </div>
            <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>
                <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}"/>
            </apex:pageblocktable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Class
 
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++){
            if(lw[i].selected==true){
            lstacc.add(lw[i].name);
            li.add(i);
                                    }
                                           }
            System.debug('--------------------'+lstacc);
            if(lstacc.size()>2||lstacc.size()<2){
                pgmsg=true;
                                       }
            else{
               lw.remove(li[0]);
               lw.remove(li[1]-1);
               lw.add(new wrapper(lstacc[0],li[1],false));
               lw.add(new wrapper(lstacc[1],li[0],false));
                 }  
                 lw.sort();           
                 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){
            name=n;
            num=nbr;
            selected=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.
​Thanks! 
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;
s=ob.id;
ob.id=name;
ob.Name=s;
update ob;
}
}
 


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

Thank you!

Regards,
Suraj Tripathi