Page 1 of 1

Upgrading custom field dates from Jiwa 6

PostPosted: Thu Apr 04, 2019 11:30 am
by pricerc
If we're upgrading custom form data that has dates, would there be merit in converting them to ISO format from AU format ?

Re: Upgrading custom field dates from Jiwa 6

PostPosted: Thu Apr 04, 2019 11:33 am
by pricerc
Also,

is there a particular reason why CustomFieldValue.GetValue<T> returns a System.Object and not a <T> ?

Re: Upgrading custom field dates from Jiwa 6

PostPosted: Thu Apr 04, 2019 11:42 am
by Mike.Sheen
pricerc wrote:If we're upgrading custom form data that has dates, would there be merit in converting them to ISO format from AU format ?


Yes - it would be less ambiguous and the result would be consistent formats.

pricerc wrote:Also,

is there a particular reason why CustomFieldValue.GetValue<T> returns a System.Object and not a <T> ?


I'm not seeing that in 7.2 - What version are you using? Maybe older versions did return an object type.

The 7.2 function looks like this:
Code: Select all
Public Function GetValue(Of T)(Name As String, DefaultValue As T) As T
   Dim customFieldValue As CustomFieldValue = ItemFromSettingName(Name)
   If customFieldValue Is Nothing Then
      Return DefaultValue
   Else
      Return customFieldValue.GetValue(DefaultValue)
   End If
End Function


Definitely not object.

Re: Upgrading custom field dates from Jiwa 6  Topic is solved

PostPosted: Thu Apr 04, 2019 11:44 am
by Mike.Sheen
Oh, you're referring to CustomFieldValue class - I was looking at the CustomFieldValueCollection class.

Yes, that returns object. No reason I guess other than nobody bothered to cast the result to T.

Re: Upgrading custom field dates from Jiwa 6

PostPosted: Thu Apr 04, 2019 1:31 pm
by pricerc
Mike.Sheen wrote:No reason I guess other than nobody bothered to cast the result to T.


That'll be because it's a pain and/or the results suck.

You could do
Code: Select all
return (T) Convert.ChangeType(textValue, typeof(T));


But it's not that robust, and the VB library functions are a bit friendlier.

E.g. CBool(value) parses more values than Boolean.Parse(value). Although neither of them parse "yes" and "no".

Re: Upgrading custom field dates from Jiwa 6

PostPosted: Thu Apr 04, 2019 2:10 pm
by pricerc
So,

based on the customer's data, and knowing they don't only access the data from the front-end.... I've done a set of overloaded extension methods called GetValueT (too lazy to be more creative):

Code: Select all
        <Extension()>
        Public Function GetValueT(this As CustomFields.CustomFieldValue, ByVal DefaultValue As DateTime) As DateTime
            If String.IsNullOrWhiteSpace(this.Contents) Then
                Return DefaultValue
            End If
            Dim result As DateTime
            If DateTime.TryParseExact(this.Contents, "o", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, result) Then
                ' preferred, new default format
                Return result
            ElseIf DateTime.TryParseExact(this.Contents, "yyyy-MM-dd", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, result) Then
                ' truncated ISO format
                Return result
            ElseIf DateTime.TryParseExact(this.Contents, "dd/MM/yyyy", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, result) Then
                ' legacy Jiwa format
                Return result
            ElseIf DateTime.TryParse(this.Contents, result) Then
                ' generic format
                Return result
            End If
            Return DefaultValue
        End Function


for the main one I care about.

and some others:

Code: Select all

        <Extension()>
        Public Function GetValueT(this As CustomFields.CustomFieldValue, ByVal DefaultValue As Integer) As Integer
            If String.IsNullOrWhiteSpace(this.Contents) Then
                Return DefaultValue
            End If

            Try
                ' because we're in VB, although Int32.TryParse should work as well
                Return CInt(this.Contents.Trim())
            Catch ex As Exception
                Return DefaultValue
            End Try
        End Function

        <Extension()>
        Public Function GetValueT(this As CustomFields.CustomFieldValue, ByVal DefaultValue As Double) As Double
            If String.IsNullOrWhiteSpace(this.Contents) Then
                Return DefaultValue
            End If

            Try
                ' because we're in VB, although Double.TryParse should work as well
                Return CDbl(this.Contents.Trim())
            Catch ex As Exception
                Return DefaultValue
            End Try
        End Function

        <Extension()>
        Public Function GetValueT(this As CustomFields.CustomFieldValue, ByVal DefaultValue As Decimal) As Decimal
            If String.IsNullOrWhiteSpace(this.Contents) Then
                Return DefaultValue
            End If

            Try
                ' because we're in VB, although Decimal.TryParse should work as well
                Return CDec(this.Contents.Trim())
            Catch ex As Exception
                Return DefaultValue
            End Try
        End Function

        <Extension()>
        Public Function GetValueT(this As CustomFields.CustomFieldValue, ByVal DefaultValue As Boolean) As Boolean
            If String.IsNullOrWhiteSpace(this.Contents) Then
                Return DefaultValue
            End If

            ' Even VB doesn't handle things like 'yes' and 'no' as boolean values.
            ' So because I'm interested in replacing legacy textbox or combobox yes/no fields with booleans,
            ' I'm going to do my own parsing.
            If {"true", "yes", "y", "1", "-1"}.Contains(this.Contents.Trim().ToLowerInvariant()) Then
                Return True
            End If

            If {"false", "no", "n", "0"}.Contains(this.Contents.Trim().ToLowerInvariant()) Then
                Return False
            End If

            ' could arguably include a test here for any non-zero integer to get a VB 'True'.

            ' if we get here, the value doesn't make sense.
            Return DefaultValue
        End Function


Re: Upgrading custom field dates from Jiwa 6

PostPosted: Thu Apr 04, 2019 2:42 pm
by Mike.Sheen
You could also use the existing GetValue method of the CustomFieldValueCollection.

Code: Select all
Dim x as Boolean = customFieldValue.CustomFieldValueCollection.GetValue(Of Boolean)(customFieldValue.CustomField.PluginCustomField.Name, false)

Re: Upgrading custom field dates from Jiwa 6

PostPosted: Thu Apr 04, 2019 3:33 pm
by pricerc
Mike.Sheen wrote:You could also use the existing GetValue method of the CustomFieldValueCollection.

Code: Select all
Dim x as Boolean = customFieldValue.CustomFieldValueCollection.GetValue(Of Boolean)(customFieldValue.CustomField.PluginCustomField.Name, false)


too many keystrokes.

But more importantly, in context, this is in the Sales UI, and I am already looping through all the custom fields for the debtor in question, getting some data out for the sales order. So I have a

Code: Select all
            For Each dbv As CustomFieldValue In debtor.CustomFieldValues
                Select Case dbv.Name
                    Case "Foo"
                        foo = dbv.Contents
                    Case "Bar"
                        bar = dbv.Contents.ToUpperInvariant()
                    Case "When"
                        when = dbv.GetValueT(#01/01/2000#)
                    Case "meh"
                        meh = dbv.Contents
...
                End Select
            Next