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
Ian_CIan_C 

How to create a record and redirect to VF page that allows additional child records?

I would like to have a page that allows for an Account creation. When user hits Save it'll redirect them to another VF page where they can add a contact, set an activity, and create a custom object record (note). The part I can't figure out is how to provide the id of the new account record to the redirected VF page. Any help?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka
public class YOURPARENTPAGECONTROLLER
{
	public PageReference YourInsertMethod()
	{
		insert account;

		PageReference pg=Page.YOURREDIRECTPAGE;

		pg.getParameters().put('id',account.id);

		pg.setRedirect(true);

		return pg;
	}

}

public class YOURREDIRECTPAGECONTROLLER
{
	public String AccountId{get;set;}
	public YOURREDIRECTPAGECONTROLLER()
	{
		AccountId = Apexpages.currentPage().getParameters().get('id');
	}
	
	public void InsertContact()
	{
		Contact newCon = new Contact();
		
		newCon.Account = AccountId;
		//your other field value setting here or bind from the page
		insert newCon;
	}

}

 

Use these methods to resolve your requirement.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

All Answers

Dev@Force.ax647Dev@Force.ax647

After you insert you can get Id of account and pass to new page e.g

 

insert account;

PageReference pr=Page.NewPage;

pr.getParameters().put('AccountID',account.id);

pr.setRedirect(true);

return pr;

 

 

 

Chamil MadusankaChamil Madusanka
public class YOURPARENTPAGECONTROLLER
{
	public PageReference YourInsertMethod()
	{
		insert account;

		PageReference pg=Page.YOURREDIRECTPAGE;

		pg.getParameters().put('id',account.id);

		pg.setRedirect(true);

		return pg;
	}

}

public class YOURREDIRECTPAGECONTROLLER
{
	public String AccountId{get;set;}
	public YOURREDIRECTPAGECONTROLLER()
	{
		AccountId = Apexpages.currentPage().getParameters().get('id');
	}
	
	public void InsertContact()
	{
		Contact newCon = new Contact();
		
		newCon.Account = AccountId;
		//your other field value setting here or bind from the page
		insert newCon;
	}

}

 

Use these methods to resolve your requirement.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

This was selected as the best answer