This example shows you how to run a script using a script event. We will use the StartNewJob.pfs script created in Script to Assign Next Job Number.
It would be nice if this process happened whenever a new job was inserted, rather than the user needing to remember to run a script. We can do this using a Script Event - this is a hook into the application which will run a script every time a particular event occurs.
F4) to insert a new event.F2) to see the available events, and pick the appropriate event from the list. We want Job Costing > JCJobForm > AfterInsert. This will run the script after an Insert is triggered on the Job form.F9).'Add a new job and calculate the next job number
Dim frmJCJob1 as Object
frmJCJob1 = GetActiveObject
If IsNull(frmJCJob1) or frmJCJob1.ClassName <> "JCJobForm" Then
Abort("This script is run by the JCJobForm.AfterInsert .")
End If
MyType = Left(InputList("Please select job type","Job Type","Contract","In House", "One Off", "Repairs","Specials"),1)
If IsNull(MyType) then Abort("Cancelled by user.")
Dim JobData as Object
JobData = CreateObject("Accredo.JCJobData")
JobData.Find (":")
JobData.Prev
MyJob = val(JobData.JobCode) + 1
JobData = Nothing
frmJCJob1.JobCode = MyJob & MyType
frmJCJob1.Save
frmJCJob1.Edit
Note: You could do this without DI but it would be messier and slower. If all Job codes started with numbers, instead of a mix of numbers and letters as in the demo data, we would replace the lines:
frmJCJob1.Find (":")
frmJCJob1.Prev
with
frmJCJob1.Last
For an example of another use for script events have a look at the sample StartAnotherInsertAfterSave.pfs script provided. This can be hooked up to the AfterSave event on transaction and document entry forms to loop around and start another insert of the same type of transaction or document. For users who do large volumes of data processing this can be a significant time saver.
Another significant use of script events it to validate information a user has entered before allowing a transaction or document to be saved. This can be as simple as checking that a field has been filled in, or as complex as checking who the user is and whether the margin achieved on each line and overall meets specified limits. The important point is it lets you decide what is required information in addition to the information required by the system for keeping valid accounting data.