Scheduled Task: Custom Table record only updates after Rebuilding the solution

Irene Gunning asked on November 14, 2022 12:17

Hi,

I'm still learning to implement custom Scheduled Tasks. I created a class to add and update a record in a custom table and added it as a scheduled task but the table only updates after I Rebuild the solution. The event log shows that the table was updated but the field value never change. It gets the value of the field data from the last Rebuild.

I followed this: https://docs.xperience.io/configuring-xperience/scheduling-tasks/scheduling-custom-tasks

Here is my scheduled task code:

    public string Execute(TaskInfo ti)
    {
        //custom class to add and update a record
        var depotData = new DepotData();

       depotData.AddDepot("3126", "Logistix", "245 Road Cleveland", "Johannesburg", "South Africa", "1449");

        string details = "Custom scheduled task executed. Task data: " + ti.TaskData;

        // Logs the execution of the task in the event log
        Service.Resolve<IEventLogService>().LogInformation("CustomTask", "Execute", details);

        // Returns a null value to indicate that the task executed successfully
        // Return an error message string with details in cases where the execution fails
        return null;
    }

What could be wrong with the code?

Recent Answers


Juraj Ondrus answered on November 14, 2022 14:00

What is the code of the DepotData method? What API are you using to update the custom table? When you rebuild the solution = app restart. The app loads the fresh data from the DB after restart. So, the issue is in the code you are using for updating and not in the scheduled task itself. I just tested it with this code and it is working fine.

0 votesVote for this answer Mark as a Correct answer

Irene Gunning answered on November 14, 2022 14:35 (last edited on November 14, 2022 17:24)

This is the code for the DepotData class:

using CMS.CustomTables;
using CMS.DataEngine;
using CMS.Helpers;

namespace Custom
{
    public class DepotData
    {
        string customTableClassName = "MySite.CT_Depot";


        public void AddDepot(string depotCode, string depotName, string addressLine, string city, string country, string postcode)
        {
            DataClassInfo customTable = DataClassInfoProvider.GetDataClassInfo(customTableClassName);

            if (customTable != null)
            {
                // Creates a new custom table item
                CustomTableItem newCustomTableItem = CustomTableItem.New(customTableClassName);

                var customTableData = CustomTableItemProvider.GetItems(customTableClassName).WhereEquals("DepotCode", depotCode);

                if (customTableData.Count == 0)
                {
                    // Sets the values for the fields of the custom table
                    newCustomTableItem.SetValue("DepotCode", depotCode);
                    newCustomTableItem.SetValue("DepotName", depotName);
                    newCustomTableItem.SetValue("AddressLine", addressLine);
                    newCustomTableItem.SetValue("City", city);
                    newCustomTableItem.SetValue("Country", country);
                    newCustomTableItem.SetValue("DepotPostcode", postcode);

                    // Save the new custom table record into the database
                    newCustomTableItem.Insert();
                }

                else
                {
                    foreach (CustomTableItem item in customTableData)
                    {
                        item.SetValue("DepotName", depotName);
                        item.SetValue("AddressLine", addressLine);
                        item.SetValue("City", city);
                        item.SetValue("Country", country);
                        item.SetValue("DepotPostcode", postcode);

                        // Saves the changes to the database
                        item.Update();
                    }

                }
            }
        }
    }
}

I'm testing it for now. The plan is to update the objects if there are changes every 5 mins and the data will be fetched from the SAP server. The data would be in CSV format.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on November 14, 2022 15:04 (last edited on November 14, 2022 15:04)

Have you considered using Integration bus? Also keep in mind the performance point of view - I do not know how many items are in the custom table, but it could become a nightmare. I would recommend to discuss this goal with somebody experienced or even schedule an ad-hoc session with our solution architect to implement this right and avoid headache in the future.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on November 14, 2022 20:20

Have you checked that the scheduled task is enabled? How about the scheduled execution of the task? What are the parameters for how often the task is to be executed?

There could be an error in your sub-method AddDepot() that causes the task to fail and get stuck so it doesn't execute again.

0 votesVote for this answer Mark as a Correct answer

Irene Gunning answered on November 15, 2022 01:38 (last edited on November 15, 2022 03:16)

The task gets executed successfully and there was no error in the logs.

Event logs screenshot

When I delete the record, it gets added again but the data added is from the previous Rebuild.

Screenshot

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.