function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Salesforce Admin 110Salesforce Admin 110 

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
 
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);
    
  } 
    
}

 
Salesforce Admin 110Salesforce Admin 110
this section needs to be rewritten so that the product is inserted with the opp, i'm really stuck on how to do this
Mathew Andresen 5Mathew Andresen 5
eachProd.Easy_Opportunity__c = opplist[0].id;

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
 
for (Easy_Opportunity__c opp :oppList) {
    // create the product(s?)
	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 = opp.id;  // this is important note how this ID now changes with each loop through the opp List
    }
}
  // outside the opplist loop  
insert Pd;


 
Salesforce Admin 110Salesforce Admin 110
can you please tell me how i can add this back into the code
Mathew Andresen 5Mathew Andresen 5
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);
     
        } // end if
    }  // end for loop
    
    if(oppList.size() > 0){
        insert opplist; 
    
    for (Easy_Opportunity__c opp :oppList) {
            // create the product(s?)
            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 = opp.id;  // this is important note how this ID now changes with each loop through the opp List
            }
        }
      // outside the opplist loop  
    insert Pd;
    system.debug('### pd ' +  pd);
        
      } 
    
}

Also, try and use proper indentation, it will make your code easier to read.