Upgrading custom field dates from Jiwa 6  Topic is solved

Discussions relating to Jiwa 7 plugin development, and the Jiwa 7 API.

Upgrading custom field dates from Jiwa 6

Postby pricerc » Thu Apr 04, 2019 11:30 am

If we're upgrading custom form data that has dates, would there be merit in converting them to ISO format from AU format ?
/Ryan

ERP Consultant,
Advanced ERP Limited, NZ
https://aerp.co.nz
User avatar
pricerc
Senpai
Senpai
 
Posts: 518
Joined: Mon Aug 10, 2009 12:22 pm
Location: Auckland, NZ
Topics Solved: 21

Re: Upgrading custom field dates from Jiwa 6

Postby pricerc » Thu Apr 04, 2019 11:33 am

Also,

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

ERP Consultant,
Advanced ERP Limited, NZ
https://aerp.co.nz
User avatar
pricerc
Senpai
Senpai
 
Posts: 518
Joined: Mon Aug 10, 2009 12:22 pm
Location: Auckland, NZ
Topics Solved: 21

Re: Upgrading custom field dates from Jiwa 6

Postby Mike.Sheen » Thu Apr 04, 2019 11:42 am

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.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

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

Postby Mike.Sheen » Thu Apr 04, 2019 11:44 am

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.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

Re: Upgrading custom field dates from Jiwa 6

Postby pricerc » Thu Apr 04, 2019 1:31 pm

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".
/Ryan

ERP Consultant,
Advanced ERP Limited, NZ
https://aerp.co.nz
User avatar
pricerc
Senpai
Senpai
 
Posts: 518
Joined: Mon Aug 10, 2009 12:22 pm
Location: Auckland, NZ
Topics Solved: 21

Re: Upgrading custom field dates from Jiwa 6

Postby pricerc » Thu Apr 04, 2019 2:10 pm

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

/Ryan

ERP Consultant,
Advanced ERP Limited, NZ
https://aerp.co.nz
User avatar
pricerc
Senpai
Senpai
 
Posts: 518
Joined: Mon Aug 10, 2009 12:22 pm
Location: Auckland, NZ
Topics Solved: 21

Re: Upgrading custom field dates from Jiwa 6

Postby Mike.Sheen » Thu Apr 04, 2019 2:42 pm

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)
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

Re: Upgrading custom field dates from Jiwa 6

Postby pricerc » Thu Apr 04, 2019 3:33 pm

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
/Ryan

ERP Consultant,
Advanced ERP Limited, NZ
https://aerp.co.nz
User avatar
pricerc
Senpai
Senpai
 
Posts: 518
Joined: Mon Aug 10, 2009 12:22 pm
Location: Auckland, NZ
Topics Solved: 21


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 4 guests

cron