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
DRobi83DRobi83 

Create custom save button and redirect to visualforce page with previously created ID in the URL

Hello

 

I am trying to achieve a 2 step wizard for an invoice.

 

Step 1: create the invoice record and click custom "save" button

Step 2: record is saved, and user is redirected to a 2nd step page, carrying with it step 1's id within the URL

 

I cannot for the life of me get the ID of the invoice record (step 1) to carry through to step 2.

 

Is there any step by step, i have looked all over this forum. Thanks in advance

 

Here is my vf

 

<apex:page standardController="Invoice__c" extensions="CurrencyPL">
    <apex:form >
    <apex:pageBlock title="Invoice Details" >
                <apex:pageBlockButtons >
                      <apex:commandButton action="{!saveAndRedirect}" value="Save Invoice & Add Line Items"/>
                      <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true"/>
                </apex:pageBlockButtons>
            <apex:pageMessages ></apex:pageMessages>
        <apex:pageBlockSection title="Information" columns="2" collapsible="false">
            <apex:inputField value="{!Invoice__c.Customer__c}" required="true" />
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Currency"/>
                <apex:outputPanel layout="block" styleClass="requiredInput" >
                <apex:outputPanel layout="block" styleClass="requiredBlock"/>
                <apex:selectList value="{!Invoice__c.Currency__c}" 
                     required="True" size="1" id="invoicecurr">
                    <apex:selectOptions value="{!getCurrency}"/>
                </apex:selectList>
                </apex:outputPanel>
                </apex:pageBlockSectionItem> 
            <apex:inputField value="{!Invoice__c.Date_Document__c}" required="true" /> 
            <apex:inputField value="{!Invoice__c.Due_Date__c}" required="true" />
            <apex:inputField value="{!Invoice__c.Type__c}" required="true" />  
            <apex:inputTextarea value="{!Invoice__c.Description__c}" required="true" /> 
        </apex:pageBlockSection>   
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

here is my class

 

public class CurrencyPL {
    private miiFinance__Invoice__c invoice;
    public CurrencyPL(ApexPages.StandardController controller) {
    this.invoice = (miiFinance__Invoice__c)controller.getRecord();
    }

   public PageReference step2() {
      return Page.NewInvoicePage2;
   }

public PageReference saveAndRedirect() {
  stdCtrl.save(); // This takes care of the details for you.
  PageReference saveAndRedirectPage = Page.NewInvoicePage2; 

somewhere here place '.id'

  saveAndRedirectPage.setRedirect(true);
  return saveAndRedirectPage;
}

    public List <SelectOption> getCurrency {
    Get{
    List <SelectOption> curr = new List <SelectOption>();
    for( miiFinance__Currency__c cu: [select Name from miiFinance__Currency__c WHERE miiFinance__Active__c = TRUE])
    curr.add(new selectOption(cu.id, cu.Name));
    return curr;
    }
    Set;
    }
    }

 

 

AmitSahuAmitSahu

You are using Standard Save method here ?

 

You can use Insert statement to get the ID of the invoice created and then append it to the PageReference.

 

Or if you can get the Id of the record newly created, then also you can add this to the page reference

DRobi83DRobi83

I would like to use standard save methos if i can.

 

Do you have an example code snippet or another post which may display one? Ive looked everywhere and people all do it differently.


Dave

bob_buzzardbob_buzzard

I've found that with the standard save method, I can't get at the id of the inserted record.  Thus I tend to use insert and then add the id to the pagereference as follows:

 

  Invoice__c inv=(Invoice__c) stdCtrl.getRecord(); insert inv;
  PageReference saveAndRedirectPage = Page.NewInvoicePage2; 
  saveAndRedirectPage.setRedirect(true);
  saveAndRedirectPage.getParameters().put('id', inv.id);
  return saveAndRedirectPage;
AmitSahuAmitSahu

Yes, that's right Bob, the save() method wont give us the ID. But insert() does ..and that can be used and may be we can add that to extensions...

DRobi83DRobi83

Hi Bob

 

I tried your code but i get this error now

 

Error: CurrencyPL Compile Error: expecting right curly bracket, found 'insert' at line 11 column 75

 

Where did i go wrong?

 

 

public class CurrencyPL {
    private miiFinance__Invoice__c invoice;
    public CurrencyPL(ApexPages.StandardController controller) {
    this.invoice = (miiFinance__Invoice__c)controller.getRecord();
    }

   public PageReference step2() {
      return Page.NewInvoicePage2;
   }

  miiFinance__Invoice__c inv=(miiFinance__Invoice__c) stdCtrl.getRecord(); insert inv;
  
  PageReference saveAndRedirectPage = Page.NewInvoicePage2; 
  saveAndRedirectPage.setRedirect(true);
  saveAndRedirectPage.getParameters().put('id', inv.id);
  return saveAndRedirectPage;


    public List <SelectOption> getCurrency {
    Get{
    List <SelectOption> curr = new List <SelectOption>();
    for( miiFinance__Currency__c cu: [select Name from miiFinance__Currency__c WHERE miiFinance__Active__c = TRUE])
    curr.add(new selectOption(cu.id, cu.Name));
    return curr;
    }
    Set;
    }
    }

 

bob_buzzardbob_buzzard

Those lines are your save method, at the moment they are just floating in the class.