You need to sign in to do that
Don't have an account?
Nejib 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 ?
<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 ?
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
Query is not required here, without query you can insert these...
Thanks
Sandeep
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
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
???
Are you using standard controller? If so, use below constructor:
public controller_audit(ApexPages.StandardController con)
{
customObj1= (Mission__c)con.getRecord();
}
I use i simple controller because i have to insert fields into different custom objects in one page visualforce.
public controller_audit()
{
customObj1= new Mission__c();
}
Please initialize the object before using it.
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