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
SF7SF7 

Save and New - redirect to a VF along with Parent ID in Visual force page

<apex:page standardController="Client_Site__c" extensions="AddClientSupplier,ClientSiteExt" tabStyle="Client_Site__c">
<apex:form id="myForm" >

<apex:sectionHeader title="New Client Site" />
<apex:pageBlock title=" Client Site Edit" mode="edit">

<apex:pageBlockButtons location="top" >
                        <apex:commandButton value="Save" action="{!saveClientSite}" />
                 <!--       <apex:commandButton value="Save & New" action="{!saveandnew1}" /> -->
                          <apex:commandButton value="Cancel" action="{!Cancel}"/>
</apex:pageBlockButtons>
</Apex:Page>
 
public class AddClientSupplier {



    ApexPages.StandardController sc;      
     public Client_Discovery__c myCustomObject;
    public Client_Site__c acct{get;set;}
    public Integer marker=2;
    public Integer selectedClientSupplier{get;set;}
    public List<WrapperClass> lClientSuppliers{get;set;}
    public String queryString {get;set;}

    
    public AddClientSupplier(ApexPages.StandardController controller) {
        this.acct = (Client_Site__c)controller.getRecord();
        sc=controller;
        lClientSuppliers=new List<WrapperClass>();
        Client_Supplier__c c=new Client_Supplier__c();
        lClientSuppliers.add(new WrapperClass(c,1));
    }
    public PageReference deleteClientSupplier(){
        Integer x=-1;
        for(WrapperClass wc:lClientSuppliers){
            x++;
            if(wc.counter==selectedClientSupplier){
                System.debug('-->selected ClientSupplier:'+selectedClientSupplier+'  position:'+x);
                break;
            }
        }
       lClientSuppliers.remove(x);
        return null;
    }
    public PageReference saveClientSite(){
        Database.SaveResult sr = Database.insert(acct, false);
        Id idey=sr.getId();
        List<Client_Supplier__c> ClientSupplierList=new List<Client_Supplier__c>();
        
        for(WrapperClass wc:lClientSuppliers){
        
        if(!string.isblank(wc.c.Supplier_Name__c) ){

                      
        Client_Supplier__c c=new Client_Supplier__c();
        c.Is_This_a_New_or_Incumbent_Supplier__c=wc.c.Is_This_a_New_or_Incumbent_Supplier__c;
        c.Skill_Type__c=wc.c.Skill_Type__c;
        c.Supplier_Name__c=wc.c.Supplier_Name__c;
        c.Will_Manpower_Manage_This_Supplier__c=wc.c.Will_Manpower_Manage_This_Supplier__c;     
        c.Client_Site__c=idey;
        ClientSupplierList.add(c);
        }
        }
        insert ClientSupplierList;
        
        
        
        return(new PageReference('/'+sr.id).setRedirect(True));       
       // return new PageReference('/' + Schema.getGlobalDescribe().get('Client_Discovery__c').getDescribe().getKeyPrefix() + '/o');

    }
    public PageReference addAClientSupplier(){
        
        Client_Supplier__c c=new Client_Supplier__c();
        lClientSuppliers.add(new WrapperClass(c,marker));        
        marker=marker+1;
        return null;
    }
    public class WrapperClass{
        public Integer counter{get;set;}
        public Client_Supplier__c c{get;set;}
        public WrapperClass(Client_Supplier__c cntc,Integer i){
            this.c=cntc;
            this.counter=i;
        }
    }
  /*  public PageReference saveandnew(){
    try {
        sc.save();
        
        Schema.DescribeSObjectResult describeResult = sc.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;
    }
}*/
}
 
public with sharing class ClientSiteExt {

    public ClientSiteExt(ApexPages.StandardController controller) {
        Client_Site__c c = (Client_Site__c) controller.getRecord();
        c.Client_Discovery__c  = ApexPages.currentPage().getParameters().get('Client_Discovery__c');
    }

}
Hi ,

I need a Custom Save and New button which when hit need to direct me to the same VF page which i just created the record on . pb is i am unable have the parent ID for the LookUp field.
Best Answer chosen by SF7
Alexander TsitsuraAlexander Tsitsura
Hi, thanks, it help. I write comment there

All Answers

Alexander TsitsuraAlexander Tsitsura
Hi Akhil,

Please see code below
public PageReference saveAndNew() {
  sc.save();

  PageReference pRef = Page.YOUR_PAGE_NAME;
  pRef.setRedirect(true);
  pRef.getParameters().put('parentId', 'YOUR_PARENT_ID');
  return pRef;
}

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
SF7SF7
Hi @alexander 

I am able to redirect to the Right Visual force i need when i hit the Save and New buttonn. Only Pb is not populated the parent ID. Here my parent Object os Client_Discovery__C

public PageReference saveAndNew() {
  sc.save();

  PageReference pRef = Page.ClientSite;
  pRef.setRedirect(true);
  pRef.getParameters().put('Client_Discovery__c', 'myCustomObject');
  return pRef;
}
Alexander TsitsuraAlexander Tsitsura
Hi, can you please explain aboud Pb?. I do not understand what this means.
SF7SF7
Hi @Alexander

Coulds you please Check it out here https://developer.salesforce.com/forums?id=906F0000000BYpjIAG i tried to explain in Screen Shots


https://developer.salesforce.com/forums?id=906F0000000BYpjIAG
 
Alexander TsitsuraAlexander Tsitsura
Hi, thanks, it help. I write comment there
This was selected as the best answer