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
Paul.FoxPaul.Fox 

How to create new record when using controller extensions and Save and New

I have a custom object with a list of related objects and I'm using a controller extension to create the object and apply a list of related objects to it. Everything is working correctly for the normal save functionality.

 

However, when I try and create a Save and New button, it is not creating a new object, it is merely opening the same object again. How do I create a new object so it won't save over the existing one?

 

The page has the oppid as a parameter, and calls the saveAndNew action.

 

Here are the relevant sections from the class (I think):

public with sharing class InvoiceExt {
	public STT_Invoice__c i {get;set;}
	Id OppId = ApexPages.currentPage().getParameters().get('oppid');

// Constructor to extend the class
	public InvoiceExt(ApexPages.StandardController c){
		controller = c;
		i = (STT_Invoice__c) c.getRecord();
		
		i.Opportunity__c = OppId;
		
		Opportunity opp = [Select Account.Finance_Contact__c, AccountId From Opportunity where Id = :i.Opportunity__c limit 1];
		i.Recipient__c = opp.Account.Finance_Contact__c;
		i.Account__c = opp.AccountId;
		i.Estimated_Send_Date__c = date.today();
		i.Payment_Terms__c = '15 Days';
	}

public PageReference saveAndNew(){
		
		PageReference p = Page.NewInvoice;
		p.getParameters().put('oppid',OppId);
		controller.save();
		
		// Get the new Invoice Id
		InvoiceId = controller.getId();
		

		i = new STT_Invoice__c();
		p.getParameters().put('id',null);
		return p;
	}
	
	/* The standard controller object which will be used later for navigation and to invoke
       it's save action method to create the new Quote. */
    private ApexPages.StandardController controller;