Set field mandatory on sale

Hi Guys,

I'm not familiar with scripting but i'm trying to create my first script for the Windows client 7.5 :-) A customer wants to make 2 fields mandatory. 

I was able to create a script using OnCurrentSaleBeforeSave. When one of the 2 fields=0, I get a message as expected.

The problem is that the sale is still saved. Is it posible to cancel the save or reopen the current sale so you have to fill in the fields before you continue? I used the folowing script:

sub OnCurrentSaleBeforeSave

'******* Give message if cost=0
if currentsale.Amount=0 then SOMessagebox "Fill in the ammount to continue"

'******* give message if amount >0 but cost=0 ******* 
if currentsale.Cost=0 and currentsale.Amount >0 then SOMessagebox "Fill in the total cost to continue"
End sub

 

Thanks in advance.

RE: Set field mandatory on sale

Hi Alexander,

You want to also set the CurrentSale.ValidationMessage property to a textual description why the sale should not be saved. When the sale is valid, then remove the ValidationMessage (set it to "") to reenable the Save button.

It's probably better to react when the user is changing fields rather than waiting until the Save button is clicked. Here is a good representation of how to do that. However, using the "FieldsChanged" events are more useful and the recommended approach these days. 

Hope this helps!

Af: Tony Yates 14. jun 2017

RE: Set field mandatory on sale

Hi Tony,

Thanks, I will look into these examples :-) I've noticed some events are a bit changed and won't work on 7.5 or 8. I will try the FieldChanged events.

Af: Alexander Hesselberth 14. jun 2017

RE: Set field mandatory on sale

The OnCurrentSaleFieldChanged(fieldname) event did the trick :-) Thanks Tony.

Af: Alexander Hesselberth 14. jun 2017

RE: Set field mandatory on sale

Hi,
This seems to be a recurring issue.
I tend to say that a Sentry plugn is the way to go.
Trying to control this with script events and dialogs is a struggle!

But there is a poor man's sentry plugin alternative for setting known db fields to mandatory using a userpreference.

For sale.amount, try this vb script:

----------------------------------------------------------------------
Set app = CreateObject( "SuperOffice.Application" )
If Not app Is Nothing Then
    Set db = app.Database
    Set prefs = db.Preferences

    section = "FieldMandatory"
    key = "Sale-amount"
    value = "1"
    prefs.Set section, key, value, 2
End If
----------------------------------------------------------------------

You can change the value from "1" to "0" and then put the Sale card into Edit mode to see the changes. You should see the Amount field get the red underline.
If this does not work, then you may have a case sensitivity issue. I can't remember if our Win preference system is case sensitive or not. But I have seen scripts fail after an upgrade due to db field names case changes!.

(No idea where this is supposed to be documented or if it is supported by the Web client!)

Now, if only it were that simple....
Cost is a calculated field so there is no db field for it. There is for earning and earning_percent but all of these are mutually dependent and our model logic attempts to keep these at least relevant..

That said, with some help from our GUI you might get lucky!
Try extending the vb script to:

--------------------------------------------
Set app = CreateObject( "SuperOffice.Application" )
If Not app Is Nothing Then
    Set db = app.Database
    Set prefs = db.Preferences

    section = "FieldMandatory"

    key = "Sale-amount"
    value = "1"
    prefs.Set section, key, value, 2
    
    key = "Sale-earning"
    value = "1"
    prefs.Set section, key, value, 2
    
    key = "Sale-earning_percent"
    value = "1"
    prefs.Set section, key, value, 2
End If
---------------------------------------

I now get:

 

Hope this helped somewhat - Not necessarily a complete solution though!
Conrad

 

Af: Conrad Weyns 14. jun 2017

RE: Set field mandatory on sale

Hi Conrad,

Thanks, I will look into this. I remeber these sentry settings fromt the past (Version 5.x) but did no know it would still work :-)

I've solved it now with a little script:

sub OnCurrentSaleFieldChanged(fieldname)

'Amount
if currentsale.Amount=0 then 
CurrentSale.ValidationMessage=" - verkoopbedrag"
Else CurrentSale.ValidationMessage=""

'Total Cost
if currentsale.Cost=0 then
CurrentSale.ValidationMessage=" - totale kosten"
Else CurrentSale.ValidationMessage=""
End if
End if
End sub

Af: Alexander Hesselberth 15. jun 2017

RE: Set field mandatory on sale

Ok, good.
ValidationMessage is a nice addition.
You may also want to (or have to) set it when the Sale panel goes into edit mode.
7.5 is long time ago, I have no environment available to check this, but in 8.x you should be garanteed to get proper OnEditSaleClicked and OnEditSaleComplete messages.
Use the Script Event viewer to verify.

My point is that I don't think that hooking the FieldChanged message will suffice.
There might be some holes then.

/Conrad

Af: Conrad Weyns 15. jun 2017