Tasks and Calendar Entries: Removing Duplicate User Assignments
Sometimes the same user is included in a calendar event, or assigned to a task, multiple times. This can be a result of User Mapping or data error in the legacy system.
Below is an example of how this can happen.
A calendar event includes user John Smith and Henry Jones. Henry Jones is an inactive user in the legacy system who will not have a user account in the destination system. The firm has decided to map Henry's user data to John Smith, an active user in the destination system. As a result, the calendar entry includes John Smith twice. Once because he was originally included in the meeting, and once because Henry Jone's user data was mapped to him.
A similar situation can happen with tasks being assigned to the same user multiple times.
The script below will remove these duplicate user assignments.
- GO
- WITH P AS
- (
- SELECT
- cpu.Id,
- ROW_NUMBER() OVER (PARTITION BY cpu.Final_CalendarEntry_Id, u.Result_Id ORDER BY (SELECT NULL)) rowNum
- FROM
- __M_CalendarEntries_Participating_Users cpu
- INNER JOIN
- __M_Users u
- ON u.Id = cpu.Final_User_Id
- ),
- P2 AS
- (
- SELECT
- Id
- FROM
- P
- WHERE 1=1
- AND rowNum > 1
- )
- UPDATE
- __M_CalendarEntries_Participating_Users
- SET
- Final_Status= 'Deleted',
- Comment = 'Duplicate assignment removed'
- WHERE 1=1
- AND Id IN
- (
- SELECT Id FROM P2
- );
- GO
- WITH P AS
- (
- SELECT
- tau.Id,
- ROW_NUMBER() OVER (PARTITION BY tau.Final_TaskEntry_Id, u.Result_Id ORDER BY (SELECT NULL)) rowNum
- FROM
- __M_Tasks_AssignedTo_Users tau
- INNER JOIN
- __M_Users u
- ON u.Id = tau.Final_User_Id
- ),
- P2 AS
- (
- SELECT
- P.Id
- FROM
- P
- WHERE 1=1
- AND rowNum > 1
- )
- UPDATE
- __M_Tasks_AssignedTo_Users
- SET
- Final_Status = 'Deleted',
- Comment = 'Duplicate assignment removed'
- WHERE 1=1
- AND Id IN
- (
- SELECT
- Id
- FROM
- P2
- );