Hi Dojo
When trying to update a string type DOM instance field using the Skyline.DataMiner.Net.Sections.SectionUtils.AddOrUpdateFieldValue<object>(..., object value) method call, the following exception is thrown. I understand that string type fields cannot be updated using AddOrUpdateFieldValue<object>(..., object value). You should use AddOrUpdateFieldValue<string>(..., string value) to make this work.
But since the actual method signature defines Skyline.DataMiner.Net.Sections.SectionUtils.AddOrUpdateFieldValue<T>(..., T value), I was wondering if it should be possible to use AddOrUpdateFieldValue<object>(..., object value)? There is no object type field available: SectionDefinition object | DataMiner Docs
Skyline.DataMiner.Net.ManagerStore.CrudFailedException: Exception of type 'Skyline.DataMiner.Net.ManagerStore.CrudFailedException' was thrown.
at Skyline.DataMiner.Net.ManagerStore.CrudHelperComponent`1.CheckTraceData(TraceData traceData)
at Skyline.DataMiner.Net.ManagerStore.CrudHelperComponent`1.InnerUpdate(T obj, IAdditionalOperationMeta operationMeta)
at Skyline.DataMiner.Net.ManagerStore.CrudHelperComponent`1.Update(T obj)
at Skyline.DataMiner.Automation.SESSA.CreateBookingFromDom.Run(Engine engine, ExecuteScriptDomActionContext context)
Containing TraceData:
TraceData: (amount = 1)
- ErrorData: (amount = 1)
- ErrorReason: DomInstanceSectionInvalidFieldValueTypes, DomInstance: ...
Thank you in advance!
Kind regards
Hi Lander. In my case two fields need to be updated, a string type field and a DateTime type field. I was trying to use the same AddOrUpdateFieldValue method call for this.
The error could be a bit more clear. That being said, I'm not a fan of allowing object for a string field. Assigning an object to a string field could indicate the developer is doing something unintended. It would be dangerous to guess how to convert the object to the string:
- Is ToString() correctly implemented?
- How to write a null? Empty string, "NULL", or something else?
For that reason it's safer to let the developer convert it to the correct string before passing it to the AddOrUpdateFieldValue method.
Note: you don't need to specify the generic type, this should be automatically detected by the compiler. So you can write:
section.AddOrUpdateFieldValue(myDatetimeField, DateTime.UTCNow);
section.AddOrUpdateFieldValue(myStringField, "My string");
section.AddOrUpdateFieldValue(myStringField, myObject?.ToString() ?? "NULL");
Hi Lander. Thank you very much for your answer! You are right. It works perfectly when you define an AddOrUpdateFieldValue method call for each required type. For instance, in my case two fields need to be updated. A string type field and a DateTime type field. So I’ve defined one AddOrUpdateFieldValue(…, dateTimeValue) method call and one AddOrUpdateFieldValue(…, stringValue). But don’t you agree that this approach can be a possible pitfall? As we’re always trying to reuse code as much as possible, the compiler won’t complain if I would define one AddOrUpdateFieldValue(…, objectValue) method call instead.
Yes I agree it’s a pity that you won’t see the error at compile time. But allowing objects for string fields only resolves this issue partially. What about setting an object to a DateTime field? Unless the object already is a DateTime, there’s no way to convert it to one. Allowing it for string fields, but not for other fields adds an inconsistency that may add extra confusion.
Okay that’s clear. Thanks for your explanation!
Hi Michiel. Can you explain why you want to put an object into a string DOM field? If it’s already a string, you can cast it to a string. If not, you can call the ToString() method or any other method or property that returns the string you want to set in the DOM field.