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

code only inserts product to first inserted opportunity
i have been modifing some code to allow multiple inserts but only the first inserted opportunity gets a product attached, please help
i need every inserted opp to have same product attached - opportunity/product is master/detail
heres a snippet of my class
i need every inserted opp to have same product attached - opportunity/product is master/detail
heres a snippet of my class
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); } } //outsideforloop // if(oppList.size() > 0){ 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); } }
Is attaching the product always to the first opportunity.
Instead you need to loop through your list of opportunities and create and attach the product to each one. Maybe something like
Also, try and use proper indentation, it will make your code easier to read.