Page 1 of 1

Best sales order event for all new orders

PostPosted: Thu Jun 01, 2023 11:34 am
by DannyC
I need to write a text string to a sales order custom field when a sales order gets created.
The sales order could be created via the GUI, the REST API, file watcher

The value is dependent upon a characteristic of the debtor, in this case the value for Category5.

Obviously (!) I need to write the plugin in the business logic section.
However which event should I use?
salesOrder.Created
salesOrder.CreateEnd
salesOrder.Creating

salesOrder.Created works well for orders raised via the Jiwa GUI. The user clicks save when ready.
Is that event still applicable for imported orders via REST API etc?

For now, I am doing the stuff on SaveStart and testing for
Code: Select all
if (salesOrder.InvoiceNo =="")
but I really don't need or want the SaveStart code to fire on every save - there's no need. But if that's the best way, cool.

Re: Best sales order event for all new orders

PostPosted: Thu Jun 01, 2023 12:26 pm
by pricerc
If you handle the events in a "BusinessLogicPlugin" class and not a "FormPlugin" Class, then it should apply regardless of how the SalesOrder is created.

Re: Best sales order event for all new orders  Topic is solved

PostPosted: Thu Jun 01, 2023 12:58 pm
by Mike.Sheen
All our business logic for maintenance type forms (which sales orders is) has a CreateEnd event raised when a new order is created.

That didn't entirely suit our needs for sales orders, as we also wanted to know when an order was created if it was from a quote or job cost invoice or whatever - so we added another event, Created which includes parameters indicating what type of new sales order this is (e_NewSalesOrder (0), e_NewSalesOrderCreditNote(1), e_NewSalesOrderJobCost(2) or e_NewSalesOrderQuote(3)) and also a nullable quote object which is not null if the order was created from a quote.

So, when a sales order is created, the business logic raises two events:

CreateEnd(sender As Object, e As System.EventArgs)
and then
Created(sender As Object, e As System.EventArgs, ByVal NewSalesOrderType As NewSalesOrderTypes, ByRef SourceQuoteObject As JiwaSales.SalesQuote.SalesQuote)

Both events get fired regardless of of where it was created - form, REST API - even a a custom 3rd party application.

In either event you can set your custom field contents there and everything will work as you expect.

However, you mention you are setting the sales order custom field based on the value of a debtor category 5 - so you'll also want to listen to the Salesorder.Debtor.Read event as if the user changes the debtor after creating the order, you'll need to know about that.

Your test in SaveStart for InvoiceNo =="" might work, but it won't always. If in the SaveStart event you want to know if the order is being saved for the first time, check the InsertFlag property instead. That will be True if the order being saved is a new order. You cannot rely on InvoiceNo =="" as a system configuration can be set to allow the user to key an invoice no in for a new order, so that test won't work in that scenario.

Re: Best sales order event for all new orders

PostPosted: Thu Jun 01, 2023 1:10 pm
by DannyC
Understood.
Thx

Re: Best sales order event for all new orders

PostPosted: Thu Jun 01, 2023 6:02 pm
by SBarnes
Is that event still applicable for imported orders via REST API etc?


On the whole events in the business logic do not discriminate about the process they are running in, if you ever only want the event to run in the user interface then use a form plugin and attach the events there.

If you ever need to check if the business object is attached to a form there is a client property it will be null when there is no form.