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
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>
<apex:column value="{!w.name.name}"/>
</apex:pageblockTable>
<apex:commandButton value="swap" action="{!swaprec}" />
<apex:pageMessage summary="Select any Two Records" severity="warning" strength="3" rendered="{!pgmsg}" />
<apex:pageMessages />
</apex:pageblock>
</apex:form>
</apex:page>

 

 

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)
             lstswap.add(s.name);
             }
           System.debug('------------------>'+lstswap);
  if(lstswap.size()>2 || lstswap.size()==1)
 {
 pgmsg = true;
 }
else
{
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));
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

Hi,

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

 

Thanks,

Anoop

 

 

Suraj Tripathi 47Suraj Tripathi 47
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