Insert item into a string from multiple list

I'm working on create the schedule using APEX. But I would like to schedule the time dynamically. So, I create five different lists: Second, Minute, Hour, Month, and Day. Each list contains its own value. Then I add all five lists into one master list and try to loop through the sublist's value. The reason is I want to create the String CRON EXPRESSION. For example, when it loops the Second list, the value in the Second list will add into the first position of the String CRON EXPRESSION. The CRON EXPRESS format looks like this: 'Seconds Minutes Hours Day_of_month Month'. When it loops through the Minute list, the value in the Minute list will add into the second position of the String CRON EXPRESSION and so on. Can anyone give me the idea how I can achieve this? Is it possible?

1 Answer

If you're trying to schedule time dynamically, I don't think you want to loop through an array of all your different lists. If you loop through the array of lists, you're going to get something that looks like this:

``````list <String> Dys = new list();
list <String> Mnth = new list();
list <String> Hrs = new list();
list <String> Mins = new list();
list <String> Scnds = new list();
String TmInstnce;
list<String> CRON_EXPRESSION = new list();
For(integer x=0; x<12; x++){
Mnth[x] = String.valueOf(x+1); // month has to come first because of impact on days
For(integer y=0; y<31; y++){
If(x==1 && y==28) y=31; // handles Feb being 28 days long
If((x==3 || x==5 || x==8 || x==10) && y==30) y=31;
// handles Apr, Jun, Aug & Nov = 30 days
Dys[y] = String.valueOf(y);
For (integer z=0; y<24; y++) {
Hrs[z] = String.valueOf(z); //use 12 if you want to use a 24hr clock
// add a For loop HERE (a=0; a<2; a++) to get AM/PM if you want 12 hour time
For (integer w=0; w<60; w++){
Mins[w] = String.valueOf(w);
For (integer v=0; v<60; v++){
Scnds[v] = String.valueOf(v);
TmInstnce = Scnds[v] +':'+ Mins[w] +':'+ Hrs[z] +':'+ Dys[y] +':' + Mnth[x];
// assumes you want colons separating everything in your string
CRON_EXPRESSION.add(TmInstnce);
}
}
}
}
}
update CRON_EXPRESSION;
``````

As you can see, what you get from the above is a list of all possible time combinations for every second that exists within a 12 month period (excluding leap years)!

If you want to put time together dynamically, I suggest you explore using the datetime.now() methods in Apex instead. You can easily parse the results as a string to get only the portions of it you want using the variety of datetime methods that exist.

Finally, if you you can also use system functions to get the time and/or only the portions of it you may need, which might also serve your purposes too depending on what they are. Those are primarily used for scheduling batch jobs and similar tasks. Datetime methods are what the APEX compiler is going to expect to see in your code.

Using the Apex time functions you can add hours, minutes, days, etc to the current DateTime to create the dynamic time that I suspect you're actually looking for. Once you have it as a string, you can use the parse command to convert into DateTime Format that APEX will recognize as valid. The APEX documentation is pretty extensive on how to use it, but if you have questions, post back for clarification. Actual code you're trying to work with would be helpful.