Tig7r
Tig7r

Reputation: 565

How can I retreive a DNN ModuleSetting or TabSetting in a DoWork() method for the Scheduler?

I have a DotNetNuke module with a View and Scheduler Class called CRMCheck.

I have setup my Scheduler Task to call my class every couple of minutes. (MyModule.Modules.CRM.CRMCheck)

I need to retrieve a Setting saved for Module or TabSettings. I am saving Module and ModuleTabSettings, by default DNN will use ModuleTabSettings when using Settings[] and saving both at the same time.

So my main issue is I want to fetch the Email Address which I saved in the settings section of the module which the Scheduler DoWork() method should use.

I am also having an issue whereby you can't get the portal ID when using the Scheduler and DNN states you that in this post: https://dnncommunity.org/forums/aft/191

This is the error:

enter image description here

This is the Scheduler Code just beneath my modules View Class:

    public class CRMCheck : SchedulerClient
{
    private readonly DaoCRM m_DaoCRM = new DaoCRM();


public CRMCheck(ScheduleHistoryItem oItem) : base()
    {
        this.ScheduleHistoryItem = oItem;
    }

    public Control LocalResourceFile { get; private set; }

    public override void DoWork()
    {
        try
        {
            //Perform required items for logging
            this.Progressing();

            //Your code goes here
            DateTime? TimeNow = DateTime.Now;
            var CRMItems = ShowCRMListDNN();
            foreach (var item in CRMItems)
            {
                DateTime? RegisteredDateINDatabase = item.DateRegistered;
                var TimeDifference = TimeNow - RegisteredDateINDatabase;
                var DidTheyPurchaseSomething = item.PurchasedSomething;
                if(TimeDifference.Value.TotalHours > 3 && DidTheyPurchaseSomething == false)
                {
                    //email and stuff
                    SendSuccessEmailToAdmin(item.FirstName, item.LastName, item.CompanyName, item.EmailAddress, item.Cellphone, item.ERPUserId, item.DateRegistered, item.PortalID, item.PurchasedSomething);
                }
            }


            //To log note
            this.ScheduleHistoryItem.AddLogNote("Item Executed");

            //Show success
            this.ScheduleHistoryItem.Succeeded = true;
        }
        catch (Exception ex)
        {
            this.ScheduleHistoryItem.Succeeded = false;
            //InsertLogNote("Exception= " + ex.ToString());
            this.Errored(ref ex);
            DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
        }
    }

    public List<CRM> ShowCRMListDNN()
    {
        var result = new List<CRM>();
        try
        {
            return result = m_DaoCRM.ShowCRMListNoPortalID();
        }
        catch (Exception ex)
        {
        EventLogController logController = new EventLogController();
        logController.AddLog("Problem fetching CRM List for Scheduled Job.", ex.ToString(), EventLogController.EventLogType.ADMIN_ALERT);
        return result;
      }
    }

