Custom Timer Job is not working in production

815 views Asked by At

I have a document library called Documents in production. I wrote a timer job for sending emails based on the columns in the library.

Everything was fine in the dev environment but the timer job in prod is giving me exceptions. Any ideas?

        Entering monitored scope (Timer Job Corporate Policies - Reminder Emails)

The Execute method of job definition CorporatePolicyReminders.DailySchedule (ID 3586f4d2-1bc1-4770-b5b8-4c2c578ee8d5) threw an exception. More information is included below. Object reference not set to an instance of an object.

Exception stack trace:
at CorporatePolicyReminders.CorporatePolicyReminders.SendCorporateReminder(SPWeb web)
at CorporatePolicyReminders.DailySchedule.Execute(Guid contentDbId)
at Microsoft.SharePoint.Administration.SPTimerJobInvokeInternal.Invoke(SPJobDefinition jd, Guid targetInstanceId, Boolean isTimerService, Int32& result)

Leaving Monitored Scope (Timer Job Corporate Policies - Reminder Emails). Execution Time=364.288198639771

SendCorporateReminers Method

 public void SendCorporateReminder(SPWeb web)
{
    SPList lstComplaint = Helper.GetList(web, "Documents", Helper.ListType.Library);

    if (lstComplaint != null)
    {
        string query = "<Where><Eq><FieldRef Name='DocFormat' /><Value Type='Choice'>Policy</Value></Eq></Where>";
        SPListItemCollection colRecords = Helper.ExecuteQueryRecursive(lstComplaint, query, null, 2000);

        if (colRecords != null && colRecords.Count > 0)
        {
            foreach (SPListItem item in colRecords)
            {

                DateTime Expires = Helper.GetSPFieldDateTimeValue(item, "Expires");
                String Title = Helper.GetSPFieldTextValue(item, "Title");
                SPFieldUserValueCollection userResp = Helper.GetUsers(item, "Contact");
                TimeSpan totaldays = DateTime.Now.Date - Expires.Date;
                Int32 days = totaldays.Days;
                String emailtemplatekeyA = "AEmail" + days.ToString();
                String emailtemplatekeyB = "BEmail" + days.ToString();
                String respToEmail = String.Empty;


                if (userResp != null && userResp.Count > 0)
                {
                    foreach (SPFieldUserValue user in userResp)
                    {
                        if (!string.IsNullOrEmpty(respToEmail))
                            respToEmail += ",";
                        respToEmail += user.User.Email;
                    }
                }
                HandleEmails handleEmail = new HandleEmails();
                handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.EmailRecipient, PlaceHolder.USERRESP.ToString(), respToEmail);
                handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.OTHER, PlaceHolder.TITLE.ToString(), Title);
                handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.OTHER, PlaceHolder.EXPDT.ToString(), Expires.ToShortDateString());
                handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.OTHER, PlaceHolder.ID.ToString(), item.ID.ToString());
                handleEmail.SendMail(web, emailtemplatekeyA);
                handleEmail = new HandleEmails();
                handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.EmailRecipient, PlaceHolder.USERRESP.ToString(), respToEmail);
                handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.OTHER, PlaceHolder.TITLE.ToString(), Title);
                handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.OTHER, PlaceHolder.EXPDT.ToString(), Expires.ToShortDateString());
                handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.OTHER, PlaceHolder.ID.ToString(), item.ID.ToString());
                handleEmail.SendMail(web, emailtemplatekeyB);
            }
        }
    }
}`
1

There are 1 answers

0
user28455 On

I found the issue and resolved it.Person whose userid is no longer valid is Contact the person of document (he is in contact column for the library i am creating timer job). So timer job is throwing exception (Object reference not set to an instance of an object.) whenever it is trying to read Contact column of the document. i figured it out by modifying my code as below and i removed that person from that column.

    Helper.WriteUlsInformation("Item processing:" + i.ToString(), TraceSeverity.Verbose);
                    DateTime Expires = Helper.GetSPFieldDateTimeValue(item, "Expires");
                    Helper.WriteUlsInformation("Expires:" + Expires.ToString(), TraceSeverity.Verbose);
                    String Title = Helper.GetSPFieldTextValue(item, "Title");
                    Helper.WriteUlsInformation("Title:" + Title, TraceSeverity.Verbose);
                    SPFieldUserValueCollection userResp = Helper.GetUsers(item, "Contact");
                    Helper.WriteUlsInformation("UserResp read" , TraceSeverity.Verbose);
                    TimeSpan totaldays = DateTime.Now.Date - Expires.Date;
                    Int32 days = totaldays.Days;
                    Helper.WriteUlsInformation("Days:"+ days.ToString(), TraceSeverity.Verbose);
                    String emailtemplatekeyA = "AEmail" + days.ToString();
                    String emailtemplatekeyB = "BEmail" + days.ToString();
                    String respToEmail = String.Empty;