+ Start a Discussion
Steve Connelly 5Steve Connelly 5 

I could use a little help with nested "while" loops in a trigger.

Good morning all. I have a trigger that creates new records related to opportunities.

I have everything working but my loop and i am stuck. I am very new to coding and would really appreciate some assistance.

This iwhat I have so far and everything works...except....
                            //while loop create record process                     	
              				decimal counter = 0;
              				while (counter < opportunityList.Quarters_Spanned_op__c)
                			{
                                
                			Open_Quarter__c oq           = new Open_Quarter__C();
                			Quarter_Number__c qn		 = new Quarter_Number__c();
                			oq.Amount_Per_Quarter_oq__c  = opportunityList.Amount_per_Quarter_op__c;
                			oq.Close_Date_oq__c          = opportunityList.CloseDate;
                			oq.Name            			 = opportunityList.Quarter_Created_op__c;
                			oq.Opportunity_Name_oq__c    = opportunityList.Id;
                			oq.Quarters_Spanned_oq__c    = opportunityList.Quarters_Spanned_op__c;

							createOpenQuarter.add(oq);
                			counter++;
          					
                            }//end while loop                                             
When I create each new record I need it to populate a field in that record with escalating quarter names. Right now it creates the right nimber of records but they all have the same quarter name in them which the starting quarter of the opp.

Now I have four custom formula fields in the opp that I can use.
  • One with the name for the quarter when the opp was created in this format: 2019-Q1 etc
  • One with the year the opp was started as a number "2019"
  • One with the starting quarter as a number (1-4)
  • One with the number of quarters the opp spanse based upon the created and close dates also expressed as a number.
I was told that I can loop the quarters within the year so that if an opp ovewrlaps years i can increment the rear number and reset the quarter to one and continue the loop for the same number of times as the quarters spanned (I already have that part)

But the rest is all very new to me. How do I build these nested loops with an output for each iteration that looks like this "2019-Q1" that I can use in creating my new records.

I am really stuck here so any help at all will be greatly appreciated.
Best regards,
Steve
 
Best Answer chosen by Steve Connelly 5
SUCHARITA MONDALSUCHARITA MONDAL

Hi Steve,

You can create one formula field, where you can define the structure as  YYYY-Q1 (find link below, )

https://success.salesforce.com/answers?id=9063A000000idPQQAY

Use that formula field to assign to that field on opporunity.

Thanks,
Sucharita
 

All Answers

SUCHARITA MONDALSUCHARITA MONDAL

Hi Steve,

You can create one formula field, where you can define the structure as  YYYY-Q1 (find link below, )

https://success.salesforce.com/answers?id=9063A000000idPQQAY

Use that formula field to assign to that field on opporunity.

Thanks,
Sucharita
 

This was selected as the best answer
Steve Connelly 5Steve Connelly 5
I actually already have that field in the Opp (see above first bullet). That is my starting point for the loop. I need to create a record for each quarter the opp spans and name each record for each subsequat quarter for use in projections.