AyaScript: close selected service workorders
Applies to AyaNova 7 (latest release, 7.6.1). Using AyaNova 8? See the AyaNova 8 manual.
What it does
This script closes many service workorders at once. Run it from the Service Workorders list. For each workorder you have checked in the list, it:
- skips the workorder if it is already Closed,
- sets Service Completed, which must be set before a workorder can be closed,
- sets Closed. AyaNova then sets the workorder status to the closed workorder status chosen in Global Settings, as it does when you close a workorder by hand.
Any workorder that can't be saved, for example because a required field is missing, is left unchanged. The script lists those workorders and the reasons in a window you can copy from, then shows Done!
Before you start
- You need the free AyaScript plug-in installed.
- Closing workorders can't be undone in bulk. Back up your database, or try the script on a copy first.
- Run it as a user who is allowed to close workorders, such as the AyaNova Administrator.
Add the script
- In AyaNova, open the AyaScript plug-in menu and select the first option, AyaScript Editor.
- Create a new script.
-
Replace all of its text with the script below,
starting at the line
//Script created:, and save it.
Run it
- In the Service navigation pane, open the Service Workorders grid. Use the workorder level, not a lower grid such as Items.
- Check the row selector of each workorder you want set to Service Completed and Closed.
- Open the AyaScript plug-in menu and select Close selected workorders.
If you see Current user not authorized to open a workorder record, make sure you started from the Service Workorders grid. If it persists, try closing that workorder by hand: AyaNova shows a clearer message there, and anything that fails by hand fails in the script too.
The script
The script does not change the workorder status itself,
because closing does that. To set it explicitly anyway,
remove the // in front of the
w.WorkorderService.WorkorderStatusID line.
//Script created: 2/5/2013
//Name: Close selected workorders
//ShowInMenuFor: List of AyaNova objects
//HandlesAyaNovaTypes: WorkorderService
public static void AyaScriptMain(bool IsList, RootObjectTypes objectType, Object ayaNovaObject, List<Guid> objectIDList)
{
//Reserve a place to store any errors during operation
System.Text.StringBuilder sbErrors=new StringBuilder();
foreach (Guid i in objectIDList)
{
//Fetch the workorder object from it's ID and do not place it in the most recently used object list (NoMRU)
Workorder w = Workorder.GetItemNoMRU(i);
//Already closed?
if (w.Closed)
continue;//It's already closed so move on to next one
//If we're here it's not closed so let's close it..
//SET WORKORDER STATUS
//Note: There is no need to set the workorder status to the global settings automatical status on close
//as that will be done automatically when you set it to closed=true below, however if for some reason you want to do it
//anyway, it would be done here like this, just uncomment the following line:
// w.WorkorderService.WorkorderStatusID = AyaBizUtils.GlobalSettings.WorkorderClosedStatus;
//First set it to service completed as that is a prerequisite to close it
w.ServiceCompleted = true;
//Now close it. No need to set closed workorder status since it automatically will do it here
w.Closed = true;
//Make sure there are no broken rules and it can be saved
if (w.IsSavable)
w.Save();//save it
else
{
//Whups, there were broken rules for some reason so save it to display at the end
sbErrors.AppendLine("Workorder " + w.WorkorderService.ServiceNumber.ToString() + " is not saveable:");
sbErrors.AppendLine(w.BrokenRulesText);
sbErrors.AppendLine("****************************************");
}
}
//display errors if necessary
if (sbErrors.Length > 0)
{
ASCopyableMessageBox d = new ASCopyableMessageBox(sbErrors.ToString());
d.ShowDialog();
d.Dispose();
}
MessageBox.Show("Done!");
}