+ Start a Discussion
chris_centrachris_centra 

recurring tasks - 1st monday of month...

Hello.

 

I have to build a number of recurring tasks via Apex.  Most are straightforward.  However some are n-th *day of the month - like "2nd Tuesday of the Month".  These can be created in the interface, but i don't know how to create them in Apex.  My guess is:

1) RecurrenceType = Monthly

2) RecurrenceDayOfWeekMask = (approrpiate day-of-week value)

3) RecurrenceInterval = n (as in n-th Tuesday)

 

However this fails with the following runtime message:

INVALID_FIELD_FOR_INSERT_UPDATE, Day of week must be blank for type Recurs Monthly.: [RecurrenceDayOfWeekMask]

 

How can i say what day of the week it is if this field must be blank?

 

thanks

Chris

Best Answer chosen by Admin (Salesforce Developers) 
jhurstjhurst

I forgot to add the ReccurenceType.  It is not "Monthly", but is "RecursMonthlyNth".  The below works for me in Apex:

 

Task t = new Task(Subject = 'test', isRecurrence = true, RecurrenceDayOfWeekMask = 4, RecurrenceType = 'RecursMonthlyNth',
                  RecurrenceEndDateOnly = date.newinstance(2012, 04, 01), RecurrenceInstance = 'First',
                  RecurrenceInterval = 1, RecurrenceStartDateOnly = date.newinstance(2012, 01, 01));
insert t;

 Hope this helps.

Jay

All Answers

jhurstjhurst

Chris,

 

You have to set a few different fields:

 

IsRecurrance = true

RecurrenceDayOfWeekMask = 4 (4 is Tuesday)

RecurrenceEndDateOnly - The date you want the task to end by

RecurrenceInstance = First (First indicates the First Tuesday)

RecurrenceInterval = 1 (1 indicates every month)

RecurrenceStartDateOnly - The date you want to start the task on

 

You can find more info on the appropriate fields in the documentation.  Also, an easy way to see what you need is to create a recurrence in the UI, and then query for the record to see what was created.

 

Hope this helps.

 

Jay

chris_centrachris_centra

Thanks for your reply.  I wasn't clear enough in my first message.  When i set the values exactly as you say, i get the following runtime error (below).  It will not let me set RecurrenceDayOfWeekMask if it's a Monthly recurrence.

 

Thanks

Chris

 

Class.Case_RecurringTaskClass.test_SaveOneMonthlyAbsoluteDateCase: line 541, column 1
18:36:16.187 (1187797000)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Case_RecurringTask: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Day of week must be blank for type Recurs Monthly.: [RecurrenceDayOfWeekMask]

jhurstjhurst

I forgot to add the ReccurenceType.  It is not "Monthly", but is "RecursMonthlyNth".  The below works for me in Apex:

 

Task t = new Task(Subject = 'test', isRecurrence = true, RecurrenceDayOfWeekMask = 4, RecurrenceType = 'RecursMonthlyNth',
                  RecurrenceEndDateOnly = date.newinstance(2012, 04, 01), RecurrenceInstance = 'First',
                  RecurrenceInterval = 1, RecurrenceStartDateOnly = date.newinstance(2012, 01, 01));
insert t;

 Hope this helps.

Jay

This was selected as the best answer
FinneyFinney

Hi Jhurst

 

I have a similar issue to this one. Can you please help me with this.

 

I want a trigger on the inventory object which sends a weekly , bi- weekly and monthly tasks based on a picklist field.

 

Frequency Seller Should be Contacted is a picklist field with 3 values

 

1) Weekly - If it is weekly a task should go to account owner every week until we change the stage

2) Bi- weekly - Task should be sent every 2 weeks until we change the stage

3)Monthly - Task should be sent every month till we change the stage

 

The terms are

Inventory terms

Is For Sale = True

Live STR “CONTAINS” 298

Stage = Available

 

The trigger should fire a task when the above terms are met.

 

 

Thanks and Kind Regards

 

Finney

jhurstjhurst

Finney,


This woul dbe a little more involved.  There are some things to consider:

 

1. When do you want the first recurrence?  If the field is changed on Wednesday, do you want the weekly reminders on Wednesday, or do you always want them on The first day of the week.  The same question applies to bi-weekly and monthly (what doy of the month do you want the reminder?)

 

2. What do you want done wit the old recurrence (if tehre is one)?  Do you want it deleted or updated?

 

3. What happens to the task if teh terms change such that the requieements no longer match?

 

I have not tested it, but a trigger would looks somethign like: 

 

Trigger createRecurringTask on Inventory_Terms__c (after update) {
    List<Task> tList = new List<Task>();
    for (integer i = 0; i < Trigger.new.size(); i++) {
        //Check if the task should be re-evaluated
        if (Trigger.new[i].Frequency__c != Trigger.old[i].Frequency__c) {
            if (Trigger.new[i].Frequency__c == 'Weekly) {
                Task t = new Task(Subject = 'test', isRecurrence = true, RecurrenceDayOfWeekMask = 2, RecurrenceType = 'RecursWeekly',
                                  RecurrenceDayOfWeekMask=2, RecurrenceInterval=1, RecurrenceStartDateOnly = Today(),
                                  RecurrenceEndDateOnly = Today() + 365);
                tList.add(t);
            }
            if (Trigger.new[i].Frequency__c == 'Bi-Weekly) {
                Task t = new Task(Subject = 'test', isRecurrence = true, RecurrenceDayOfWeekMask = 2, RecurrenceType = 'RecursWeekly',
                                  RecurrenceDayOfWeekMask=2, RecurrenceInterval=2, RecurrenceStartDateOnly = Today(),
                                  RecurrenceEndDateOnly = Today() + 365);
                tList.add(t);
            }
            if (Trigger.new[i].Frequency__c == 'Mothly) {
                Task t = new Task(Subject = 'test', isRecurrence = true, RecurrenceDayOfWeekMask = 2, RecurrenceType = 'RecursMonthly',
                                  RecurrenceDayOfMonth=1, RecurrenceInterval=1, RecurrenceStartDateOnly = Today(),
                                  RecurrenceEndDateOnly = Today() + 365);
                tList.add(t);
            }
        }  
    }
    insert t;
}

 

The above will create a recurring series for teh next year if teh picklist is changed.  The weekly and Bi-weekly fire on Monday, the Monthly fires on the first day of the month.

 

There is more information in the documentation also - 

http://www.salesforce.com/us/developer/docs/api/index_Left.htm#StartTopic=Content/sforce_api_objects_task.htm?SearchType=Stem

 

Hope this helps.

Jay

 

FinneyFinney

Dear Jay

 

Thanks a lot for your suggestions.

 

Will try this and come back to you if it works.

 

Appreciate your help mate.

 

Thanks

 

Finney