Skip to content

Scripts (Script manager) (EN)

Control Panel › Script manager – small Groovy scripts for a kind of record and a group. Left: all scripts; right: the editor.

The script manager is the preferred place for template fields: one script for "All documents" works for every invoice, offer, order, credit note … – nothing to enter per record.

For whom: "For" and "Group"

For Applies to
All documents invoices, offers, orders, credit notes, part payment and deposit invoices, delivery notes, order confirmations
Invoice, Offer, Order, Credit note, … only documents of this type (by the type of the document)
All contacts every contact
Customer, Supplier, Manufacturer contacts with this role
Product, Purchase order, Reminder, Conversation, … records of this kind

Group: "All groups", or a group – then the script applies to records of this group and its subgroups.

Several scripts can apply to a record: general ones first (All documents, All groups), then specific ones (the type, deeper groups). For template fields of the same name the more specific one wins – e.g. gruss for All documents, and a different gruss for invoices only. Hooks (modifyOnSave …) all run, in this order.

What a script does: its name

Name ("Use as") When What it can do
any name, e.g. anrede when exporting with a template its result is the template field <item.property.anrede> (documents), <item.contact.property.anrede> in documents and <contact.property.anrede> on the contact sheet (contact scripts), <product.property.anrede> (products) – the data is not changed
modifyOnSave before a record is saved change the data, e.g. clean up a text
executeAfterSave after saving e.g. a notice, another record
executeAfterCreate after a record is created e.g. defaults
modifyOnDelete before deleting throw an exception to stop the deletion
IntItemLabel when a document is shown its result is the name of documents in lists
IntRevenueLabel when a revenue is shown its result is the name of revenues in lists

Choose the purpose with Use as; the line below the name shows the template field the result appears in.

In a script

The fields of the record have their template names, without <> – raw values (true/false, numbers, dates):

item.cnumber, item.netvalue, item.dateadded, item.contact.cname, item.contact.ismale, item.contact.property.anrede in document scripts
contact.cname, contact.prename, contact.ismale, contact.iscompany, contact.property.NAME in contact scripts
product.cnumber, product.cname … in product scripts
dbo the record itself, e.g. dbo.__getCname()
getObject('contact', 'Smith') another record by kind and name
getFormField('item.netvaluef') a formatted template field

The last value is the result. Scripts may span several lines; they must not contain the script symbol #.

Examples

Insert example… puts a ready script for the chosen kind into the editor (salutation, text depending on the amount, customer number, date format, trim before saving, name in lists).

Salutation for all documents – For: All documents, name anrede:

item.contact.iscompany ? "Dear Sir or Madam," : (item.contact.ismale ? "Dear Mr " : "Dear Mrs ") + item.contact.cname + ","

In every document template: <item.property.anrede>.

Text depending on the amount – name shipping:

item.netvalue >= 500 ? "Free shipping." : "Plus shipping costs."

modifyOnSave for all documents – trim the description:

dbo.setDescription(String.valueOf(dbo.__getDescription() ?: '').trim())

A contact's own field without a script (one contact only): contact › Properties, name delivery, value 2 weeks → <item.contact.property.delivery>.

Test

Test with a record… runs the script on a record you choose and shows the result and all variables of the record as they are written in a script (item.contact.cname …) – handy to find the right name.

Toolbar actions

Toolbar actions… (button in the script manager; was View › Add toolbar action): own buttons in a toolbar that run a Groovy script. In these scripts: MPView (the main window), Application, QueryHandler. Takes effect after a restart.


Deutsch: Skripte