Page 1 of 1

Update a plugin System Setting programatically

PostPosted: Tue Jan 12, 2021 8:48 am
by SBarnes
If

Code: Select all
manager.Database.ReadSysData(PluginName, "IncludeBackOrders", "0").ToString()


will read a plugins system setting how do I update it through code?

Re: Update a plugin System Setting programatically  Topic is solved

PostPosted: Tue Jan 12, 2021 8:58 am
by Scott.Pearce
Code: Select all
manager.Database.SaveSysData(PluginName, "IncludeBackOrders", "0");

Re: Update a plugin System Setting programatically

PostPosted: Tue Jan 12, 2021 9:01 am
by SBarnes
Thanks

Re: Update a plugin System Setting programatically

PostPosted: Wed Feb 24, 2021 12:24 pm
by SBarnes
What about for a datetime do you need to format up a string and if so what format?

Re: Update a plugin System Setting programatically

PostPosted: Wed Feb 24, 2021 3:12 pm
by Mike.Sheen
SBarnes wrote:What about for a datetime do you need to format up a string and if so what format?


ISO-8601 format - we actually don't do this ourselves (logged as DEV-8616) but you should store as ISO 8601 format (in .NET use the "s" format specifier). We happily read ISO 8601 format dates and will display them in the format of the system culture, but sadly we write them back currently in the short date format of the system culture.

So, use .ToString("s") - that'll produce a string like "2021-02-24T00:00:00".

Re: Update a plugin System Setting programatically

PostPosted: Wed Feb 24, 2021 5:26 pm
by SBarnes
How can I convert the string from manager.Database.ReadSysData(PluginName, "ISOUpdate", "0").ToString(); to a datetime the following with a format of MM/dd/yyyy hh:mm:ss tt isn't working which is pretty much the format it is?

Code: Select all
        public static DateTime ToDateTime(this string value, string format = "")
        {
            DateTime outdate = DateTime.Now;
            if (format == "")
            {
                format = SettingsVault.DateTimeFormat;
            }

            if (DateTime.TryParseExact(value, format,
                CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces,
                out outdate))
            {
                return outdate;
            }
            else
            {
               return DateTime.MinValue;
            }
        }

Re: Update a plugin System Setting programatically

PostPosted: Wed Feb 24, 2021 5:49 pm
by Mike.Sheen
ParseExact (which should behave the same in terms of formatting interpretation) worked for me when I provided a format matching what you describe ("MM/dd/yyyy hh:mm:ss tt")

ParseExact.png
ParseExact.png (6.16 KiB) Viewed 12533 times


If you can't get that to work, then I'd be wanting the exact format you're using and the exact string value you are attempting to convert.

Re: Update a plugin System Setting programatically

PostPosted: Wed Feb 24, 2021 5:52 pm
by SBarnes
I found a simpler solution

Code: Select all
DateTime dtTest = (DateTime)manager.Database.ReadSysData(PluginName, "ISOUpdate", "0");

Re: Update a plugin System Setting programatically

PostPosted: Wed Feb 24, 2021 6:01 pm
by Mike.Sheen
SBarnes wrote:I found a simpler solution

Code: Select all
DateTime dtTest = (DateTime)manager.Database.ReadSysData(PluginName, "ISOUpdate", "0");


There might be some problems if you have a machines with different formats reading the same data - so a machine using US date format MM/dd/yyyy will interpret the string "01/07/2021" differently to machine using AU date formats. Which is why I recommend storing in ISO 8601 yyyy-MM-dd format. Doesn't help that we have a shortcoming in our own System settings form which uses the machine culture format - but we're gonna fix that!

Right now, if you do have a mix of clients with different culture / regional formats then you're going to have a bad time.

We sorted this all out with dates for custom fields - we just didn't think to apply the same to system settings of type date.

Re: Update a plugin System Setting programatically

PostPosted: Wed Feb 24, 2021 6:09 pm
by SBarnes
Just an idea but maybe something similar to the functions on the generic object object collection like SetOrUpdate would be helpful on the custom field??

As for the cast it should be ok as this is on a scheduled plugin where the customers server is also a remote server i.e. all clients and services on the same server.

But given I also need to do a similar thing for another client I may just revert to using text custom fields that way I can control the going in and coming out.