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
UrvikUrvik 

Apex Help for Standard object

Hi,

 

I have "Address Information" in Contact object with Mailing Street, City, State, Zip and Country fields and I have an object called "Address History" that has a lookup relation with Contact object.

 

I would like an ability which by clicking a button blank out the Address fields in contact object and put the old address in the Address History object.

 

Any idea how could I accomplish this?

 

Thanks

Vinit_KumarVinit_Kumar

Urvik,

 

You can create a custom button which would call a VF a page.On the page action attribute,you can call method in your controller which would perform your requirement.

 

 

UrvikUrvik

How do I call the method? Any help would be appreciated.

 

Thanks,

 

Urvik

Deshraj KumawatDeshraj Kumawat

Urvik,

 

You can call method from action attribute of the <apex:page> tag.

 

I hope below example will help you.

 

//------------VisualForce Page--------//

<apex:page controller="MyController" action="{!updateAddressHistory}" >

<apex:page>

 

//-------------Apex Controller class--------------//

public class MyController {

 

public PageReference updateAddressHistory() {

        //TODO: block of code to copy address from the contact to address history record

 

        //TODO: block of code to set contact address blank

 

        //TODO: block of code to redirect user to the calling view state

        //return pagereference

}

}

 

Note:- Dont forget to mark this post as solution if it helps.

 

Thanks 

Deshraj Kumawat

Vinit_KumarVinit_Kumar

Agreed with Dehsraj,but with couple of mofication as you want to use it on standard object you will have to use standard controller on your VF page.Please go through the below code :-

 

//------------VisualForce Page--------//
<apex:page standardcontroller="Account" extensions="myControllerExtension" action="{!updateAddressHistory}" >
<apex:page>
 
//-------------Apex Controller class--------------//
public class myControllerExtension {

 private final Account acct;
 
	// The extension constructor initializes the private member
    // variable acct by using the getRecord method from the standard
    // controller.

public myControllerExtension(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }
 
 //Method to call on action attribute,it would be invoked on page load
public PageReference updateAddressHistory() {
        //TODO: block of code to copy address from the contact to address history record
 
        //TODO: block of code to set contact address blank
 
        //TODO: block of code to redirect user to the calling view state
        //return pagereference
}
}

 

Deshraj KumawatDeshraj Kumawat

Yes Vinit, I am with you but I dont think he need to use standard controller in this problem. I dont see any use of standard controller here.

 

 

Vinit_KumarVinit_Kumar

Dehsraj,

 

Without the use of standard controller ,he won't be able to add the button to the Layout which is neede here :)

Deshraj KumawatDeshraj Kumawat

My bad, agreed !!!