You need to sign in to do that
Don't have an account?

code fails after one transaction please help
this code works fine per single transaction but when i einsert multiple contacts at same time the insert fails
public with sharing class easycontacttriggerhelper { public static void buyerconConveyancy2(list<Contact> cons) { system.debug('###list<Contact> cons ' + cons); list<Easy_Opportunity__c> opplist = new list <Easy_Opportunity__c>(); recordtype[] tt = [Select r.Id, r.SobjectType, r.Name From RecordType r where sobjecttype ='Easy_Opportunity__c' and Name = 'Residential Sales']; for(Contact con : cons){ system.debug('### con ' + con); if( con.Stage__c == 'Lead' && con.contacttype__c != null && con.contacttype__C.contains('Buyer')){ Easy_Opportunity__c newopp = new Easy_Opportunity__c (); newopp.ownerid = con.Allocated_Negotiator__c; newopp.name = 'Applicant Conveyancy Opportunity'; newopp.account_name__c = con.accountid; newopp.CurrencyIsoCode = con.currencyisocode; newopp.stage__c = 'New'; newopp.recordtypeid = tt[0].Id; newopp.Contact_Name__c = con.id; newopp.Close_Date__c = Date.today().addDays(2); opplist.add(newopp); insert opplist; system.debug('### opplist ' + opplist); List<Product__c> Pd = (List<Product__c>) System.Json.deserialize('[{"attributes":{"type":"Product__c"},"Name":"Conveyancing","Sale_Price__c":"111.00"}]', List<Product__c>.class); for (Product__c eachProd : Pd) eachProd.Easy_Opportunity__c = opplist[0].id; insert Pd; system.debug('### pd ' + pd); } } } }
How are you calling this class from trigger? Can you share your trigger code also.
Also, the above code is having multiple DML operations under FOR Loop. It would be good if you move them outside the loop.
-Thanks,
TK
caused by: System.DmlException: Insert failed. First exception on row 0 with id a13250000005OnzAAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Class.easycontacttriggerhelper.buyerconConveyancy2: line 139, column 1
Trigger.easycontacttrigger: line 28, column 1