0

I have created the lemonade app. And for that app, I have created a visual force page which displays a form and a report. The form gets the value and inserts it into the object (Drink_Order__c).

Visual force page:-

<div class="row">
            <div class="col-md-6">
                <apex:form styleClass="form-group">
                    <apex:pageBlock>
                        <apex:inputField styleClass="form-control" value="{!d.Flavor__c}"/><br />
                        <apex:inputField styleClass="form-control" value="{!d.Size__c}"/><br />
                        <apex:inputField styleClass="form-control" value="{!d.Price__c}"/><br />


                        <apex:commandButton styleClass="btn btn-primary" action="{!save}" value="Take Order"/>

                        <apex:pageBlockTable value="{!samepage}" var="c">
                          <apex:column headerValue="Flavor">
                            <apex:outputField value="{!c.Flavor__c}"/>
                          </apex:column>

                          <apex:column headerValue="Size">
                            <apex:outputField value="{!c.Size__c}"/>
                          </apex:column>

                          <apex:column headerValue="Price">
                            <apex:outputField value="{!c.Price__c}"/>
                          </apex:column>
                        </apex:pageBlockTable>
                    </apex:pageBlock>
                </apex:form>

            </div>
            <div class="jumbotron" style="padding: 5px;">
                <div class="">
                    <h3>Report</h3>
                    <analytics:reportChart developerName="" reportId="00O6F00000CbFKkUAN"/>        
                </div>
            </div>
        </div>

Controller:-

public with sharing class LemonadeController {
    // fetching fields from Drink_Order__c
    public Drink_Order__c d{get; set;}
    public List<Drink_Order__c> samepage { get; set; }


    public LemonadeController(){
        d = new Drink_Order__c();

    }

    public PageReference save(){
        insert d;
        samepage = [SELECT Id, Flavor__c, Size__c, Price__c FROM Drink_Order__c WHERE id=:d.id];
        return null;
    }

}

I got the below error after 1 record insert

Insert failed. First exception on row 0 with id a086F000028cCCzQAM; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Error is in expression '{!save}' in component in page lemonadeapp: Class.LemonadeController.save: line 13, column 1

1 Answer 1

0

When you first load the VF Page. Its Controller is getting initialized and value of d is getting set.

And when you save the record for the first time it remains as the blank entity.

Now, when you enter the second record, the value of d has already Id in it. And when the call goes to save method and insert d statement is being called, it gets failed. Because it already has the id that was being set in first record insertion.

3
  • Hey, @VivekMakker What is the solution for this? I want to display all the records in the visualforce page. And every new record should be displayed on the page itself. Sep 13, 2018 at 10:06
  • @SagarPanwar simple way I can see is to add the line d = new Drink_Order__c(); after insert d statement. And, rerender the page block after the click of Take order button. And use repeat in your pageblock and pass a list in it, Sep 13, 2018 at 10:15
  • Thank you. I'll try this solution. I'll let you know if it works for me. Sep 13, 2018 at 10:35

You must log in to answer this question.

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