    /// <summary>
    /// Sends an email when the job runs
    /// </summary>
    /// <remarks>ZR - Added - 09/03/2018</remarks>
    private void SendSuccessEmailToAdmin(string FirstName, string LastName, string CompanyName, string Email, string Cellphone, int ERPUserID, DateTime? DateRegistered, int Portal, bool PurchasedSomething)
    {
        if (Settings["CRMEmail"] != null && !string.IsNullOrWhiteSpace(Settings["CRMEmail"].ToString()))
        {
            System.Text.StringBuilder strAdminEmailMsg = new System.Text.StringBuilder();

            strAdminEmailMsg.AppendLine("<br><br>");
            strAdminEmailMsg.AppendLine("<table style=\"border-color:#007799;color:#007799;border-style:double;font-family:arial,verdana;font-size:8pt;\" width=\"70%\" border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"0\">");
            strAdminEmailMsg.AppendLine("    <tr>");
            strAdminEmailMsg.AppendLine("        <td style=\"text-align:center;background:#007799;color:#ffffff;font-weight:bold;font-family:arial;font-size:11pt;\">New Customer Registration</td>");
            strAdminEmailMsg.AppendLine("    </tr>");
            strAdminEmailMsg.AppendLine("    <tr>");
            strAdminEmailMsg.AppendLine("        <td align=\"center\"><table width=\"80%\" style=\"font-family:verdana;font-size:8pt;\">");
            strAdminEmailMsg.AppendFormat("  <tr><td align=\"left\">Date:</td><td align=\"left\">{0}</td></tr>", DateTime.Now.ToLocalTime()).AppendLine();
            strAdminEmailMsg.AppendLine("</table>");

            strAdminEmailMsg.AppendLine("<table style=\"color:#007799;font-family:arial,verdana;font-size:8pt;\" width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"0\">");
            strAdminEmailMsg.AppendLine("    <tr>");
            strAdminEmailMsg.AppendLine("        <td>&nbsp;</td>");
            strAdminEmailMsg.AppendLine("    </tr>");
            strAdminEmailMsg.AppendLine("    <tr>");
            strAdminEmailMsg.AppendLine("        <td style=\"text-align:center\">&nbsp;");
            //all changes in inner table
            strAdminEmailMsg.AppendLine("            <table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#007799\" border=\"1\" style=\"font-family:verdana;font-size:8pt;border-collapse:collapse;\">");
            strAdminEmailMsg.AppendLine("                <tr>");
            strAdminEmailMsg.AppendFormat("                  <td style=\"color:#000000;\" bgcolor=\"#e7e8e9\">FirstName</td>");
            strAdminEmailMsg.AppendFormat("                  <td style=\"color:#000000;\" bgcolor=\"#e7e8e9\">{0}</td>", FirstName);
            strAdminEmailMsg.AppendLine("                </tr>");
            strAdminEmailMsg.AppendLine("                <tr>");
            strAdminEmailMsg.AppendFormat("                  <td style=\"color:#000000;\" bgcolor=\"#e7e8e9\">Last Name</td>");
            strAdminEmailMsg.AppendFormat("                  <td style=\"color:#000000;\" bgcolor=\"#e7e8e9\">{0}</td>", LastName);
            strAdminEmailMsg.AppendLine("                </tr>");
            strAdminEmailMsg.AppendLine("                <tr>");
            strAdminEmailMsg.AppendFormat("                  <td style=\"color:#000000;\" bgcolor=\"#e7e8e9\">Company Name</td>");
            strAdminEmailMsg.AppendFormat("                  <td style=\"color:#000000;\" bgcolor=\"#e7e8e9\">{0}</td>", CompanyName);
            strAdminEmailMsg.AppendLine("                </tr>");
            strAdminEmailMsg.AppendLine("                <tr>");
            strAdminEmailMsg.AppendFormat("                  <td style=\"color:#000000;\" bgcolor=\"#e7e8e9\">Email</td>");
            strAdminEmailMsg.AppendFormat("                  <td style=\"color:#000000;\" bgcolor=\"#e7e8e9\">{0}</td>", Email);
            strAdminEmailMsg.AppendLine("                </tr>");
            strAdminEmailMsg.AppendLine("            </table>");
            strAdminEmailMsg.AppendLine("        </td>");
            strAdminEmailMsg.AppendLine("    </tr>");
            strAdminEmailMsg.AppendLine("</table>");

            SendEmail("System Job", strAdminEmailMsg.ToString(), Settings["CRMEmail"].ToString());
        }
    }

    /// <summary>
    /// Send an email
    /// </summary>
    /// <param name="MailSubject"></param>
    /// <param name="MailMessage"></param>
    /// <param name="RecipientEmail"></param>
    private void SendEmail(string MailSubject, string MailMessage, string RecipientEmail)
    {
        bool EmailSent = false;

        //EmailSent = webcall
        EmailSent = SessionManager.GSettings.Globalvars.ClHelper.eBusiness.SendEmail(-1, 2, RecipientEmail, MailSubject, MailMessage);

        //Show a message to the user if it was sent or not
        if (!EmailSent)
        {
            EventLogController logController = new EventLogController();
            logController.AddLog("Problem sending Job Email", "", EventLogController.EventLogType.ADMIN_ALERT);
        }

    }

}

Upvotes: 1

Views: 142

Answers (1)

Tig7r
Tig7r

Reputation: 565

I found a workaround by creating a method that goes to a Dao class whereby I can call a Database query to the ModuleSettings table.

Upvotes: 1

Related Questions