4

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>
2

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);
0

I am still wondering on the forst page where are you entering the account name. I am not able to trace out the same. So in case if account doesnot exist. The insert account will fail and there is no point in having account.id.

  • entering the name in VF page 2 here <apex:inputField value="{!account.Name}" /> – Udaya Jul 16 '13 at 13:18
0

Your controller for VF Page 1 is a standard controller for SAP_SD__c, so in the URL, it needs to have parameter called id that has a SAP_SD__c id as it's value for the controller to work. Right now, when you redirect from VF Page 2 back to VF Page 1, you are only using a parameter called accId to pass Account information.

Since VF Page 1 is a standard controller for SAP_SD__c, you must have some SAP_SD__c record that's used when you begin your scenario.

i.e.

public AccountCreateController1(ApexPages.StandardController controller) {
    acc = new Account();
    this.sap = (SAP_SD__c) controller.getRecord();  //<-------- this part

Change your "Save" method to also pass the associated SAP_SD__c with it for the direct from VF Page 1 to VF Page 2

e.g.

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

    return null;
  }
  • thanks for reply amatorVitae ,i change the code but getting the same error System.StringException: Invalid id: account.Id Class.AccountCreateController.<init>: line 12, column 1 – Udaya Jul 16 '13 at 13:16
  • what is actually line 12 in your controller? – Scott Pelak Jul 16 '13 at 13:22
  • this line accId = ApexPages.currentPage().getParameters().get('accId'); – Udaya Jul 16 '13 at 13:28
  • just debug what's in the parameter before assigning it to accId, chances are you're passing in something other than an ID – Ralph Callaway Jul 16 '13 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.