Page 1 of 1

Creating new custom field value

PostPosted: Wed Aug 02, 2023 12:23 pm
by DannyC
Back in 7.0.175 if there was a missing customFieldValue we could use
Code: Select all
customFieldValue = New JiwaApplication.CustomFields.CustomFieldValue
customFieldValue.CustomField = item.SalesOrderLines.SalesOrder.LineCustomFields.ItemFromSettingName("FieldName", "My plugin name")
item.CustomFieldValues.Add(customFieldValue)


but for 7.2.1 I assume we need to use a factory instead of New?
What is that syntax exactly? (vb for this one!)

Re: Creating new custom field value  Topic is solved

PostPosted: Wed Aug 02, 2023 12:34 pm
by Mike.Sheen
The correct way would be:
Code: Select all
 Dim customFieldValue As CustomFieldValue = Manager.CollectionItemFactory.CreateCollectionItem(Of CustomFieldValue)()


But you won't ever have to do that.

When we read the host items (sales order lines in your case), we call the line CustomFieldValueCollection.Read which initialises a CustomFieldValue for you. When a sales order line is added to it's own collection, we also at that point initialise the custom field values for you.

You should never come across the custom field value being null and requiring you initialise and add it yourself.

Re: Creating new custom field value

PostPosted: Wed Aug 02, 2023 12:49 pm
by DannyC
You should never come across the custom field value being null and requiring you initialise and add it yourself


Perfect. I just came across an old plugin from 7.0.175 which used that method. I'm wanting to upgrade to 7.2.1. I'll rem that line & we should be good.

Thanks for the quick response.

Re: Creating new custom field value

PostPosted: Wed Aug 02, 2023 6:12 pm
by Mike.Sheen
On the topic of custom fields, things have evolved a fair bit over time and we have some methods of convenience to make it easier to get and set custom field values - you may or may not be aware of them.

Firstly, there is the GetValue generic method:

Code: Select all
Public Function GetValue(Of T)(Name As String, DefaultValue As T) As T


An example of using it to get a sales order line custom field value for a checkbox custom field named "IsWarrantyItem" :

Code: Select all
Dim IsWarranty As Boolean = SalesOrderLine.CustomFieldValues.GetValue(Of Boolean)("IsWarrantyItem", False)


We do all the conversions for you and return the appropriate value of the appropriate type - including a default value if none was ever set.

There is the expected SetValue method also, which is the inverse of the above - to set the custom field value:

Code: Select all
Public Overloads Sub SetValue(Of T)(Name As String, NewValue As T)


An example of setting a value to True:
Code: Select all
SalesOrderLine.CustomFieldValues.SetValue(Of Boolean)("IsWarrantyItem", True)


And what if you don't have a sales order line, but you want to get the value of a custom field? There are shared (static in c#) helper methods to assist:

Code: Select all
Public Shared Function ReadCustomFieldValue(Manager As JiwaApplication.Manager, CustomFieldModuleName As String, ID As String, SettingName As String) As String


Which could be used with the previous examples like:
Code: Select all
Dim isWarrantyItem As String = JiwaFinancials.Jiwa.JiwaApplication.CustomFields.CustomFieldValueCollection(Manager, "Sales Order Line", "56584b8c4f8744c3af06", "IsWarrantyItem")


In the above example, "Sales Order Line" is the ModuleName from SY_PluginCustomSettingModules - that's the list of Modules seen on the Custom Fields of the Plugin Maintenance form.
"56584b8c4f8744c3af06" is the Sales order invoice line id (SO_Lines.InvoiceLineID) and "IsWarrantyItem" is the custom field name.

There are other overloads of the method ReadCustomFieldValue, but the above is probably the easiest to use.