• Patrick Ford
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 0
    Replies
So I'm beginning to understand ContentDocument and ContentDocumentLink SOQL queries... I think what I want to do is make new ContentDocumentLinks between my ContentDocuments... so that the LinkedEntityId is the Contact instead of the Case... but struggling with how to implement this as ContentDocumentLinks cannot be edited...

Here's my SOQL for looking up the files owned by a particular user as a test case.
 
SELECT Title, OwnerId, Id, FileExtension
FROM ContentDocument
WHERE OwnerId = '0051Q00000Ef3t3QAB'

and files associated with a single case:
SELECT ContentDocument.title, ContentDocument.ownerId, Id, ContentDocument.FileExtension
FROM ContentDocumentLink
WHERE LinkedEntityId = '5001Q00000pJCv1QAG'

but i'm unclear where to from here!
I have created an apex class (run by a lighting quick action) that creates a "timecard" (timecard__c custom object) based on the activities/events of a Salesforce user... it gives basic information on the sum of time spent based on a few different metrics including the event record type, the day of the month (i.e first, second, third... thirtieth, thirty-first), and a picklist field.

This "timecard" is currently also filtered by the pay period... there are two pay periods in a month, the 1-15th and 16-end of month. Each event has a custom formula field that names which payroll period it is based on the activity date... so something like "2020-05 Payroll Period 1" or "2019-11 Payroll Period 2" etc. So I can just filter based on the payroll period.

Everything works... all is well...

But I just got the requirement (in addition to keeping the above) to allow users to input the start and end date of the "timecard" they want to create... so instead of just naming the payroll period, they should also be able to say "start the timecard on May 2, 2020 and end it on May 9, 2020).

To be clear, we still want the option for users to be able to choose just a payroll period for one type of timecard... but for this new type of timecard we want the start and end dates. I'm imagining to record types for timecard__c object... one that goes by payroll period, the other that goes by a start and end date.

Logistically, it would be easy for me to do this in a clunky way.

I can imagine just having an if statement at the beginning of each method to check the record type and then run a soql query differently based on that... or to basically duplicate a lot of the code I've made into new methods... but wondering if there's a better way to do this with interface, extension, abstract, or virtual classes?For my project, do I use an Interface, Extension, Abstract, or Virtual class?
I have created an apex class (run by a lighting quick action) that creates a "timecard" (timecard__c custom object) based on the activities/events of a Salesforce user... it gives basic information on the sum of time spent based on a few different metrics including the event record type, the day of the month (i.e first, second, third... thirtieth, thirty-first), and a picklist field.

This "timecard" is currently also filtered by the pay period... there are two pay periods in a month, the 1-15th and 16-end of month. Each event has a custom formula field that names which payroll period it is based on the activity date... so something like "2020-05 Payroll Period 1" or "2019-11 Payroll Period 2" etc. So I can just filter based on the payroll period.

Everything works... all is well...

But I just got the requirement (in addition to keeping the above) to allow users to input the start and end date of the "timecard" they want to create... so instead of just naming the payroll period, they should also be able to say "start the timecard on May 2, 2020 and end it on May 9, 2020).

To be clear, we still want the option for users to be able to choose just a payroll period for one type of timecard... but for this new type of timecard we want the start and end dates. I'm imagining to record types for timecard__c object... one that goes by payroll period, the other that goes by a start and end date.

Logistically, it would be easy for me to do this in a clunky way.

I can imagine just having an if statement at the beginning of each method to check the record type and then run a soql query differently based on that... or to basically duplicate a lot of the code I've made into new methods... but wondering if there's a better way to do this with interface, extension, abstract, or virtual classes?

Basically, for the payroll type of sorting, I would do this:
for(AggregateResult objAgr : [SELECT   ActivityDate, SUM(DurationHours__c)
FROM Event 
WHERE OwnerId = :u.Id AND
Payroll_Period__c  = :payPeriod
GROUP BY ActivityDate])
Whereas for the start and end date sorting I would do this:
 
for(AggregateResult objAgr : [SELECT   ActivityDate, SUM(DurationHours__c)
FROM Event 
WHERE OwnerId = :u.Id AND
StartDateTime  >= :startDate AND
StartDateTime  <= :endDate
GROUP BY ActivityDate])




 
I've got the following code
 
User u =   [SELECT Id 
            FROM User
            Where Email='admin@***.org'];

for(AggregateResult objAgr : [SELECT   RecordType.Name, SUM(DurationHours__c)
                              FROM     Event 
                              WHERE    OwnerId = :u.Id
                              GROUP BY RecordType.Name])
{
    String label = string.valueof(objAgr.get('RecordType.Name'));
    Double total = double.valueof(objAgr.get('expr0'));
    system.debug(label + ' | ' + total);
}

I get an error when running this from an execute anonymous window that says:

System.SObjectException: Invalid field RecordType.Name for AggregateResult

I am able to use this exact same code for a similar query, except instead of grouping by RecordType.Name, I group by ActivityDate. This works perfectly. So how do I get RecordType.Name results?