Every time an invoice is being issued or stored you are able to execute a script on SQL

That can happen on setting-> Purchases/Sales->Documents->Run SQL

EMDIs variables that is send to SQL:

:aa  Documents line number
:cd  Type of Documents line number
:rd  Related Document
:pd Parent Document
:cl   Client line number
:bl  Line number of delivery customer
:as  Line number of assignor
:re  Line number related Document

The result can be saved on any field that has been run. There is the field #CUS that is splitted in 5 parts with the symbol | , #CUS1, #CUS2, #CUS3, #CUS4, #CUS5.