Salesforce – Re-assigning Event Re-occurrence Instance

by | Mar 7, 2014 | Apex, Force.com, Salesforce | 4 comments

Utilising the Salesforce Calendar for events has many benefits compared to using Outlook.

One of my clients came across a limitation recently when they create a weekly re-occurring event for their team member. The problem is that when the staff member cannot attend the appointment they then need to re-assign it to someone else.

With the current version of Salesforce you cannot do this, you can only reassign the complete series to someone else.

The good thing is that with a bit of research I found that each occurrence is a record. So my first attempt was to find the event and then using a Visual Force page I asked who they want to reassign it to.

I wrote some Apex code to update the record to the newly assigned user, but the Salesforce business rules do not allow this.

So the solution was to clone the event into a new object, delete the original and insert the new. This worked beautifully.

public class ReassignEventController
{

    public String assignTo { get; set; }

    public List getUsers() {
        List options = new List();

        for(User u :[SELECT Id, Name FROM User]) {
            options.add(new SelectOption(u.id, u.Name));
        }
        return options;
    }

    public Event getEvent() {
        return [SELECT Id, Description, Owner.Id, Owner.Name FROM Event
                WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public PageReference reassign() {
        System.debug('Save clicked: ' + assignTo);

        Event ev = [SELECT Id, WhoId, WhatId, Subject, 
                    Location, IsAllDayEvent, ActivityDateTime, 
                    ActivityDate, DurationInMinutes, StartDateTime, 
                    EndDateTime, Description, AccountId, OwnerId, Type, 
                    IsReminderSet FROM Event
                WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
        ev.OwnerId = assignTo;

		Event ev2 = ev.clone(false,true);
        ev2.OwnerId = assignTo;

        Insert ev2;

        Delete ev;

        PageReference redirecturl = new PageReference('/' + ev2.Id);

        return redirecturl;
    }
}

Once the event has been created then it redirects to the newly created one details page.

The Visual Force page is very simple and I will probably include this in the Event details page.


I hope this helps you if you come across the same situation too. Leave a comment if you need help.

Spread the love