+ Start a Discussion
Patrick FordPatrick Ford 

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?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])




 
ShirishaShirisha (Salesforce Developers) 
Hi Patrick,

Greetings!

You can simply query all the records with the required recordType instead of checking before the Soql Query whch will return the records with the required recordType only to update.

Also,since you have created the Apex class this should work for all the interfaces.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri