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
Sandra WicketSandra Wicket 

Create a child record for each parent record in the list

Hi Guys,

this is my code :
public class CreateMultiExtraBoost {

	public List<cBoost> boostList {get; set;}
    Integer listSize {get;set;}
    public Id opportunityId {get;set;}
    public ID finalBoostId {get; set;}
        
    // Constructor 
    public CreateMultiExtraBoost (){
    	opportunityId =  ApexPages.currentPage().getparameters().get('oppId');
    }

	//Methode
	public List<cBoost> getBoosts() {
		if(boostList == null) {
			boostList = new List<cBoost>();
			for(FinalBoost__c c: [select Id, Name from FinalBoost__c ]) {
				boostList.add(new cBoost(c));
			}
		}
		return boostList;
	}

	public PageReference save() {
 
		List<FinalBoost__c> selectedContacts = new List <FinalBoost__c>();
        List<BoostOpp__c> boostItems = new List <BoostOpp__c>(); 
        pagereference oppPage = new pagereference('/' + opportunityId);

		for(cBoost cCon: getBoosts ()) {
			if(cCon.selected == true ) {
				selectedContacts.add(cCon.con);
			}
		}

		System.debug('These are the selected ...');
		for(FinalBoost__c con: selectedContacts) {
			system.debug(con);
		}
        
        for (FinalBoost__c con: selectedContacts) {
            	boostItems.add(new BoostOpp__c());
            	finalBoostId = con.Id;
            
            		for(BoostOpp__c boostIteminList : boostItems) {
                        boostIteminList.Opportunity__c = opportunityId;
                        boostIteminList.FinalBoost__c = finalBoostId;
       	 }
       }
        insert boostItems;
        return oppPage;
	}
    
   
   // WrapperClass
	public class cBoost {
		public FinalBoost__c con {get; set;}
		public Boolean selected {get; set;}

		// Constructor 
        public cBoost(FinalBoost__c c) {
			con = c;
			selected = false;
		}
	}
}

A visualforcepage shows the table of FinalBoost records. I can select records and use them in the save methode.  The BoostOpp__c object is a junction object. It is related to the opportunity and the FinalBoost Object. 

In my loop, i want to create for each selected record one child record and assign the parent id (FinalBoost) to the masterDetail field. At the moment, it creates for each selection one record but allways with the same finalBoostId.  Is there a simple solution for that ? 


Cheers Sandra
Best Answer chosen by Sandra Wicket
<Saket><Saket>
Hi Sandra,

Please find the solution below.
 
public PageReference save() {

List<FinalBoost__c> selectedContacts = new List <FinalBoost__c>();
List<BoostOpp__c> boostItems = new List <BoostOpp__c>(); 
pagereference oppPage = new pagereference('/' + opportunityId);

for(cBoost cCon: getBoosts ()) {
	if(cCon.selected == true ) {
		selectedContacts.add(cCon.con);
    }
}

for (FinalBoost__c con: selectedContacts) {
	
	BoostOpp__c boost = new BoostOpp__c();
    boost.FinalBoost__c = con.Id;
	boost.Opportunity__c = opportunityId;
	boostItems.add(boost);

	
}
insert boostItems;
return oppPage;
}

 

All Answers

<Saket><Saket>
Hi Sandra,

Please find the solution below.
 
public PageReference save() {

List<FinalBoost__c> selectedContacts = new List <FinalBoost__c>();
List<BoostOpp__c> boostItems = new List <BoostOpp__c>(); 
pagereference oppPage = new pagereference('/' + opportunityId);

for(cBoost cCon: getBoosts ()) {
	if(cCon.selected == true ) {
		selectedContacts.add(cCon.con);
    }
}

for (FinalBoost__c con: selectedContacts) {
	
	BoostOpp__c boost = new BoostOpp__c();
    boost.FinalBoost__c = con.Id;
	boost.Opportunity__c = opportunityId;
	boostItems.add(boost);

	
}
insert boostItems;
return oppPage;
}

 
This was selected as the best answer
Sandra WicketSandra Wicket
Thanks Saket , It works perfect ! There was one loop too much ^^  So it started the method for each selected record and assigned the last index id, right ? 
<Saket><Saket>
Yup u r correct :)
Now please mark my answer as best answer :P
Sandra WicketSandra Wicket
Hi Saket, 
could you help me again pls :( i need to get a user input in my wrapper class object and assign the value to the boost.userInputfield 
<Saket><Saket>
Hi Sandra,


What I understand is you want to bind the wrapper class variable which is(Custom Object -> FinalBoost__c field into visualforce tag inputfield)?, Please correct me if I am on wrong track, 

okay for this I am providing u a visualforce code snippet it may help u :D
 
<apex:repeat value="{!boostList}" var="boost">
  <apex:inputField value="{!boost.con.FinalBoostFieldApiName}"/>
</apex:repeat>
If this snippet dosen't helps you then please let me know I will be here with another solution for u :)

Thanks 
SaketSharma
sharmasaket703@gmail.com




 
Sandra WicketSandra Wicket
Hi Saket,
thanks for your quick response.  Yes, that is what i want. It should work like the selected variable in the cBoost object. So i want to give the user the option to create a value for each selected cBoost object and use this input in my save methode 

Thanks a lot ! 

Cheers Sandra
<Saket><Saket>
Hi Sandra,

Is your problem is solved ? if not then let me know

Thanks 
Sandra WicketSandra Wicket
unfortunately not. 

Greetings Sandra
<Saket><Saket>
Can u also share ur vf code here so that i can help in a better way?