Event Log

Theodoulos Iacovou asked on March 11, 2020 09:57

The following code delete yearly the ContactUs form records.

I want on the Event log to show the number of the records where deleted.

using CMS.Scheduler;
using CMS.EventLog;
using CMS;
using CMS.OnlineForms;
using CMS.SiteProvider;
using CMS.DataEngine;



[assembly: RegisterCustomClass("Custom.CustomTask", typeof(Custom.CustomTask))]




namespace Custom
{
    public class CustomTask : ITask
    {
        /// <summary>
        /// Executes the task.
        /// </summary>
        //// <param name="ti">Info object representing the scheduled task</param>
        public string Execute(TaskInfo ti)
        {
            string details = "Custom scheduled task executed. Task data: " + ti.TaskData;

            // Logs the execution of the task in the event log
            // Gets the form object representing the 'ContactUs' form on the current site
            BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo("ContactUs", SiteContext.CurrentSiteID);


            if (formObject != null)
            {
                // Gets the class name of the 'ContactUs' form
                DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
                string formClassName = formClass.ClassName;

                // Loads all data records from the form that have an empty value in the 'UserMessage' field
                ObjectQuery<BizFormItem> data = BizFormItemProvider.GetItems(formClassName).Where("FormInserted < DATEADD(day, -1, GETDATE())");




                // Loops through the form's data records
                foreach (BizFormItem item in data)
                {
                    // Deletes all files stored in the form's fields
                    BizFormInfoProvider.DeleteBizFormRecordFiles(formClass.ClassFormDefinition, item, SiteContext.CurrentSiteName);

                    // Deletes the form record from the database
                    item.Delete();

                }


            }
            // 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;
        }
    }
}

Correct Answer

Arjan van Hugten answered on March 11, 2020 10:17

I guess you are looking for something like this?

EventLogProvider.LogEvent(EventType.INFORMATION, "ContactUs Form cleanup", "CONTACTUSFORMCLEANUP", eventDescription: $"Removing {data.Count} form rows from the contact us form.");
1 votesVote for this answer Unmark Correct answer

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