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
SFDC@ChennaiSFDC@Chennai 

How to add a multiple values in list of same fields/variable

Hi
I need to create a list in that i have add 6 values, finally that list should be inserted in db,

My code is as below, 

List < Forecast_Revenue_Report__c > frList = new List < Forecast_Revenue_Report__c > ();

  Forecast_Revenue_Report__c frr = new Forecast_Revenue_Report__c();
  frr.opp_id__c = opp.Id;
           if(frr.Forecast_Amount__c != 0){
                 frr.Forecast_Amount__c = this.amount1; 
                 frList.add(frr);
                 frr= null;
                 
                 frr.Forecast_Amount__c = this.amount2;
                 frList.add(frr);
                 frr= null;

                 frr.Forecast_Amount__c = this.amount3;
                 frList.add(frr);
                 frr= null;

                 frr.Forecast_Amount__c = this.amount4;
                 frList.add(frr);
                frr= null;

                 frr.Forecast_Amount__c = this.amount5;
                 frList.add(frr);
                 frr= null;

                 frr.Forecast_Amount__c = this.amount6;
                 frList.add(frr);
                frr= null;
           }
        insert frList;

In this case i can insert  only frr.Forecast_Amount__c = this.amount6;value(list contain final value), but i need to get all the 6 types of amonut in Forecast_Amount__c filed/ List(frList) can any one help for this!
Best Answer chosen by SFDC@Chennai
Vatsal KothariVatsal Kothari
Yes,It in the If condition so we can use same name and every time we are creating new object i.e frr to insert 6 different records.

All Answers

Vatsal KothariVatsal Kothari
Hi,
you can refer below code:
List <Forecast_Revenue_Report__c > frList = new List < Forecast_Revenue_Report__c > ();

if(frr.Forecast_Amount__c != 0){

	if(this.amount1 != null){
		Forecast_Revenue_Report__c frr = new Forecast_Revenue_Report__c();
		frr.opp_id__c = opp.Id;
		frr.Forecast_Amount__c = this.amount1; 
		frList.add(frr);                 
	}
	
	if(this.amount2 != null){
		Forecast_Revenue_Report__c frr = new Forecast_Revenue_Report__c();
		frr.opp_id__c = opp.Id;
		frr.Forecast_Amount__c = this.amount2;
		frList.add(frr);
	}
	
	if(this.amount3 != null){	
		Forecast_Revenue_Report__c frr = new Forecast_Revenue_Report__c();
		frr.opp_id__c = opp.Id;
		frr.Forecast_Amount__c = this.amount3;
		frList.add(frr);
	}
	
	if(this.amount4 != null){
		Forecast_Revenue_Report__c frr = new Forecast_Revenue_Report__c();
		frr.opp_id__c = opp.Id;
		frr.Forecast_Amount__c = this.amount4;
		frList.add(frr);
	}
	
	if(this.amount5 != null){
		Forecast_Revenue_Report__c frr = new Forecast_Revenue_Report__c();
		frr.opp_id__c = opp.Id;
		frr.Forecast_Amount__c = this.amount5;
		frList.add(frr);
	}
	
	if(this.amount6 != null){
		Forecast_Revenue_Report__c frr = new Forecast_Revenue_Report__c();
		frr.opp_id__c = opp.Id;
		frr.Forecast_Amount__c = this.amount6;
		frList.add(frr);      
	}
}
if(frList.size() > 0){
	insert frList;
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
SFDC@ChennaiSFDC@Chennai
Vatsal,
Should we initialize frr(Object Reference) all the time?
Vatsal KothariVatsal Kothari
Yes,It in the If condition so we can use same name and every time we are creating new object i.e frr to insert 6 different records.
This was selected as the best answer