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 values in List

I have 10 inputtext values each i need to add in a single list.
My code(for eg)
Forecast_Revenue_Report__c frr = new Forecast_Revenue_Report__c();
        List<Forecast_Revenue_Report__c> frList = new List<Forecast_Revenue_Report__c>();
        frr.opp_id__c = opp.Id;
           if(frr.Forecast_Amount__c != 0){
                 frr.Forecast_Month__c = this.month1;
           frr.Forecast_Amount__c = this.amount1;
                 frList.add(frr);
              frr= null;
            System.debug('this is for ffff'+frr);
           }else if(frr.Forecast_Amount__c != 0){
                frr.Forecast_Month__c = this.month2;
           frr.Forecast_Amount__c = this.amount2;
                 frList.add(frr);
              frr= null;
           }


In above coding i can get only values for 1st condition(month1 & amount 1), but i need to get like all the 10 values in loop.
Please help me for this!
Grazitti TeamGrazitti Team
Hi,

As per my understanding, you just want to add 10 instances of "Forecast_Revenue_Report__c" object in the list. If you need this then you should do some changes in the code. 

Please see the code below:

List<Forecast_Revenue_Report__c> frList = new List<Forecast_Revenue_Report__c>();
for(i=0; i<9; i++){
	Forecast_Revenue_Report__c frr = new Forecast_Revenue_Report__c();
	if(frr.Forecast_Amount__c != 0){
		frr.opp_id__c = opp.Id;
		frr.Forecast_Month__c = this.month1;
		frr.Forecast_Amount__c = this.amount1;
	}else if(frr.Forecast_Amount__c != 0){
		frr.Forecast_Month__c = this.month2;
		frr.Forecast_Amount__c = this.amount2;
	}
	frList.add(frr);
	i++;
}

Please mark as best answer if it helps you.

Regards,
Grazitti Team,
www.grazitti.com
Vatsal KothariVatsal Kothari
Hi,

You can refer below code:
List<Forecast_Revenue_Report__c> frList = new List<Forecast_Revenue_Report__c>();
for(Integer i = 0; i < 10 ; i++){
	Forecast_Revenue_Report__c frr = new Forecast_Revenue_Report__c();
	if(frr.Forecast_Amount__c != 0){
		frr.Forecast_Month__c = this.month1;
		frr.Forecast_Amount__c = this.amount1;	
		frList.add(frr);
	}else if(frr.Forecast_Amount__c != 0){
		frr.Forecast_Month__c = this.month2;
		frr.Forecast_Amount__c = this.amount2;
		frList.add(frr);
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal