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- Need to redirect to Same VF Page with parent ID

Hi ,
I have a Visual force page and Need a Custom Save and New Functionality,

Opportunity (Parent Object)        -------      Client_Discovery__C (Child Object)   , 
Client_Discovery__C (Parent Object) ----- Client_Site__C (Child Object)
Client_Site__C (Parent Object) ------ Client Supplier (Child Object)

What i am trying to achive with my visual force page is , On Opportunity related list i have New Client Disovery Button withich is Standard  Visual Page.
User-added image
When user Clicks New Client Disocvery and enters data
User-added image
and hits save page is Redirected to new Visual Force page where users can create Client Site and Multipole Supplier at the Same time .

User-added image

Here when user finishes enetring data He needs to hit Save & New and again go to the same VF page where he just hit save & new . I am able to redirect to that page but missing the Parent ID.

First Vf Page Standard page ClientQuestionarrie with Custom Save method in Client Questionarrie Controller.
public with sharing class ClientQuestionarrie {

    Apexpages.StandardController controller;
    public Client_Discovery__c myCustomObject;
    public ClientQuestionarrie(ApexPages.StandardController sc)
    { 
    this.controller = sc;
    myCustomObject = (Client_Discovery__c) sc.getRecord();  
    }

    public PageReference SaveQuestionarrie()
    {
          upsert myCustomObject;
          Pagereference ref = new Pagereference('/Apex/ClientSite');
          ref.getParameters().put('Client_Discovery__c', myCustomObject.Id);
          ref.setRedirect(true);
          return ref;
    }
  
  
}
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');
    }

}

<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="{!SaveAndNew}"/>
            <apex:commandButton value="Cancel" action="{!Cancel}"/>
</apex:pageBlockButtons>

<apex:pageBlockSection title="Information" columns="2">
  
    <apex:inputField value="{!Client_Site__c.Client_Site_Name__c}" taborderhint="1"/>
    <apex:inputField value="{!Client_Site__c.Client_Discovery__c}" taborderhint="6"/>
    <apex:inputField value="{!Client_Site__c.City__c}" taborderhint="2"/>
    <apex:inputField value="{!Client_Site__c.Number_of_Shifts__c}" taborderhint="7"/>
    <apex:inputField value="{!Client_Site__c.State__c}" taborderhint="3"/>
    <apex:inputField value="{!Client_Site__c.Number_of_Team_Leads__c}" taborderhint="8"/>
    <apex:inputField value="{!Client_Site__c.Head_count__c}" taborderhint="4"/>
    <apex:inputField value="{!Client_Site__c.Number_of_On_Site_Managers__c}" taborderhint="9"/>
    <apex:inputField value="{!Client_Site__c.Job_Titles__c}" taborderhint="5"/>
    <apex:inputField value="{!Client_Site__c.Union_or_Non_Union__c}" taborderhint="10"/>
  
  </apex:pageBlockSection>
<apex:pageBlockSection title="Client Suppliers" columns="4">
</apex:pageBlockSection>
     <apex:repeat value="{!lClientSuppliers}" var="x">
     <apex:panelGrid columns="6">
     <apex:panelGrid >
     <apex:facet name="header">Client Supplier Name</apex:facet>
     <apex:inputField value="{!x.c.Supplier_Name__c}" style="width:200px" />
     </apex:panelGrid>
     <apex:panelGrid >
     <apex:facet name="header">Is This a New or Incumbent Supplier?y</apex:facet>
     <apex:inputField value="{!x.c.Is_This_a_New_or_Incumbent_Supplier__c}" style="width:200px" />  
     </apex:panelGrid>  
     <apex:panelGrid >
     <apex:facet name="header">Skill Type</apex:facet>
     <apex:inputField value="{!x.c.Skill_Type__c}" style="width:200px"/> 
     </apex:panelGrid>
     <apex:panelGrid >
     <apex:facet name="header"> Will Manpower manage this supplier?</apex:facet>
     <apex:inputField value="{!x.c.Will_Manpower_Manage_This_Supplier__c}" style="width:200px" />
     </apex:panelGrid>
     <apex:panelGrid >
     </apex:panelGrid>    
    <apex:commandButton action="{!deleteClientSupplier}" style="Button" value="Delete ClientSite" reRender="myForm" immediate="true">
    
<apex:param value="{!x.counter}" name="selected"
assignTo="{!selectedContact}"/>
</apex:commandButton>      
    </apex:panelGrid> 
    </apex:repeat>   
    
    <apex:pageBlockButtons location="bottom">
    
     <apex:panelGrid ></apex:panelGrid>
    <apex:commandButton value="Add Client Supplier" action="{!addAClientSupplier}" reRender="myForm" immediate="true" />    
    <apex:commandButton value="Save" action="{!saveClientSite}"  />
    <apex:commandButton value="Cancel" action="{!cancel}"/>
   </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>
