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
sayanasreekanth@gmail.comsayanasreekanth@gmail.com 

Record ID: cannot specify Id in an insert call

 

Hi There is a dropdown values in which user slects any of the drop down value and hit search button and the values will be displayed. I am writing my code to get the user information whenever a user hits the search button for reporting purpose.
For the first time when a user selects a value from the drop down and hit search button my code is working fine. For the second time on the same page when the user hits the search button it is throwing the error. If i refresh the page and do the search it is working. Here is the error it is throwing. i am pasting the code. Please have a look and apprepiate your help.
Record ID: cannot specify Id in an insert call
Apex controller.
public String getSearchColumnNames(String strName){
if(strName.equals('GAN')) return 'GAN__c';
searchreport();
if(strName.equals('BTN')) return 'BTN_Phone__c';
searchreport();
if(strName.equals('Order Number')) return 'BTN_Phone__c';
searchreport();
if(strName.equals('Trouble Ticket Number')) return 'BTN_Phone__c';
searchreport();
if(strName.equals('Circuit ID')) return 'Circuit_id__c';
searchreport();
if(strName.equals('CLLI')) return 'CLLI__c';
if(strName.equals('Product')) return 'BTN_Phone__c';
if(strName.equals('USI')) return 'BTN_Phone__c';
if(strName.equals('Summary Master TN')) return 'Summary_master_phone__c';
searchreport();
if(strName.equals('Customer Name')) return 'Name__c';
searchreport();
if(strName.equals('WTN')) return 'WTN__c';
searchreport();

return null;
}
public PageReference searchReport() {

if(linkSelected != null && linkSelected != '') {
system.debug('###### '+linkSelected);
tool.Search_by_BTN__c = 'linkselected';
}

if(strSearchByValue != null && strSearchByValue != '')
{
if(strSearchByValue.equals('BTN')){
tool.Search_by_BTN__c = 'Search By BTN';
}

else if(strSearchByValue.equals('GAN')) {
tool.Search_by_BTN__c = 'Search By GAN';
}
else {
tool.Search_by_BTN__c = strSearchByValue;
}
if(strSearchByValue.equals('Summary Master TN')){
tool.Search_by_BTN__c = 'Search by Summary Master TN';
}
if(strSearchByValue.equals('customer Name')){
tool.Search_by_BTN__c = 'Search by Customer Name';
}
if(strSearchByValue.equals('WTN')){
tool.Search_by_BTN__c = 'Search by WTN';
}
if(strSearchByValue.equals('Circuit ID')){
tool.Search_by_BTN__c = 'Search by Circuit ID';
}
if(strSearchByValue.equals('Order Number')){
tool.Search_by_BTN__c = 'Search by Order Number';
}
if(strSearchByValue.equals('Trouble Ticket Number')){
tool.Search_by_BTN__c = 'Search by Trouble Ticket Number';
}

}
system.debug('###### '+tool.Search_by_BTN__c);
cnt = [SELECT COUNT() FROM Users_of_Tool__c WHERE User_Record_Id__c = :Userinfo.getUserId()];

if(cnt == 0) //not existing yet
{
ulist.add(new Users_of_Tool__c(User_Record_Id__c = Userinfo.getUserId()));
try{
Database.SaveResult saveResult = Database.Insert(uList[0], false);

System.debug('************* New List_of_User__c Id: ' + saveResult.getId());

tool.Users__c = saveResult.getId();
insert tool;


}catch(System.DMLException de){
System.debug('************insert of new tools used to NEW user FAILED:'+'\n'+ de.getMessage());
}
}else{
//get the current user record Id
uList = [SELECT Id, User_Record_Id__c FROM Users_of_Tool__c WHERE User_Record_Id__c = :Userinfo.getUserId() Limit 1];
tool.Users__c = uList[0].Id;
try{
insert tool;
update uList[0];

}catch(System.DMLException de){
System.debug('************insert of new tools used to EXISTING user FAILED: '+'\n'+ de.getMessage());
}
}
return null;
}

Here is the vf page
<apex:outputLink target="_blank" onclick="callCon('DST');" value="https://www3.onlinefinancialdocs.com/tf/FANMedia?tx=Startup&cz=c060413270517141319080417&cmd=logon_&doctype=list&qta=verify">DST</apex:outputLink>

<apex:outputLink target="_blank" value="https://www3.onlinefinancialdocs.com/tf/FANMedia?tx=Startup&cz=c060413270517141319080417&cmd=logon_&doctype=list&qta=verify">DST</apex:outputLink>-->
<apex:actionFunction name="callCon" onComplete="callCon1();">
<apex:param name="firstParam" value="" assignTo="{!linkSelected}"/>
</apex:actionFunction>
<apex:actionfunction name="callCon1" action="{!searchReport}"/>

Best Answer chosen by Admin (Salesforce Developers) 
Mohith Kumar ShrivastavaMohith Kumar Shrivastava

This exception comes if you try to insert already inserted record .

 

Example :

 

Account a =new Account();
a.name='Hello';
insert a;
insert a;

 

23:43:37:131 EXCEPTION_THROWN [4]|System.DmlException: Insert failed. First exception on row 0 with id 0019000000H6g5WAAR; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

 So this means you would have already inserted the record and again you are trying to insert the same .Hope this helps you to debug and understand .