0

I want to insert a record of custom object Positions__c from VF page using CUSTOM CONTROLLER. Howevere, I am gettin below exception:

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

Error is in expression '{!insertPositions}' in component in page basicvfpage: Class.BasicVFPageController.insertPositions: line 18, column 1 Below is my code:

VF Page:

<apex:page Controller="BasicVFPageController">
<apex:form >
    <apex:pageBlock title="Edit Positions">
        <apex:pageMessages id="myPGMSG"/>
        <apex:pageBlockSection > 
            <apex:inputField value="{!inputPosition.Name}"/>
            <apex:inputField value="{!inputPosition.Functional_Area__c}"/>
        </apex:pageBlockSection>

            <apex:commandButton value="Save" action="{!insertPositions}" rerender="myPGMSG"/>

    </apex:pageBlock>
</apex:form>

Custom Controller:

public class BasicVFPageController {

Public Positions__C inputPosition {get;set;}

public BasicVFPageController(){
    inputPosition = new Positions__C();
} 

 public void insertPositions(){
    upsert inputPosition;
     //return null;
}

}

I know I am missing something silly. Please somebody help me!!

2 Answers 2

1

error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

The above means you're specifying an Id in your code someplace when inserting records.

Error is in expression '{!insertPositions}' in component in page basicvfpage: Class.BasicVFPageController.insertPositions: line 18, column 1 Below is my code:

{!insertPositions} is being called from your Command Button. The error message is essentially telling you that you can't use Upsert and need to use insert records.

If you have records where you're specifying the Id, then you need to use update instead.

4
  • I am nowhere specifying the ID, and also, I have tried with insert but it is still giving me the same result
    – Sarang
    Commented Jun 11, 2016 at 14:01
  • What are the required fields on this object and is there a reason you've not written a controller that's an extension of Positions__C? Is the Positions__C.Name field unique? Is Functional_Area__c an external Id? Is there perhaps more code for this page that you've not shown us?
    – crmprogdev
    Commented Jun 11, 2016 at 15:19
  • Functional_Area__c and Name are both required fields. I have not written extension because I wanted to try and save record using custom controller only, just to understand what are the problems/positives of that. Please let me know which is the advisable way of inserting/updating a record in DB, a)using std. controller and extensions OR b)Using cutomm controllers. Functional_Area__c is not an external ID . This is the only code that I have given here
    – Sarang
    Commented Jun 13, 2016 at 8:09
  • You need to have an instance of the standardController. It is much easier to write a controller Extension than it is a purely custom controller. In fact, I don't really see anything on your page that requires you to use anything beyond the standardController for Positions__c. Everything you've used on your page is standard functionality for a controller. See the VisualForce Reference Guide for more on this. Don't try to reinvent the wheel when it's not necessary.
    – crmprogdev
    Commented Jun 13, 2016 at 11:49
0

Maybe the problem is the 'upsert' method. Do you hava an ExternalId and Unique custom field?

To upsert something, do you need a 'key' to compare if it exist or not.

Read the SOAP API: Upsert SOAP API

There a paraph with this info:

If you are upserting a record for an object that has a custom field with both the External ID and Unique attributes selected (a unique index), you do not need any special permissions, because the Unique attribute prevents the creation of duplicates.

3
  • Even if I Insert it, the same problem occurs.
    – Sarang
    Commented Jun 11, 2016 at 14:01
  • And using an standardController?
    – Raspi
    Commented Jun 11, 2016 at 14:05
  • Using Standard controller, it is working fine
    – Sarang
    Commented Jun 12, 2016 at 4:31

You must log in to answer this question.

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