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

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.

  • I am nowhere specifying the ID, and also, I have tried with insert but it is still giving me the same result – Sarang Jun 11 '16 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 Jun 11 '16 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 Jun 13 '16 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 Jun 13 '16 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.

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

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.