Forcing creation of custom value records.

Posted:
Tue Jul 20, 2021 9:51 am
by pricerc
If you don't add any custom data to, e.g. a sales order or quote, the underlying SQL records are not created, which I think is quite fine.
However, the customer has a 'viewer' web app (inherited from Jiwa 6; should probably convert it to using the API, but that's a different topic), and have asked if the custom values can be populated always, even if they're blank.
Is it sufficient to just 'touch' any custom value in, say, SalesOrder_SaveStart ?
I am going to have a discussion with them about maybe fixing their viewer, but thought I would ask, since there may be a good case for it, and the answer may be interesting to others.
Re: Forcing creation of custom value records.

Posted:
Tue Jul 20, 2021 10:27 am
by SBarnes
I use the save start on inventory and debtor to check for custom fields that I want to be required against a static class that contains a list of the field names so I can't see why you can't do the same sort of thing and fill in the blanks given under the hood I believe you are really dealing with a Jiwa Collection and Jiwa collection Item so all you really need to do is force the change flags to true and it should save it which putting in some sort of default value like an empty string should do.
Re: Forcing creation of custom value records. 

Posted:
Tue Jul 20, 2021 11:39 am
by Mike.Sheen
Setting the contents to anything will work, but you cannot just set the Insert or ChangeFlag - you need to also set the SettingValueID/RecID to be a newGUID.
This is the property setter of the Contents property of CustomSettingValue (ignore the first part relating to date formatting):
- Code: Select all
Set(ByVal Value As String)
If CustomFieldValueCollection IsNot Nothing AndAlso _CustomField.PluginCustomField.CellType = Plugin.CustomField.CellTypes.Date Then
If Value Is Nothing OrElse Value.Trim.Length = 0 Then
_Contents = Value
Else
Dim dateValue As DateTime
If DateTime.TryParse(Value, dateValue) Then
_Contents = dateValue.ToString("s")
Else
Throw New InvalidCastException(String.Format("Value '{0}' cannot be cast to a type of Datetime", Value))
End If
End If
Else
_Contents = Value
End If
If CustomFieldValueCollection IsNot Nothing AndAlso CustomFieldValueCollection.Displaying = False AndAlso (SettingValueID Is Nothing OrElse SettingValueID.Trim.Length = 0) Then
' We set the insert flag to be true here, because when we read
' in the custom field values, any missing values are still added
' to the collection, but with a blank or empty SettingValueID.
' If the user doesn't change the setting value, then no setting
' value is created, but if they do change it, we need to do an insert.
m_RecID = CustomFieldValueCollection.Manager.Database.NewGUID ' m_RecID is the private field backing the RecID property and for some reason we have another property SettingValueID which also just gets and sets m_RecID
InsertFlag = True
ChangeFlag = True
End If
NotifyPropertyChanged("Contents")
End Set