Tasks and Calendar Entries: Removing Duplicate User Assignments

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.

  1.   GO
  2. WITH P AS
  3. (
  4. SELECT
  5. cpu.Id,
  6. ROW_NUMBER() OVER (PARTITION BY cpu.Final_CalendarEntry_Id, u.Result_Id ORDER BY (SELECT NULL)) rowNum
  7. FROM
  8. __M_CalendarEntries_Participating_Users cpu
  9. INNER JOIN
  10. __M_Users u
  11. ON u.Id = cpu.Final_User_Id
  12. ),
  13. P2 AS
  14. (
  15. SELECT 
  16. Id
  17. FROM
  18. P
  19. WHERE 1=1
  20. AND rowNum > 1
  21. )
  22. UPDATE
  23. __M_CalendarEntries_Participating_Users
  24. SET
  25. Final_Status= 'Deleted',
  26.         Comment = 'Duplicate assignment removed'
  27. WHERE 1=1
  28. AND Id IN 
  29. (
  30. SELECT Id FROM P2
  31. );



  32. GO
  33. WITH P AS 
  34. (
  35. SELECT 
  36. tau.Id,
  37. ROW_NUMBER() OVER (PARTITION BY tau.Final_TaskEntry_Id, u.Result_Id ORDER BY (SELECT NULL)) rowNum
  38. FROM 
  39. __M_Tasks_AssignedTo_Users tau
  40. INNER JOIN
  41. __M_Users u
  42. ON u.Id = tau.Final_User_Id
  43. ),
  44. P2 AS
  45. (
  46. SELECT
  47. P.Id
  48. FROM
  49. P
  50. WHERE 1=1
  51. AND rowNum  > 1
  52. )
  53. UPDATE
  54. __M_Tasks_AssignedTo_Users
  55. SET
  56. Final_Status = 'Deleted',
  57.         Comment = 'Duplicate assignment removed'
  58. WHERE 1=1
  59. AND Id IN 
  60. (
  61. SELECT
  62. Id
  63. FROM
  64. P2
  65. );