+ Start a Discussion
learning1.3953795511514497E12learning1.3953795511514497E12 

override the standard save button in salesforce

I have a requirement where i need to customize the Account's "save" button in VF page. The save button should do the following.

1 : Save the record and go to that particular record's view page. (This is standard functionality)
2 : Get the record id from the url in addressbar.

I need the record id to create related list records for this particular Account.

OR

Is there any way to create the Account record and its child records at a time?

I should not create the related list records manually since the data is coming from external entity.I just need to dump that data to the related list .So I have to go through the above method only.

I have tried the below code but no luck...

In VF page
----------------
<apex:commandButton value="Save" action="{!SaveAndCreteLocations}"/>

In APex controller :
-------------------------------
public pageReference SaveAndCreteLocations()
    {
         pageReference pv = controller.view(); 
         controller.save();
         pv.setRedirect(true);
         return pv;  

    }


In the above code the record is saving but not going to the view page.
Please help...

Best Answer chosen by learning1.3953795511514497E12
Vinit_KumarVinit_Kumar
As per your post,you are talking about 'Save' button on VF page,not on Standard pages.That can be done ,also you can insert child reords as well,you just need to modify your conttroller mthod like below :-

public pageReference SaveAndCreteLocations()
    {
		 insert Account; // Assuming we are inserting account recod
		 
		 // Insert child record Contact
		 
		 Contact con = new Contact(LastName ='Kumar',FirstName='Vinit',AccountId=Account.id);//Populate all the required fields to insert a Contact record
		 insert con;
		 
         pageReference pv = new pagereference('/' + Account.id); // redirecting it to record view page
        // controller.save();
         pv.setRedirect(true);
         return pv;  

    }

If this helps,please mark this as best answer to help others :)

All Answers

Vinit_KumarVinit_Kumar
As per your post,you are talking about 'Save' button on VF page,not on Standard pages.That can be done ,also you can insert child reords as well,you just need to modify your conttroller mthod like below :-

public pageReference SaveAndCreteLocations()
    {
		 insert Account; // Assuming we are inserting account recod
		 
		 // Insert child record Contact
		 
		 Contact con = new Contact(LastName ='Kumar',FirstName='Vinit',AccountId=Account.id);//Populate all the required fields to insert a Contact record
		 insert con;
		 
         pageReference pv = new pagereference('/' + Account.id); // redirecting it to record view page
        // controller.save();
         pv.setRedirect(true);
         return pv;  

    }

If this helps,please mark this as best answer to help others :)
This was selected as the best answer
learning1.3953795511514497E12learning1.3953795511514497E12
Hi Vinit_Kumar,

Thanku soo much... :)
It worked ...