2

I have a query that I wanted to add additional sorting to the nested query ActivityHistories. The sorting works with ORDER BY ActivityDate DESC, LastModifiedDate DESC but when I add a third condition ORDER BY ActivityDate DESC, LastModifiedDate DESC, CreatedDate DESC I get a permission error stating:

"There is an implementation restriction on ActivityHistories. When you query this relationship, security evaluation is implemented for users who don't have administrator permissions, and you must use a specific sort order: ActivityDate DESC, LastModifiedDate DESC"

I understand from these posts that is has to do with admin permissions:

https://community.skuid.com/t/salesforce-implementation-restriction-on-activityhistories/4939

https://documentation.conga.com/grid/latest/activity-history-and-open-activities-implementation-restriction-143886917.html

The solution suggests that I have to give my users "View All Data" permissions which I do not want to do.

Is there a way around this limitation without having to give view all data permissions to people we do not want to have this access?

Here is the query we want:

SELECT Id, Name, (SELECT WhoId, WhatId, Subject, Status, Response_Channel__c, Reason__c, RPC__c, Priority, OwnerId, Owner.Name, Location, IsTask, IsClosed, Id, DurationInMinutes, Description, CallType, CallObject, CallDurationInSeconds, CallDisposition, ActivityType, ActivityDate,CreatedDate, LastModifiedDate, AccountId,Date_Activity_Logged__c FROM ActivityHistories ORDER BY ActivityDate DESC, LastModifiedDate DESC, CreatedDate DESC limit 50), (SELECT n.Title, n.ParentId, n.OwnerId,  n.Owner.Name, n.Id, n.CreatedById, n.CreatedDate, n.Body, LastModifiedDate FROM Notes n ORDER BY n.LastModifiedDate DESC limit 50),(SELECT a.ParentId, a.OwnerId, a.Owner.Name, a.Name, a.Id, a.Description, a.ContentType, a.CreatedDate, a.CreatedById, LastModifiedDate FROM Attachments a ORDER BY a.LastModifiedDate DESC limit 50) FROM Lead WHERE Id =: leadId ];
2
  • Can you create a custom TEXT formula field on Activity that is the concatenation of the three datetime fields (concatenated as 3 ISO datetimes)? Then order by the custom formula field.
    – cropredy
    Commented Sep 2, 2022 at 1:34
  • @cropredy I am not sure I am following. How would I create a concat Text formula field with DateTimes? I attached a picture of what I was thinking but the syntax obviously doesn't pass. And then further, once the datetimes are concatenated, how would the ORDER BY in the query be able to filter by a string of three different datetimes?
    – Olivia
    Commented Sep 2, 2022 at 17:35

1 Answer 1

2

Create a formula field on Activity called Sortable_Activity_LastMod_CreatedDate__c

TEXT(UNIXTIMESTAMP(ActivityDate)) &'-'& 
TEXT(UNIXTIMESTAMP(LastModifiedDate))&'-'&
TEXT(UNIXTIMESTAMP(CreatedDate)) 

This will generate a string that will look like

1632441600-1632522839-1632522839
1632441600-1632530526-1632530526
...

that you can sort.

Notes -- Assumes ActivityDate has a value, if not, the formula will generate

-1635469551-1635469551

You may want to change the formula to use ...BLANKVALUE(ActivityDate,DATEVALUE(someVal))

where someVal is either a constant date in the past or deep future, depending on your business use case

8
  • How does this take into consideration DESC?
    – Olivia
    Commented Sep 4, 2022 at 1:53
  • it is a string that can be sorted DESC, it will sort ActivityDate DESC, if any dups, then will sort LastModifiedDate DESC, if dups then finally sort CreatedDate DESC
    – cropredy
    Commented Sep 6, 2022 at 4:17
  • This makes sense and I just tried running it and it works, thank you. I am curious about how the ORDER BY can look at three values separated by a dash. Is this how it is compiled with three separate values? I was under the impression that when it compiles the query runs something like "ORDER BY 1632441600 AND ORDER BY 1632522839 AND ORDER BY 1632522839". Just curious as to how it is working on the backend
    – Olivia
    Commented Sep 6, 2022 at 19:39
  • no - it is ORDER BY 1632441600-1632522839-1632522839 as a single string. I used the dash merely to make the concatenated string easier to read. I could have used a colon : or an emoji or dispensed with it entirely
    – cropredy
    Commented Sep 6, 2022 at 20:45
  • 1
    Because the entire OP sort is all fields DESC, sorting on one field DESC is equivalent. If you were sorting on field 1 desc, field 2 ASC, field 3 DESC - my approach would not work
    – cropredy
    Commented Sep 8, 2022 at 22:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .