I want to insert the records in a custom object. The code is working fine when I insert one record at a time but when I am trying to insert more than one record on a same run one at a time than it is giving exception INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call can anyone help?
<apex:page controller="myclass" docType="html-5.0">
<apex:form >
Product Name: <apex:input type="text" id="name" value="{!value}"/><br/>
Product Category: <apex:input type="text" id="cat" value="{!value2}"/><br/>
Product Status: <apex:input type="text" id="status" value="{!value3}"/><br/>
<apex:commandButton action="{!insertRecords}" value="INSERT RECORDS" rerender="theForm"/>
</apex:form>
</apex:page>
public class myclass {
opportunityProduct__c op= new opportunityProduct__c();
public string value { get; set;}
public string value2 { get; set;}
public string value3 { get; set;}
//String [] arrayOfProducts = new String[] {'Shavings','Bins/ Containers','others','fee'};
public void insertRecords(){
// value = apexpages.currentPage().getParameters().get('inpval1');
//value2 = apexpages.currentPage().getParameters().get('inpval2');
//value3 = apexpages.currentPage().getParameters().get('inpval3');
system.debug(value + value2+ value3);
op.Prod_name__c= value;
op.Prod_type__c=value2;
insert op;
value='';
value2='';
system.debug('record inserted' + op);
// for (Integer i=0; i<arrayOfProducts.size(); i++) {
//if(arrayOfProducts[i].equalsIgnoreCase(value2) && ((!value3.equalsIgnoreCase('Closed won') && (!value3.equalsIgnoreCase('Closed lost'))) ){
// insert op;
//system.debug('record inserted' + op);
//}
//else{
//system.debug('Category not predefined. Record with a new type inserted. ' + op);
//arrayOfProducts[arrayOfProducts.size()]=value2;
//insert op;
// }
//}
if ( value3.equalsIgnoreCase('Closed won') || value3.equalsIgnoreCase('Closed lost'))
system.debug('Opportunity is in stage Closed Won or Closed Lost.');
}
}