i have created lookup relationship of accountnumber with account object and custom object customer. but I could not insert data in accountnumber field using below code. getting error System.StringException: Invalid id: ddf Error is in expression '{!save}' in component in page accountcustomer_vf: Class.AccountCustomer.save: line 32, column 1

<apex:page Controller="AccountCustomer">
    <apex:form >
        <apex:inputText value="{!accnumber}" label="accountnumber"/>
        <apex:selectList value="{!selectoptions}" size="1">
            <apex:actionSupport event="onchange" />
            <apex:selectOptions value="{!AccountCustomer}">

        <apex:commandButton value="save" action="{!save}"/>

public class AccountCustomer{

    public string selectoptions{get;set;}
    public string selectoptions2{get;set;}
    public string selectoptions3{get;set;}
    public string accnumber{get;set;}
    List <SelectOption> option = new List<SelectOption>();
    public list <customer__c> querycresultc{get;set;}
    public list <customer__c> querycresult{get;set;}
    public list <Account> queryresult{get;set;}

    public AccountCustomer(){

        this.accnumber = accnumber;
    public void save() {

        querycresultc = [select  index__c,EMAILID__C,AccountNumber__c,id,ADDRESS_1__C,ADDRESS_2__C,AGE__C,PHONENO__C from customer__c where id =: selectoptions];
        // System.debug('the emailid value is' +querycresultc.EMAILID__C); 
        customer__c cc = new customer__c();
        cc.EMAILID__C = querycresultc[0].EMAILID__C;
        cc.ADDRESS_1__C = querycresultc[0].ADDRESS_1__C;
        cc.ADDRESS_2__C = querycresultc[0].ADDRESS_2__C;
        cc.AGE__C = querycresultc[0].AGE__C;
        cc.PHONENO__C = querycresultc[0].PHONENO__C;
        cc.AccountNumber__c = accnumber;  
        insert cc; 
    public List<selectoption> getAccountCustomer(){

        querycresult = [select id,EMAILID__C from customer__c];
        for(customer__c custlist: querycresult){
        option.add(new SelectOption(custlist.id,custlist.EMAILID__C));}
        System.debug('this is the value to display' + selectoptions);
        return option;

please suggest where I am wrong?

1 Answer 1


The problem is probably here: cc.AccountNumber__c = accnumber;

The AccountNumber__c field needs to receive an Account Id. That's how lookup relationships work on salesforce. You're probably passing the account number or name.

A better way for that to work without changing too much of you code is to do something like this:

  1. Instead of public string accnumber{get;set;}, use public customer__c temp{get;set;}
  2. Change this.accnumber = accnumber; with temp = new customer__c()
  3. On you visualforce change the <apex:inputText value="{!accnumber}/> to <apex:inputField value{!temp.AccountNumber__c}/>
  4. Change this line cc.AccountNumber__c = accnumber; with cc.AccountNumber__c = temp.AccountNumber__c;
  • thanks fernando for quick response.but I am getting below error in vf page " Error: Could not resolve the entity from <apex:inputField> value binding '{!temp.AccountNumber__c}'. <apex:inputField> can only be used with SObjects, or objects that are Visualforce field component resolvable."
    – meera
    Jul 18, 2016 at 9:01
  • Did you do step 1 and saved the class? Jul 18, 2016 at 9:13
  • yes..I have done same modification but its showing now error " System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!save}' in component <apex:commandButton> in page accountcustomer_vf: Class.AccountCustomer.save: line 31, column 1"
    – meera
    Jul 18, 2016 at 9:20
  • Oops. Sorry. Add a new step (2) that prevents that. Jul 18, 2016 at 9:26
  • thanks its working fine. But i want to insert new account number through VF page. how can I achieve it?
    – meera
    Jul 18, 2016 at 9:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .