The following script was recently required to diagnose a schedule component issue and we thought it might be of use to others:
//Script created: 3/29/2011 11:07:04 AM
//Name: Test - Generate random workorders
//ShowInMenuFor: Everywhere
//HandlesAyaNovaTypes: Nothing
//*** FOR DEVELOPMENT TESTING PURPOSES. DO NOT USE WITH A PRODUCTION DATABASE! ***
public static void AyaScriptMain(bool IsList, RootObjectTypes objectType, Object ayaNovaObject, List<Guid> objectIDList)
{
//Generate random test workorders and schedule markers
//Modify the values in the block below to adjust how and how many workorders are generated
DateTime CENTRALDATE = DateTime.Today;//Half the workorders will be generated before this date and half after. Default it today.
int DAYSTOGENERATE = 8;//Number of days to generate test workorders for
int WORKORDERSPERDAYTOGENERATE = 10;//Number of workorders across all users per day to generate
int SCHEDULEMARKERSPERDAYTOGENERATE = 4;//Number of sched markers across all users per day to generate
int PERCENTAGEOFAPPOINTMENTSTHATSPANMORETHANONEDAY = 60;//set to zero for no multi day appointments
int MAXDAYSOFMULTIDAYAPPOINTMENTS = 5;
if (MessageBox.Show("Generate " + DAYSTOGENERATE.ToString() + " days worth of test data now?", "Generate test data", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
return;
Random r = new Random();
System.Collections.Generic.List<Guid> UserList = new System.Collections.Generic.List<Guid>();
//fill user list
UserPickList upl = UserPickList.GetList(false);
foreach (UserPickList.UserPickListInfo i in upl)
if (i.Type == UserTypes.Schedulable && i.Active)
UserList.Add(i.ID);
if (UserList.Count < 1)
throw new System.ApplicationException("Need at least one active, scheduleable user to generate data");
PriorityPickList PriorityList = PriorityPickList.GetList();
if (PriorityList.Count < 1)
throw new System.ApplicationException("Need at least one Priority to generate data");
WorkorderStatusPickList StatusList = WorkorderStatusPickList.GetList();
if (StatusList.Count < 1)
throw new System.ApplicationException("Need at least one workorder status to generate data");
ClientPickList ClientList = ClientPickList.GetList();
if (ClientList.Count < 1)
throw new System.ApplicationException("Need at least one active, client to generate data");
//Put half the items before the central date and half after
DateTime dtFrom = CENTRALDATE.AddDays(-(DAYSTOGENERATE / 2));
for (int x = 0; x < DAYSTOGENERATE; x++)
{
dtFrom = dtFrom.AddDays(1);
DateTime dt = new DateTime(dtFrom.Year, dtFrom.Month, dtFrom.Day, 8, 0, 0);
int WorkordersGenerated = 0;
while (WorkordersGenerated < WORKORDERSPERDAYTOGENERATE)
{
Workorder w = Workorder.NewItem(WorkorderTypes.Service);
w.ClientID = ClientList[r.Next(ClientList.Count)].ID;
WorkorderItem wi = w.WorkorderItems[0];
WorkorderItemScheduledUser wisu = wi.ScheduledUsers.Add(wi);
wisu.UserID = UserList[r.Next(UserList.Count)];
DateTime dtStart = dt.AddHours(r.Next(8));
DateTime dtStop = dtStart.AddHours(r.Next(0, 4)).AddMinutes(r.Next(10) > 5 ? 30 : 60);
if (dtStop == dtStart)
dtStop = dtStart.AddMinutes(30);
if (PERCENTAGEOFAPPOINTMENTSTHATSPANMORETHANONEDAY > 0)
{
//Make a multi day appointment?
if (r.Next(100) < PERCENTAGEOFAPPOINTMENTSTHATSPANMORETHANONEDAY)
{
//Add random days
dtStop.AddDays(r.Next(1, MAXDAYSOFMULTIDAYAPPOINTMENTS + 1));
}
}
wisu.StartDate = dtStart;
wisu.StopDate = dtStop;
wi.PriorityID = PriorityList[r.Next(PriorityList.Count)].ID;
wi.WorkorderStatusID = StatusList[r.Next(StatusList.Count)].ID;
w.Summary = "Test work order (" + x.ToString() + ", " + WorkordersGenerated.ToString() + ")";
wi.Summary = w.Summary.Replace("work order", "WorkorOrderItem");
w.Save();
WorkordersGenerated++;
}
int ScheduleMarkersGenerated = 0;
while (ScheduleMarkersGenerated < SCHEDULEMARKERSPERDAYTOGENERATE)
{
ScheduleMarker sm = ScheduleMarker.NewItem();
sm.ARGB = System.Drawing.Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)).ToArgb();
DateTime dtStart = dt.AddHours(r.Next(8));
DateTime dtStop = dtStart.AddHours(r.Next(0, 4)).AddMinutes(r.Next(10) > 5 ? 30 : 60);
if (dtStop == dtStart)
dtStop = dtStart.AddMinutes(30);
if (PERCENTAGEOFAPPOINTMENTSTHATSPANMORETHANONEDAY > 0)
{
//make a multi day appointment?
if (r.Next(100) < PERCENTAGEOFAPPOINTMENTSTHATSPANMORETHANONEDAY)
{
//Add random days
dtStop.AddDays(r.Next(1, MAXDAYSOFMULTIDAYAPPOINTMENTS + 1));
}
}
sm.StartDate = dtStart;
sm.StopDate = dtStop;
sm.Name = "Test marker (" + x.ToString() + ", " + ScheduleMarkersGenerated.ToString() + ")";
sm.SourceID = UserList[r.Next(UserList.Count)];
sm.ScheduleMarkerSourceType = ScheduleMarkerSourceTypes.User;
sm.Save();
ScheduleMarkersGenerated++;
}
}
MessageBox.Show("Done");
}