Scripting a custom workflow for a project

a.c.pro supports scripting using the standard VBScript language. a.c.pro raises events which you can handle in your script.

The following events are supported:

Event Reference

Function Item_OnNew (item)

This event is raised when an item has been created and set to its default state. It can be used to override system defaults for an item.


Function Item_OnChange (item, oldItem)

This event is raised when an item is about to be saved (but before a.c.pro has carried its standard workflow). It can be used to prevent certain changes before a.c.pro has reacted to them (by raising an error).


Function Item_OnChange2 (item, oldItem)

This event is raised when an item is about to be saved after a.c.pro has carried its standard workflow. It can be used to react to changes of status or assignment etc.


Function Item_OnDelete (item)

This event is raised when an item is about to be deleted. It can be used for instance to prevent deletion under certain circumstances.


Code Examples

Function Item_OnNew (item)
  ' initialize all items to status 5
  item.status =  5
End Function


Function Item_OnNew (item)
  ' only allow a particular user to enter items in this project
  if item.session.loggedonuser.usernr <> 215 then Err.Raise 99, "Our Workflow", "Only Fred is allowed to create items in this project"
End Function


 Function Item_OnChange (item, oldItem)
  if CStr(item.AssignedToUserNumber) <> CStr(oldItem.AssignedToUserNumber) then
     item.description = item.description & vbcrlf & "Assignment changed!"
  end if
  Item_OnChange = true ' continue with default processing
End Function


 
Function Item_OnChange (item, oldItem)
   ' only allow a result reference to be set if the status is final
   if item.ResultReferenceNr <> "" and not item.IsStatusTerminal then
      Err.Raise 1, "Item_OnChange", "Result can only be entered for an item in a final status"
   end if
   Item_OnChange = True
End Function


 

Function Item_OnChange(item, oldItem)

  ' if an item in the Offers project has been set to "accepted by customer" status, then create an entry in the "Invoices" project

  Dim createInvoiceEntry: createInvoiceEntry = False

  if item.statusName = "Accepted by customer"  then

    if oldItem is nothing then ' its a new item

      createInvoiceEntry = true

    else ' its a change to an existing item

      if item.status <> oldItem.status then createInvoiceEntry = true

    end if

  end if

           

  if createInvoiceEntry then

      dim newItem: set newItem = item.session.NewItem

      newItem.projectName = "Invoices"

      newItem.statusName = "Open"

      newItem.title = "Offer " & item.itemNumber & " accepted"

      newItem.description = item.title & vbcrlf & vbcrlf & item.description

      newItem.AssignTo "fred" ' either the userid or email address can be used for AssignTo

      newItem.Save True

      item.session.AddMessage "AC invoicing item " & newItem.itemNumber & " created"

  end if

  Item_OnChange = true ' continue with default processing

End Function


Function Item_OnDelete (item)
  if CStr(item.AssignedToUserNumber) <> CStr(item.Session.LoggedOnUser.UserNr) then
     item.Session.AddMessage "You are only allowed to delete items assigned to you."
     Item_OnDelete = false
  else
     Item_OnDelete = true
  end if
End Function


Function Item_OnChange (item, oldItem)
  ' suppress mails
   item.SuppressMail = True
   Item_OnChange = True
End Function