+ Start a Discussion
as6as6 

why can't I update the value of an input text box connected to an s-object

I've created the following page:

 

<apex:page controller="MyClass4">

    <apex:form >
        <apex:inputfield value="{!mileage.Miles__c}"/>
            <BR/>
        <apex:commandbutton action="{!submitMileage}" value="Submit Mileage"/>
    </apex:form>
</apex:page>

 

Controller is:

 

public class MyClass4
{

    public Integer inputValue{get;set;}
      

    private Mileage__c mileage= new Mileage__c(); //I have an s-object with API name 'Mileage__c'
    public Mileage__c getMileage()
    {return mileage;}
    public void setMileage(Mileage__c mile)
    {mileage= mile;}
   
    public Pagereference submitMileage() {
        insert mileage;
      return null;
    }
}

 

I've included this page and it's controller along with a test suite and the custom object 'Mileagein a package and uploaded it.

Installation URL is:

https://login.salesforce.com/?startURL=%2Fpackaging%2FinstallPackage.apexp%3Fp0%3D04t900000008vib

Navigate to the above Installation URL to install the package. You'll then have everything you need to solve my problem.

 

 

Steps:

1. Open MyPage4 in browser. You'll see an inputtext box and a button 'Submit Mileage'.

2. Enter any value in the inputtext box. Click the 'Submit Mileage' button. In the background, a new record of the s-object 'Mileage__c' will be made  and it's 'Miles__c' field will be populated with the value you provided in the inputtext box.

3. Now, change the value in the inputtext box and click 'Submit Mileage' button again.

4. You'll get an error like:

System.DmlException: Insert failed. First exception on row 0 with id a0190000000EvlBAAS; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Now, my question is why do we get this error.

 

I guess we get this error because the Controller Class' object doesn't die until the page gets refreshed. The controller's object has only one object of type 'Mileage__c'. It's row0 has 'Miles__c' which already has the value that we passed through the inputtext box the first time.

When we enter some other value in the inputtext box and press the 'Submit Mileage' button again, the page tries to put this new value in row0 again. But, perhaps it's not allowed to overwrite the value once entered into any field of the object of type 'Mileage__c'.

 

If you're not understanding my words, please feel free to call me on 9028527067.

Best Answer chosen by Admin (Salesforce Developers) 
XactiumBenXactiumBen

When you click on the button it saves your record to the Mileage object.  Clicking on the button again tries to insert your already saved record into the mileage object.  This is not allowed.

 

If you want to update your mileage record from the screen you will have to use 'upsert' instead of 'insert'.  However, if you want to create a new mileage record every time you click on 'Submit Mileage' you will have to create a new Mileage record after the current record has been inserted:

 

public Pagereference submitMileage() { insert mileage; // Creates a new Mileage record that you can insert into Salesforce mileage = new Mileage(); return null; }

 

All Answers

XactiumBenXactiumBen

When you click on the button it saves your record to the Mileage object.  Clicking on the button again tries to insert your already saved record into the mileage object.  This is not allowed.

 

If you want to update your mileage record from the screen you will have to use 'upsert' instead of 'insert'.  However, if you want to create a new mileage record every time you click on 'Submit Mileage' you will have to create a new Mileage record after the current record has been inserted:

 

public Pagereference submitMileage() { insert mileage; // Creates a new Mileage record that you can insert into Salesforce mileage = new Mileage(); return null; }

 

This was selected as the best answer
as6as6

Thanks XactiumBen. Your answer was complete, to the point and just great! I tried both the solutions that you suggested. They're working fine for me. 

 

I've another doubt. I'm creating another thread for it. Please look into that too.