function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bibbi84bibbi84 

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

Hi, 

 

i have some problems with the insert of many records of a custom object from a single visualforce page.

Now i explain the question : i have a custom Object ( Elaborato__c ) and i had build a controller extension for it. Well, when i try to create ( and save ) more than a Elaborato__c from a single visualforce page, i have an error :

 

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

 

 

The reason,  i think( cause i see the view state of the page ), is that i create only one Elaborato__c and after the first Insert, the Id of the sObject remain the same for the second record too.

 

Now, one possible solution is make all with a custom controller, but by this way i lose all the advantages of using the Standard Controller of Elaborato__c.

 

public with sharing class ProvaDocenteController {

public List<Elaborato__c> lista;
	public Elaborato__c elaborato;
	public Docente__c docente{get;set;}
    public String email{get;set;}
    public String password{get;set;} 
    private boolean logged=false; 
    
    
     public ProvaDocenteController(ApexPages.StandardController controller){
    	
         
    	
    	this.elaborato = (Elaborato__c) controller.getRecord();
    	
    	
    	this.elaborato.titolo__c='Inserisci titolo';
    	this.elaborato.descrizione__c='Inserisci Descrizione';
    	this.elaborato.cdl__c='Ingegneria informatica';
    }
    
    
  
   
			
    
    public void save() {
    	
    	
    insert (elaborato);
    
    
	this.elaborato.titolo__c='Inserisci titolo';
	this.elaborato.dataProposta__c=System.today();
	this.elaborato.descrizione__c='Inserisci Descrizione';
	this.elaborato.cdl__c='Ingegneria informatica';
		

	}

	public List<Elaborato__c> getlista(){
		this.lista=new list<Elaborato__c>();
		if(this.logged!=false)
			this.lista=[SELECT name,titolo__c,descrizione__c,materia__r.nome__c,dataproposta__c, dataAssegnazione__c,dataArchiviazione__c,stato__c 
 			FROM Elaborato__c WHERE Docente__c=:this.docente.id  ORDER BY LastModifiedDate DESC  LIMIT 10 ];
		
		return lista;
	}

}

 

 

<apex:page sidebar="false" showHeader="false"
    standardController="Elaborato__c" extensions="ProvaDocenteController" 
    >

    <apex:composition template="UninaTemplate">

        <apex:define name="header">
            <apex:include pageName="header" />
        </apex:define>

        <apex:define name="body">

       
            <apex:form title="Pagina per l'inserimento di un elaborato ">
                <apex:pageblock title="Inserisci Proposta Elaborato">
                    <apex:pageblocksection id="mainsection">
                        <apex:inputfield value="{!Elaborato__c.titolo__c}" />
                        <apex:inputfield value="{!Elaborato__c.dataProposta__c}" />
                        <apex:inputfield value="{!Elaborato__c.descrizione__c}" />
                        <apex:inputfield value="{!Elaborato__c.Materia__c}" />
                        <apex:commandButton value="Save" action="{!save}"
                            rerender="mainsection,listaelab" />
                    </apex:pageblocksection>

                    <apex:pageblocksection collapsible="false">
                        <apex:pageBlockTable value="{!lista}" columns="10" width="80%"
                            var="e" id="listaelab">
                            <apex:column headerValue="Numero Elaborato"
                                title="Numero Elaborato" value="{!e.name}" />
                            <apex:column headerValue="titolo" title="titolo"
                                value="{!e.titolo__c}" />
                            <apex:column headerValue="descrizione" title="descrizione"
                                value="{!e.descrizione__c}" />
                            <apex:column headerValue="titolo" title="materia"
                                value="{!e.Materia__c}" />
                            <apex:column headerValue="Data Proposta" title="Data Proposta"
                                value="{!e.dataProposta__c}" />
                            <apex:column headerValue="Data Assegnazione"
                                title="Data Assegnazione" value="{!e.dataAssegnazione__c}" />
                            <apex:column headerValue="Data Archiviazione"
                                title="Data Archiviazione" value="{!e.dataArchiviazione__c}" />
                            <apex:column headerValue="Stato" title="Stato"
                                value="{!e.stato__c}" />
                        </apex:pageBlockTable>
                    </apex:pageblocksection>

                    </apex:pageBlock>
            </apex:form>
        </apex:define>
    </apex:composition>
</apex:page>

 

 I cut some lines of code from the controller, but i think that the problem is in the function save(). 

 

 

The question is : is possible to make more insert from a single page using the extension of a standard controller?

 

 

Thanks to all,

 

Fabrizio

Niket SFNiket SF

Hello Fabrizio,

 

I think if you are using

 

     this.elaborato = (Elaborato__c) controller.getRecord();    // This means you have record alredy saved

 

and you again trying to insert this record ====>  insert (elaborato); 

I think you need to use Update command depends upon your business requirement.

 

 

Thanks

Nik's

 

 

 

bibbi84bibbi84

Hi only nik,

 

 

The situation is this : all works good for the first insert. My problem is than for me is impossible to make a second Insert for a new record.

If i write only    update(elaborato)  i have this error

 

System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []