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
Jesse WolffJesse Wolff 

Custom Object Save and New with Parameters on Custom Object

Hello - I have a custom object which when created, my users need to enter multiple child objects to.  Currently there is a custom visualforce form which loads parameters into the URL to populate 2 mandatory fields on the child object, all of which come from the parent custom object.

The URL looks like this:
https://..../apex/myVFpage?Work_Order__c=a3To00000002Voj&Invoice__c=a3O1N000003WBkB

Once the information is entered, the user clicks "Save" and standard controller save action is used which returns them to the page for the newly created child record.

I have created a controller extension to execute a "Save and New", which works great, but does not populate the required fields.  I need to grab the URL as listed above, and reload it for the "New" portion of the action.  Below is my code:

public class saveAndnew {

    public ApexPages.StandardController scMain{get;set;}
        public saveAndnew(ApexPages.StandardController controller) {
        scMain = controller;
        }

    public PageReference saveAndNew() {
    scMain.save();
    PageReference newInvoiceLinefromInv= new PageReference('/apex/newInvoiceLinefromInv');
    newInvoiceLinefromInv.setRedirect(true);
    return newInvoiceLinefromInv;
    }
}

I'm certain there's a way to do this, but I'm spending way too much time on this and coming up empty.  Any snippits of wisdom for me?

Thanks,
Best Answer chosen by Jesse Wolff
Jesse WolffJesse Wolff
Update:  I have been able to piece together a resolution to this problem.  Additionally to the Save and New functionality, I am returning to the parent object after Save and Quit.  Now if anyone can suggest how to write the test class for this, I'd love some snippets!
 
public class saveAndnew {

    public ApexPages.StandardController scMain{get;set;}
        public saveAndnew(ApexPages.StandardController controller) {
        scMain = controller;
        }

    public String reload {get; set;}
    {
        reload = URL.getCurrentRequestUrl().toExternalForm();
    }

    public PageReference saveAndNew() {
    scMain.save();
    PageReference newInvoiceLinefromInv= new PageReference(reload);
    newInvoiceLinefromInv.setRedirect(true);
    return newInvoiceLinefromInv;
    }
    
    public String goback {get; set;}
    {
        goback = ApexPages.currentPage().getParameters().get('retURL');
    }

    public PageReference saveAndfinito() {
    scMain.save();
    PageReference invedit= new PageReference(goback);
    invedit.setRedirect(true);
    return invedit;
    }
}



 

All Answers

Jesse WolffJesse Wolff
Follow up - below is the VisualForce Page related to this:

<apex:page standardController="Invoice_Line__c" extensions="DefaultFieldExtension,saveAndnew">
    
    <apex:form >
    
    <apex:pageBlock mode="detail" id="items" title="Billing Entry">
        <apex:pageMessages />
        
                // Pageblock stuff....
                
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save" />
            <apex:commandButton action="{!quicksave}" value="Get Price" />
            <apex:commandButton action="{!saveAndnew}" value="Save and New"/>
        </apex:pageBlockButtons>
                
    </apex:pageBlock>
 
    </apex:form>
    
</apex:page>
Jesse WolffJesse Wolff
Update:  I have been able to piece together a resolution to this problem.  Additionally to the Save and New functionality, I am returning to the parent object after Save and Quit.  Now if anyone can suggest how to write the test class for this, I'd love some snippets!
 
public class saveAndnew {

    public ApexPages.StandardController scMain{get;set;}
        public saveAndnew(ApexPages.StandardController controller) {
        scMain = controller;
        }

    public String reload {get; set;}
    {
        reload = URL.getCurrentRequestUrl().toExternalForm();
    }

    public PageReference saveAndNew() {
    scMain.save();
    PageReference newInvoiceLinefromInv= new PageReference(reload);
    newInvoiceLinefromInv.setRedirect(true);
    return newInvoiceLinefromInv;
    }
    
    public String goback {get; set;}
    {
        goback = ApexPages.currentPage().getParameters().get('retURL');
    }

    public PageReference saveAndfinito() {
    scMain.save();
    PageReference invedit= new PageReference(goback);
    invedit.setRedirect(true);
    return invedit;
    }
}



 
This was selected as the best answer