I had a requirement where i need to create records based on the Campaign Members in the Opportunitie.i had done the trigger and the code was working fine but today i had an error
Error:Apex trigger CreateList caused an unexpected exception, contact your administrator: CreateList: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0 with id 0062800000CMrpPAAT; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]: Trigger.CreateList: line 34, column 1
Trigger CreateList on Campaign ( after insert ) {
List<Opportunity> opp = new List<Opportunity>( );
List <OpportunityContactRole> ocrole = new List<OpportunityContactRole>( );
public integer iOPPCount {get; set;
}
public integer icount {get; set;
}
for( Campaign camp : trigger.new ){
if( camp.Create_List__c == True ){
String sCamp= String.valueof( camp.id );
iOPPCount = [SELECT count( ) FROM CampaignMember WHERE CampaignId =:sCamp];
if ( iOPPCount > 0 ){
// camp.addError( String.Valueof( iOPPCount ) );
List<CampaignMember> camps = [SELECT id,FirstName,LastName,CompanyOrAccount,ContactId FROM CampaignMember WHERE CampaignId =:sCamp];
for( CampaignMember CM : camps ){
String sEvent = String.valueof( camp.EventIDName__c );
String sName = String.valueof( CM.FirstName ) + ' ' + String.valueof( CM.LastName );
icount = [SELECT count( ) FROM Opportunity WHERE Campaign__c =:camp.Id and Name=:sEvent +'-' + sName];
if ( icount == 0 ){
// camp.addError( sEvent + '-' + sLast );
//Opportunity op = new Opportunity( Campaign__c=camp.Id,Name=sEvent+ '-' + sName,CloseDate=camp.EndDate,StageName='Prospecting' );
Opportunity op = new Opportunity(
Campaign__c=camp.Id,
Name=sEvent +'-' + sName,
CloseDate=camp.EndDate,
StageName='Prospecting',
Contact__c= cm.ContactId );
opp.add( op );
}
insert opp;
for( Opportunity ops : opp ){
string sOppId = String.Valueof( ops.get( 'id' ) );
string sConId = String.Valueof( ops.get( 'Contact__c' ) );
// camp.addError( String.Valueof( opp[0].id ) );
OpportunityContactRole ocr = new OpportunityContactRole(
OpportunityId = sOppId,
//ContactId = cm.ContactId,
ContactId = sConId,
IsPrimary = TRUE );
ocrole.add( ocr );
}
insert ocrole;
// insert ocrole;
}
}
else if( iOPPCount < 0 ){
//do nothing
}
/* {
List<Contact> OPPList =[Select Name from Contact];
for ( Contact OPP : OPPList )
{
String sOPPList= String.Valueof( OPP.get( 'Name' ) );
Opportunity o = new Opportunity( Campaign = camp.id, Name=camp.Name + '-' + sOPPList ,Contact__c =camp.Contact__c );
ot.add( o );
}
insert ot;
}
*/
}
}
}
opp
list includes objects that have already been inserted - and so have Id values assigned - if the loop is gone round more than once. The way to fix is (as Novarg points out) to move the DML and queries out of the loop.