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
Nejib EssouriNejib Essouri 

Insert a row in a custom object

My page bloc section :
<apex:pageblockSection title="Organisme audité">
                     <apex:pageblockSection >
                     <apex:pageblockSection >
                          <apex:inputField value="{!customObj1.Name}"/>
                      </apex:pageblockSection>
                  
                      <apex:pageblockSection >
                          <apex:inputField value="{!customObj1.Date_debut__c}"/>
                      </apex:pageblockSection>
                      
                      <apex:pageblockSection >
                          <apex:inputField value="{!customObj1.Date_fin__c}"/>
                      </apex:pageblockSection>
                     
                      <apex:pageblockSection >
                          <apex:commandButton value="save" action="{!save}"/>
                      </apex:pageblockSection>
 my controller :
public with sharing class controller_audit {
    
     public Mission__c customObj1 {get;set;}
public controller_audit()
{
     customObj1= [select Name, Date_debut__c, Date_fin__c from Mission__c LIMIT 1];
}

public void save()
{
    
     insert customObj1;
   
}
}

Error: 
System.DmlException: Insert failed. First exception on row 0 with id a0P24000000GPkhEAG; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
help ?
Best Answer chosen by Nejib Essouri
sandeep sankhlasandeep sankhla
Hi,

As an best practise we should always initalize all the variables before using..so in your case please initalize it in constructor..

customObj1  = new Mission__c();

Thanks
Sandeep
 

All Answers

sandeep sankhlasandeep sankhla
Hi

Query is not required here, without query you can insert these...

Thanks
Sandeep
Michael DsozaMichael Dsoza
Hi Nejib,

As per your code, you are trying to insert record which is already exist in object. remove your controller code (customObj1= [select Name, Date_debut__c, Date_fin__c from Mission__c LIMIT 1];) and try it will work for you. If you want existing data to be displayed & you want to insert updated data to be inserted in your object then go for update instead of insert.

Hope it will help you.

thanks
Nejib EssouriNejib Essouri
Thanks everyone.
I try to remove the query code of my controller (
customObj1= [select Name, Date_debut__c, Date_fin__c from Mission__c LIMIT 1];
) But i have an other error :
System.NullPointerException: Attempt to de-reference a null object 
???
Pankaj_GanwaniPankaj_Ganwani
Hi,

Are you using standard controller? If so, use below constructor:

public controller_audit(ApexPages.StandardController con)
{
     customObj1= (Mission__c)con.getRecord();
}
Nejib EssouriNejib Essouri
Hi Pankaj,
I use i simple controller because i have to insert fields into different custom objects in one page visualforce.
Pankaj_GanwaniPankaj_Ganwani

public controller_audit()
{
     customObj1= new Mission__c();

}

Please initialize the object before using it.
sandeep sankhlasandeep sankhla
Hi,

As an best practise we should always initalize all the variables before using..so in your case please initalize it in constructor..

customObj1  = new Mission__c();

Thanks
Sandeep
 
This was selected as the best answer
Nejib EssouriNejib Essouri
Thanks guys for your support :)