Previous Topic

Next Topic

Book Contents

Book Index

Script to Toggle Invoice Lines Type

This example shows you how to record a script to change an Invoice line Type from Product to Narrative or vice versa.

  1. Record a script to change the invoice line type from Product to Narrative:

    Result: Script Editor opens and displays what it has recorded:

    Dim frmINInvoice1 as Object

    frmINInvoice1 = GetTriggerObject

    if IsNull(frmINInvoice1) then frmINInvoice1 = GetActiveObject

    if IsNull(frmINInvoice1) or frmINInvoice1.ClassName <> "INInvoiceEntryForm" then Error "Wrong form class for script"

    frmINInvoice1.Line.LineType = "Narrative"

    frmINInvoice1.Line.Description = "stuff"

  2. Next we will change the script so that the Description is not set to the same thing (for example stuff) each time. We will change the script to keep the Description field selected, so we can enter a description after the script has run.

    Forms have an ActiveProperty property, that contains the form field that currently has focus. You can set the ActiveProperty to change the focus on the form. This lets us put the cursor in a field, without having to add text to the field.

    To do this, change the last line of the script to:

    frmINInvoice1.ActiveProperty = "Line.Description".

  3. Save this script with the name NarrativeLine.pfs. Leave the Script Editor form open.
  4. Now we will record a script to change the line type back to Product, as we did in Steps 1 and 2. Select a line with type Narrative, or set the line type to Narrative. Now repeat Steps 1 and 2, but change the line type to Product.

    A new Script Editor window should show the code:

    Dim frmINInvoice1 as Object

    frmINInvoice1 = GetTriggerObject

    if IsNull(frmINInvoice1) then frmINInvoice1 = GetActiveObject

    if IsNull(frmINInvoice1) or frmINInvoice1.ClassName <> "INInvoiceEntryForm" then Error "Wrong form class for script"
    frmINInvoice1.Line.LineType = "Product"
    frmINInvoice1.ActiveProperty = "Line.Description"

  5. We want to combine the two scripts, so that if the line Type Is Product the script changes it to Narrative, but if the type is Narrative the script changes it to Product. Copy the code from your second script editor, and paste it in the first script editor, after the existing code.
  6. We will modify the code to include an If statement, to check what the existing line Type is. Modify the code as follows:

    Dim frmINInvoice1 as Object

    frmINInvoice1 = GetTriggerObject

    if IsNull(frmINInvoice1) then frmINInvoice1 = GetActiveObject

    if IsNull(frmINInvoice1) or frmINInvoice1.ClassName <> "INInvoiceEntryForm" then Error "Wrong form class for script"
    If frmINInvoice1.Line.LineType = "Product" then
      frmINInvoice1.Line.LineType = "Narrative"
      frmINInvoice1.ActiveProperty = "Line.Description"
    ElseIf frmINInvoice1.Line.LineType = "Narrative" then
      frmINInvoice1.Line.LineType = "Product"
      frmINInvoice1.ActiveProperty = "Line.Description"
    End If

  7. We can even take this further so the script can be run using the same keyboard shortcut in an invoice, order or purchase order. This is a script we supply as a sample in the Demo and Live systems. Load the "ToggleLineType" script and have a look:

    'Toggle document line type between Narrative and Product

    'Script Shortcut - Keyboard shortcut only Ctrl+\

    Dim frmINInvoice1 as Object

    frmINInvoice1 = GetActiveObject

    If IsNull(frmINInvoice1) then

    'Do nothing if no form open

    ElseIf frmINInvoice1.ClassName = "INInvoiceEntryForm" or frmINInvoice1.ClassName = "POPurchaseOrderEntryForm" or frmINInvoice1.ClassName = "OESalesOrderEntryForm" then

    If frmINInvoice1.Editing then

    If frmINInvoice1.Line.LineType <> "Narrative" then

    frmINInvoice1.Line.LineType = "Narrative"

    frmINInvoice1.ActiveProperty = "Line.Description"

    ElseIf frmINInvoice1.Line.LineType = "Narrative" then

    frmINInvoice1.Line.LineType = "Product"

    frmINInvoice1.ActiveProperty = "Line.ProductCode"

    End If

    Else

    Abort(frmINInvoice1.ClassName & " not in Edit mode, cannot change line type.")

    End If

    ElseIf frmINInvoice1.ClassName = "JCBatchTransactionEntryForm" or frmINInvoice1.ClassName = "JCTimesheetTransactionEntryForm" then

    If frmINInvoice1.Editing then

    If frmINInvoice1.Line.TransactionType <> "Narrative" then

    frmINInvoice1.Line.TransactionType = "Narrative"

    frmINInvoice1.ActiveProperty = "Line.Description"

    ElseIf frmINInvoice1.Line.TransactionType = "Narrative" then

    frmINInvoice1.Line.TransactionType = frmINInvoice1.TransactionType

    If frmINInvoice1.CostCentreCode <> "" Then frmINInvoice1.Line.CostCentreCode = frmINInvoice1.CostCentreCode

    If frmINInvoice1.ComponentCode <> "" Then frmINInvoice1.Line.ComponentCode = frmINInvoice1.ComponentCode

    If frmINInvoice1.ProductCode <> "" Then frmINInvoice1.Line.ProductCode = frmINInvoice1.ProductCode

    If frmINInvoice1.CreditorCode <> "" Then frmINInvoice1.Line.CreditorCode = frmINInvoice1.CreditorCode

    If frmINInvoice1.Unit <> "" Then frmINInvoice1.Line.Unit = frmINInvoice1.Unit

    frmINInvoice1.ActiveProperty = "Line.TransactionQuantity"

    End If

    Else

    Abort(frmINInvoice1.ClassName & " not in Edit mode, cannot change line type.")

    End If

    ElseIf frmINInvoice1.ClassName = "JCJobForm" then

    If frmINInvoice1.Editing then

    If frmINInvoice1.Estimate.EstimateLineType <> "Narrative" then

    frmINInvoice1.Estimate.EstimateLineType = "Narrative"

    frmINInvoice1.ActiveProperty = "Estimate.Description"

    ElseIf frmINInvoice1.Estimate.EstimateLineType = "Narrative" then

    frmINInvoice1.Estimate.EstimateLineType = "Material"

    frmINInvoice1.ActiveProperty = "Estimate.ProductCode"

    End If

    Else

    Abort(frmINInvoice1.ClassName & " not in Edit mode, cannot change line type.")

    End If

    ElseIf frmINInvoice1.ClassName = "APShipmentEntryForm" then

    If frmINInvoice1.Editing AND frmINInvoice1.ActivePage = 2 then ' if in edit mode and in the lines tab

    If frmINInvoice1.Line.LineType <> "Narrative" then

    frmINInvoice1.Line.LineType = "Narrative"

    frmINInvoice1.ActiveProperty = "Line.Description"

    ElseIf frmINInvoice1.Line.LineType = "Narrative" then

    frmINInvoice1.Line.LineType = "Product"

    frmINInvoice1.ActiveProperty = "Line.ProductCode"

    End If

    Else

    Abort(frmINInvoice1.ClassName & " not in Edit mode or not in the Lines tab, cannot change line type.")

    End If

    Else

    'Do nothing if wrong form open

    End If

    frmINInvoice1 = Nothing

The first two lines document what the script is for. The apostrophe at the start of the line "remarks" out the line so it is not treated as part of our code. The script has been structured to fail silently if run in the wrong place, but to tell the user what is wrong if run in the right place but wrong context, such as if you are not editing your document.