[SalesForce] Redirect to Visualforce Page

I have created a custom button in custom object "SAP". When i click the custom button it will go to the new VF page(VF page 1).In that page i am having a custom link when i click that link it will go to another VF page (which is to create a new account). After creating the new account when i click save it must redriect to the VF page1 and the account name must be populated in the VF page 1 textbox . when i run this program i getting this error .how can i solve this issues. please check my code is right or wrong.
System.StringException: Invalid id: account.Id
Class.AccountCreateController.: line 12, column 1

Apex Class:

public with sharing class AccountCreateController1 {
  private SAP_SD__c sap;
  public Id accId;
  public Account acc {
    get;
    set;
  }

  public AccountCreateController1(ApexPages.StandardController controller) {
    acc = new Account();
    this.sap = (SAP_SD__c) controller.getRecord();
    if (ApexPages.currentPage().getParameters().get('accId') != NULL) {
        accId = ApexPages.currentPage().getParameters().get('accId');
    }
    if (accId != NULL) {
        acc = [SELECT ID, name from Account where id = : accId];
    }
  }

  // the account record you are adding values to
  public Account account {
    get {
        if (account == null)
            account = new Account();
        return account;
    }
    set;
  }

  public AccountCreateController1() {
    // blank constructor
  }

  // save button is clicked
  public PageReference save() {
    try {
        insert account; // 
        PageReference newocp = new PageReference('/apex/VFPage1?accId=
            account.Id');
        newocp.setRedirect(true);
        return newocp;
    } catch (DMLException e) {
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error creating new account.'));
        return null;
    }

    return null;
  }
}

VF page 1:

<apex:page standardController="SAP_SD__c" extensions="AccountCreateController1">    
 <apex:form>    
   <apex:outputField value="{!acc.name}" />
   <apex:outputLink value="https://c.ap1.visual.force.com/apex/AccountCreateController1?scontrolCaching=1&id=a089000000GrUA9" id="page">Click here</apex:outputlink>
 </apex:form>
</apex:page>

VF Page 2:

<apex:page standardController="SAP_SD__c" extensions="AccountCreateController1">
 <apex:sectionHeader title="Create MainContractor" /> 
  <apex:form >
   <apex:pageMessages /> <!-- this is where the error messages will appear -->
   <apex:pageBlock title="Account Info"> 
    <apex:pageBlockButtons >
     <apex:commandButton action="{!save}" value="Save"/>
     <apex:commandButton action="{!cancel}" value="Cancel"/>
    </apex:pageBlockButtons> 
    <apex:pageBlockSection showHeader="false" columns="2">
     <apex:inputField value="{!account.Name}" />
     <!--<apex:inputField value="{!account.Name}" />-->
     <apex:inputField value="{!account.BillingStreet}" />
     <apex:inputField value="{!account.BillingCity}" />
     <apex:inputField value="{!account.BillingState}" />
     <apex:inputField value="{!account.BillingCountry}" />
     <apex:inputField value="{!account.BillingPostalCode}" />
     <apex:inputField value="{!account.Website}" />
     <!--<apex:inputField value="{!account.Name}" />
     <apex:inputField value="{!account.Name}" />
     <apex:inputField value="{!account.Name}" />-->
     <apex:inputField value="{!account.Phone}" />
     <apex:inputField value="{!account.Email__c}" />
     <apex:inputField value="{!account.Fax}" />
   </apex:pageBlockSection> 
  </apex:pageBlock>
 </apex:form>
</apex:page>

Best Answer

I think your problem is the return url in the save function

public PageReference save() {
    try {
        insert account; // 
        PageReference newocp = new PageReference('/apex/VFPage1?accId=
            account.Id');

You have included the account.id in the string literal so accId actually equals account.id

Change it to :

PageReference newocp = new PageReference('/apex/VFPage1?accId='+account.Id);
Related Topic