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
Aam MAam M 

Save & New Button

In custom "Save and New" button  I've to fetch the same parent from Lookup in every record when we click the save & new button.
How can we achive this ? Please reply ASAP
User-added image

Like in this pic every new record same Account should be selected when we select for the first record!! 


Anyone Please reply ASAP.
It will be very helpful for us.

 
Best Answer chosen by Aam M
Varun SinghVarun Singh
Hi @Mujahid Ahmed Aamir M

 
ApexPages.addMessages(e);
instead of ApexPages.addMessage() (plural method instead of the singular method).
 
Add Message will only spit out one error messages, while Add Messages will spit out all the errors in the exception.
 
Some additions to this code that might help anybody else who stumbles on here would be to make sure your Save & New action is passing along any pre-filled values on to the next Create page.  This is important when createing detail objects, so that the Master field is already filled in.
 
My code kinda looks like this...
 
/*** PROPERTIES ***/
private ApexPages.StandardController myStdController;
public String queryString {get;set;}

/*** CONSTRUCTOR ***/
public vf_Child_Object_Create(ApexPages.StandardController stdController) {
    myStdController = stdController;
    PageReference thisPage = ApexPages.currentPage();
    List<String> url = thisPage.getUrl().split('\\?');
    queryString = url[1];
}

/*** ACTIONS ***/
public PageReference saveandnew(){
    try {
        myStdController.save();
        
        Schema.DescribeSObjectResult describeResult = myStdController.getRecord().getSObjectType().getDescribe();
        
        PageReference pr = new PageReference('/' + describeResult.getKeyPrefix() + '/e?' + queryString);
        pr.setRedirect(true);
        return pr;  

    } catch(System.DMLException e) {
        // Note: not spitting out any error messages, as SF seems to be handling the errors fine, at least for my simple object; your mileage my vary.
        return null;
    }
}

The querystring that I read during the contructor phase should remain untouched by your processes.  SF will dump in other variables into the querystring, keeping track of the view state, etc. which you don't want.
 
I might be missing something else here, but so far this works for my use cases.
 
Good luck!
 

All Answers

Varun SinghVarun Singh
Hi @Mujahid Ahmed Aamir M

 
ApexPages.addMessages(e);
instead of ApexPages.addMessage() (plural method instead of the singular method).
 
Add Message will only spit out one error messages, while Add Messages will spit out all the errors in the exception.
 
Some additions to this code that might help anybody else who stumbles on here would be to make sure your Save & New action is passing along any pre-filled values on to the next Create page.  This is important when createing detail objects, so that the Master field is already filled in.
 
My code kinda looks like this...
 
/*** PROPERTIES ***/
private ApexPages.StandardController myStdController;
public String queryString {get;set;}

/*** CONSTRUCTOR ***/
public vf_Child_Object_Create(ApexPages.StandardController stdController) {
    myStdController = stdController;
    PageReference thisPage = ApexPages.currentPage();
    List<String> url = thisPage.getUrl().split('\\?');
    queryString = url[1];
}

/*** ACTIONS ***/
public PageReference saveandnew(){
    try {
        myStdController.save();
        
        Schema.DescribeSObjectResult describeResult = myStdController.getRecord().getSObjectType().getDescribe();
        
        PageReference pr = new PageReference('/' + describeResult.getKeyPrefix() + '/e?' + queryString);
        pr.setRedirect(true);
        return pr;  

    } catch(System.DMLException e) {
        // Note: not spitting out any error messages, as SF seems to be handling the errors fine, at least for my simple object; your mileage my vary.
        return null;
    }
}

The querystring that I read during the contructor phase should remain untouched by your processes.  SF will dump in other variables into the querystring, keeping track of the view state, etc. which you don't want.
 
I might be missing something else here, but so far this works for my use cases.
 
Good luck!
 
This was selected as the best answer
Varun SinghVarun Singh
I have another  working example
 
Visualforce page:

<apex:page standardController="Contact" extensions="Sample">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}"/>
            <apex:commandButton value="Save & New" action="{!saveAndNew}"/>
            <apex:commandButton value="Cancel" action="{!cancel}"/>
        </apex:pageBlockButtons>
            <apex:pageBlockSection >
                <apex:inputField value="{!Contact.FirstName}"/>
                <apex:inputField value="{!Contact.LastName}"/>
                <apex:inputField value="{!Contact.Email}"/>
            </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>
 
public class Sample {
    ApexPages.StandardController sController;
    public Sample(ApexPages.StandardController controller) {
        sController = controller;
    }

    public PageReference saveAndNew() {
        sController.save(); 
        PageReference pg = new PageReference('/apex/Sample');
        pg.setRedirect(true);
        return pg;
    }
}

Instead of Contact use you custom controller and  in vf page use all required fileds for your custom object like look and others
Aam MAam M
@Varun Sing... Please give me your email id. I'll communicate with you in detail
 
Varun SinghVarun Singh
Hi @Mujahid Ahmed Aamir M

my Email id is-spnvarun0121@gmail.com
NatsuNatsu
1. You can use extention.
2. When user clicks on the custom button add parameters to the url passing the parent lookupid. eg: 'apex/page?parentid='+parentid
3. In the constructor check if accountid exists ApexPages.getCurrentPage().getParameters().get('parentid') initialise your child object variable with the parentid 
Aam MAam M
@Varun please check your email.