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
RemediosRemedios 

How to swap values of two or more fields?

We have three fields for email addresses. When we send email campaigns or do email sync only the default email address field is used. My users would like to have the option with a click of a button to swap the swap the address in the default email address field with the one from the alternative email address field. What would be the best way to achieve that?

Thank you!

bob_buzzardbob_buzzard

If it needs to be done at the click of a button, one way would be to tie that button to a visualforce page that contains a page action attribute.  This will cause an action method to be executed prior to the page being rendered.  This method can swap the values, update the record via DML and then return the pagereference to view the updated record.

RemediosRemedios

Thank you for your prompt response. Is it difficult to set up this? I am new to visualforce. Do I need to create a field where to keep the values during the exchange? If you can help me with more specific steps will be very gratefull - or refer me a training source?

bob_buzzardbob_buzzard

It should be fairly straightforward.  Assuming your sobject is of type 'MyObject__c' and the two fields to swap are strings named 'MyField1__c' and 'MyField2__c', then you'd have a page something like:

 

<apex:page standardController='MyObject__c' extensions='MyController' action='{!swapFields}>
</apex:page>

 

and a controller something like:

 

public class MyController
{
   private ApexPages.StandardController stdCtrl;

   public MyController(ApexPages.StandardController inStd)
   {
      stdCtrl=inStr;
   }

   public PageReference swapFields()
   {
      MyObject__c sobj= (MyObject__c) stdCtrl.getRecord();
      String tmpStr=sobj.MyField1__c;
      sobj.MyField1__c=sobj.MyField2__c;
      sobj.MyField2__c=tmpStr;
      update sobj;

      return stdCtrl.view();
   }
}

 You should then be able to create a record detail button with a target of the visualforce page.

 

 

 

Suraj Tripathi 47Suraj Tripathi 47

Hi Remedios

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