ContactUs form create a class

Theodoulos Iacovou asked on March 3, 2020 12:36

Can I add the bellow code in a custom task? // 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)
                                                            .WhereEmpty("UserMessage");

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

reference: https://docs.kentico.com/api12sp/content-management/form-data

Recent Answers


vasu yerramsetti answered on March 3, 2020 14:03

I am not sure your requirement but as per above code snippet my understanding is , You need to implement one custom schedule task to delete records from Form that have empty value in "usermessage" field.

Yes, you can add this code snippet for delete records from form based on your logic.

Refer Scheduling custom tasks in Kentico 12 documentation.

0 votesVote for this answer Mark as a Correct answer

Theodoulos Iacovou answered on March 3, 2020 14:46 (last edited on March 3, 2020 14:46)

It will be like this in the class?

using CMS.Scheduler;
using CMS.EventLog;

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
            EventLogProvider.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;

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)
                                                            .WhereEmpty("UserMessage");

    // 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();
    }
}
        }
    }
}
0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on March 3, 2020 14:55

To answer your question "Can I add the bellow code in a custom task?", yes you can add that as a custom task.

The question back to you is what do you want your custom task to do? You provide no information regarding what you want it to do.

0 votesVote for this answer Mark as a Correct answer

Theodoulos Iacovou answered on March 3, 2020 15:11

I want a custom task to add it on Scheduling.

0 votesVote for this answer Mark as a Correct answer

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