ShowAll Questionssorted byDate Posted
Steve Connelly 5

# Using nested loops to populate field on new record

How do I increnet loops through quarter and year and generate output for field?

Fields in Opp provide year and quarter. opp was created and number of quarters the opp spans

I need loops that will start with the year (ex 2019) and quarter (ex 2) that the opp was created.

Outer loop starts with the year, 2019.

Inner loop starts with the quarter, say 3.

The opp spans 5 quarters so i nee dthis to iterate 5 times.

The output for the first iteration should be 2019-Q3 so i can input that value into the first new record.

The next run increments up the quarter from 3 to four and it creates the next record with 2019-Q4.

The next run needs to do two things...Restart the quarter count back to 1 and increment the year up to 2020 for a result of 2020-Q1

And so forth until we have looped the number of times  indicated by the span number.

NOTE I alreadt have the basic loop that creates new records for the correct number of times, I just don't know how to do all this incrementing and creating the incementing output.

Any help out there for a new coder please?
following is the part I am working with
```string created = opportunityList.Quarter_Created_op__c;
decimal year = opportunityList.Start_Year__c;
decimal quarter = opportunityList.Start_Quarter__c;
decimal span = opportunityList.Quarters_Spanned_op__c;

//while loop1 increment quarter
decimal counter1 = year;
while (counter1 < Year + 1)
{

//while loop2 increment year
decimal counter2 = quarter;
while (counter2 < quarter + 1)
{

counter2++;
}//end while loop2

counter1++;
}//end while loop1

//while loop3 create record process
decimal counter3 = span;
while (counter3 < span)
{

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);
counter3++;

}//end while loop3```

Thans much,
Steve
Best Answer chosen by Steve Connelly 5
Steve Connelly 5
Nope, not at all what i am looking for,
Sc

### All Answers

Abhishek (Salesforce Developers)
Hi Steve,

You can try the suggestion as mentioned in the below blog,

https://success.salesforce.com/apex/answers?id=90630000000CiZ3AAK

It might help you.If it does, please mark as Best Answer to help others too.

Thanks.
Steve Connelly 5
Sorry no, not what I am looking for at all. but thanks for answering.

I need to know how to start a loop in a trigger over when it hits 4.

This is cycling through quarters. So it if starts at q3, it would increment to q4 and in the next run increment back to 1.

If it starts ar 1, it would increment to 2, then 3 and then 4 ans start over again.

How do i do that?
Steve
Abhishek (Salesforce Developers)
Try this,

https://developer.salesforce.com/forums/?id=906F000000092I7IAI
Steve Connelly 5
Nope, not at all what i am looking for,
Sc
This was selected as the best answer
Abhishek (Salesforce Developers)
Please feel free to reach out to the accelerator team who can give you the idea of how you can implement according to your business requirement if you have the premier success plan.

Please find the details on how to request for an accelerator:https://help.salesforce.com/articleView?id=000337601&type=1&mode=1

Otherwise,I would request you to reach out to your Account Executive If you wish to upgrade to Premier Support please contact your Account Executive.