</apex:page>
public class AddClientSupplier {

    ApexPages.StandardController sc;      
    public Client_Site__c acct{get;set;}
    public Client_Discovery__c cDiscovery;
    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) {
        sc=controller;
        this.acct = (Client_Site__c)controller.getRecord();        
        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() {
    sc.save();  
 
  PageReference pRef = Page.ClientSite;     
  pRef.setRedirect(true);
  //pRef.getParameters().put('Client_Discovery__c', cDiscovery.id);
  
  return pRef;
} 

}
When i Hit Save and New 

User-added image

Missing Client Disovery ID

Thanks 
Akhil 
Best Answer chosen by SF7
Alexander TsitsuraAlexander Tsitsura
Yeah, i found problem

Problem is that when you open page extension rewriten Client_Discovery__c. Extenstion try get id from url but not found. In you case this automaticaly get from CF00N1300000BRJGg_lkid

try this code
public with sharing class ClientSiteExt {
    public ClientSiteExt(ApexPages.StandardController controller) {
        Client_Site__c c = (Client_Site__c) controller.getRecord();
        String cdId = ApexPages.currentPage().getParameters().get('Client_Discovery__c');
        if ((null != cdId) && (cdId.length() > 0)) {
            c.Client_Discovery__c  = cdId;
        }
    }
}

Thanks,
Alex

 

All Answers

Alexander TsitsuraAlexander Tsitsura
Hi Akhil,

Try this code(i copied all params from page)
 
public PageReference saveAndNew() {
  sc.save();  
  PageReference pRef = Page.ClientSite;     
  pRef.getParameters().putAll(ApexPages.currentPage().getParameters());
  pRef.setRedirect(true);
  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
@Alexander 

Bro Life Safer , Thanks man . This was driving me crazy.

Thank You very much. 
SF7SF7
@Alexander,

One more question please.  i am using a Custom Save method to redirect from Client Disocvery to Client Site. Now when we go to an existing Client disovery and try to create a New client Site i am trying to open the Same Client Site visual force where users can create Client Site with Client Suppliers .So i replaced my standard new Button with ClientSite VF page but again i am nbot getting Client Discovery ID (LookUp). Any suggestion please
SF7SF7
When i hit new i see it in the URl but it is not populating inside the LookUp

https://c.cs19.visual.force.com/apex/ClientSite1?CF00N1300000BRJGg=CD-0161&CF00N1300000BRJGg_lkid=a1j2900000001dO&scontrolCaching=1&retURL=%2Fapex%2FClientDiscoveryView%3Fid%3Da1j2900000001dO%26sfdc.override%3D1&sf
Alexander TsitsuraAlexander Tsitsura
Akhil can you put here link when you click on "Client Disocvery" and open your page for create "Client Site"?
Alexander TsitsuraAlexander Tsitsura
Client Dicovery id it's "CF00N1300000BRJGg_lkid" param from url
Alexander TsitsuraAlexander Tsitsura
can you share you vf page?
SF7SF7
@alexander 

I am using the same VF ClientSite mentioned above.
SF7SF7
I am using ClientSite Page when redirectly from ClientQuestionarrie Page, So i am Using the same page to replace New Button. 
Alexander TsitsuraAlexander Tsitsura
Yeah, i found problem

Problem is that when you open page extension rewriten Client_Discovery__c. Extenstion try get id from url but not found. In you case this automaticaly get from CF00N1300000BRJGg_lkid

try this code
public with sharing class ClientSiteExt {
    public ClientSiteExt(ApexPages.StandardController controller) {
        Client_Site__c c = (Client_Site__c) controller.getRecord();
        String cdId = ApexPages.currentPage().getParameters().get('Client_Discovery__c');
        if ((null != cdId) && (cdId.length() > 0)) {
            c.Client_Discovery__c  = cdId;
        }
    }
}

Thanks,
Alex

 
This was selected as the best answer
SF7SF7
Awesome that worked . Thank you very much Alexader.
SF7SF7
@alexander 

Sorry Man , last question i promise . When i save the Client Site along with the Supplier (using Save method) it save both Client Site and Client Supplier but when i use SAVE and NEW it only saves CLient Site parent object but not child object.
Alexander TsitsuraAlexander Tsitsura
Hi Akhil,

You need add this logic in your saveAndNew method
 
public PageReference saveAndNew() {
    saveClientSite();
    
    // existing code
    return pRef;
}

Thanks,
Alex
 
SF7SF7
That worked man appreaciate it. ,