Coming soon: Your brand new Help Center & Community! Get a sneak-peek here

vbscript for OnCurrentContactFieldChanged(fieldn­ame)

Hello, 

We have previously created a COM-script for one of our customers that validate different fields on a contact before they are allowed to save. 

The script looks like this: 

Dim errorMessageContact
Dim validationMessageContact
 
Sub OnCurrentContactFieldChanged(fieldname)
 	If CurrentContact.Identity = 0 Then
		if (CurrentContact.Category.Id  = 0) then
			CurrentContact.ValidationMessage = "Du må sette en kategori"
		else
			CurrentContact.ValidationMessage = ""
		End if	
		If CurrentContact.Category.Id = 3 OR CurrentContact.Category.Id = 12 Then
			validationMessageContact = 0
			ValidateContactPostalAddressField()
			ValidateContactPostalZipField()
			ValidateContactPostalCityField()
			ValidateContactBusinessField()
			ValidateContactPhoneField()
			ValidateContactEmailField()
		End if	
	End if
End Sub

First i check if this is a new contact, then i first validate if they have set a category. 
The rest is pretty much the same as above, i validate postal-fields, business, phones and email if the category is of id 3 or 12. I have created Subs for all the individual fields and just call the sub from the event as shown above. 

This works as it should, the save-button is disabled as expected, but the client is 'laggy' when i input anything in a field. 
This is 100% because of our script and im not sure if there is a better way of solving this. 

As far as i know i cant prevent an actuall save in the BeforeSaveContact-event, i can only route the user back to the same contact in edit-mode.. I dont want the contact to be saved at all unless these fields are filled out. 

There is no other scripts running on contact so its only this code that makes the COM-client laggy. 
Any ideas? 

/Eivind


RE: vbscript for OnCurrentContactFieldChanged(fieldn­ame)

I am afraid this is a hopeless undertaking, particularly if you must do deep validation. I.o.w. verifying that e-mails, phones and addresses are not crap. If you don't do deep validation but merely verify that there is some string data, then what is the point?

As I see it, the only real issue you have is that you want the Category field to be mandatory.
Here is a vb script with which you can achieve that:

Set app = CreateObject( "SuperOffice.Application" )
If Not app Is Nothing Then
    Set prefs = app.Database.Preferences
    ' 1 is the flag for true
    ' 2 if the default acces level for "SystemWide"
    prefs.Set "FieldMandatory", "Contact-CATEGORY_IDX", 1, 2 
Else
    MsgBox "Failed to get application i­nstance!", vbOKOnly, ""
End If

The down-side might be that category will also become mandatory on existing records, whenever you go into Edit mode.

Conrad

 

Af: Conrad Weyns 3. jun 2019

RE: vbscript for OnCurrentContactFieldChanged(fieldn­ame)

Hello Conrad, 

Not sure I understood your reply why this is 'redundant' but I figured out a logical workaround anyways: 

I check if any of the important fields are the one being edited before i do any validation:

Sub OnCurrentContactFieldChanged(fieldname)
	If CurrentContact.Identity = 0 Then
		'SoMessageBox fieldname
		If CurrentContact.Category.Id = 0 OR CurrentContact.Business.Id  = 0 Then
			validationMessageContact = "Du må velge en kategori og bransje før du kan gå videre"
		Else
			If fieldname = "contact.CATEGORY_IDX" OR fieldname = "contact.BUSINESS_IDX" OR fieldname = "address.ADDRESS1" OR fieldname = "address.ZIPCODE" OR fieldname = "address.CITY" OR fieldname = "phone.PHONE" OR fieldname = "email.EMAIL_ADDRESS"  Then
				validateAll()
			End if
		End if
		If validationFieldCount > 0 OR validationMessageContact = "Du må velge en kategori og bransje før du kan gå videre"  Then
			CurrentContact.ValidationMessage = validationMessageContact
		Else 
			CurrentContact.ValidationMessage = ""
		End if
	End if
End Sub

Now I can input the Contact name without validating all the fields on each character input so its good enough :)


//Eivind

Af: Eivind Johan Fasting 4. jun 2019