You need to sign in to do that
Don't have an account?
riojohn
Error in Trigger used for creating records in Opportunities based on the Campaign Members
I had a requirement where i need to create records based on the Campaign Members in the Opportunity.i had done the trigger and the code was working fine but today i had an error .Can anyone help me out !
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
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; } */ } } }
INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call
This error occur only when you are trying to insert a record with an ID. and its not possible because record is alerdy inserted.
I think you need to use upsert instead of insert at line 34.