SELECT {Activity}.[Id] AS IActivityId, ISNULL({Activity_Definition_Lang}.[Label], {Activity_Definition}.[Label]) AS Label, {Activity}.[Inbox_Detail] AS Detail, {Activity}.[Created] AS StartDate, {Activity}.[Due_Date] AS DueDate, CASE WHEN {Activity}.[Due_Date] IS NULL or {Activity}.[Due_Date]=@NullDate THEN 0 ELSE DATEDIFF(day, {Activity}.[Due_Date], getdate()) END AS DaysOverdue, ISNULL({Activity_Definition_Lang}.[Inbox_Instructions], {Activity_Definition}.[Inbox_Instructions]) AS InboxInstructions, {Activity_Definition}.[Skippable] AS Skippable, {Process}.[Id] AS ProcessId, ISNULL({Process_Definition_Lang}.[Label], {Process_Definition}.[Label]) AS ProcessLabel, {User}.[Id] UserMasterId, {User}.[Name] UserName, {Group}.[Id] GroupId, {Group}.[Name] GroupName, {ACTIVITY_INBOX}.* FROM {Activity} INNER JOIN {Activity_Status} ON {Activity}.[Status_Id] = {Activity_Status}.[Id] AND {Activity_Status}.[Appears_In_Inbox] = 1 INNER JOIN {Activity_Definition} ON {Activity}.[Activity_Def_Id] = {Activity_Definition}.[Id] AND {Activity_Definition}.[Is_Active] = 1 INNER JOIN {Process} ON {Activity}.[Process_Id] = {Process}.[Id] AND {Process}.[Status_Id] = 1 INNER JOIN {Process_Status} ON {Process}.[Status_Id] = {Process_Status}.[Id] AND {Process_Status}.[Is_Terminal] = 0 INNER JOIN {Process_Definition} ON {Activity_Definition}.[Process_Def_Id] = {Process_Definition}.[Id] AND {Process_Definition}.[Is_Active] = 1 AND {Process_Definition}.[Is_Locked] = 0 INNER JOIN {Tenant} ON {Activity}.[Tenant_Id] = {Tenant}.[Id] AND {Tenant}.[Is_Active] = 1 INNER JOIN {Espace} ON {Process_Definition}.[Espace_Id] = {Espace}.[Id] AND {Espace}.[Is_Active] = 1 AND {Espace}.[Is_Locked] = 0 LEFT JOIN {Activity_Definition_Lang} ON {Activity_Definition}.[Id] = {Activity_Definition_Lang}.[Activity_Def_Id] AND {Activity_Definition_Lang}.[Locale] = @Locale LEFT JOIN {Process_Definition_Lang} ON {Process_Definition}.[Id] = {Process_Definition_Lang}.[Process_Def_Id] AND {Process_Definition_Lang}.[Locale] = @Locale LEFT JOIN {User} ON {User}.[Id] = {Activity}.[User_Id] LEFT JOIN {Group} ON {Group}.[Id] = {Activity}.[Group_Id] LEFT JOIN {ACTIVITY_INBOX} ON {ACTIVITY_INBOX}.[ACTIVITYId] = {Activity}.[Id] WHERE ( {Activity}.[User_Id] = @UserId OR ( {Activity}.[User_Id] IS NULL AND ({Activity}.[Group_Id] IS NULL OR EXISTS ( SELECT 1 FROM {Group_User} WHERE {Group_User}.[User_Id] = @UserId AND {Group_User}.[Group_Id] = {Activity}.[Group_Id] ) ) AND ({Activity_Definition}.[Requires_Permission] = 0 OR EXISTS ( SELECT 1 FROM {Activity_Def_Role} INNER JOIN {User_Effective_Role} ON {User_Effective_Role}.[User_Id] = @UserId AND {Activity_Def_Role}.[Role_Id] = {User_Effective_Role}.[Role_Id] WHERE {Activity_Def_Role}.[Activity_Def_Id] = {Activity_Definition}.[Id] ) ) ) ) /* Custom Filters */ AND 1 = 1 /* End of Custom Filters */ ORDER BY [DaysOverdue] DESC, [DueDate], [StartDate]