function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mwardmward 

Referencing a custom field in Tasks?

So I have a custom object called Timesheet.  For Timesheet, I've included the built-in Activities/Tasks, and added a custom field to the Activities Custom Fields called "Hours Spent."  So, let's say this is a Timesheet for Project X.  I want to have a list of "Tasks" using the built-in Activities/Tasks that are related to my Timesheet.  For instance, "Perform Duty Y for Project X" is one task, and the "Hours Spent" field is set to 3.0, i.e. it took 3 hours to complete that task.

 

What I want is to have a custom field in Timesheet (not the Activities/Tasks) called "Total Hours Spent" that accumulates all of the "Hours Spent" from the individual Tasks below.  How can I retrieve those values, sum them up, and have them appear in a Timesheet custom field?

 

I attempted to use a Custom Field for Timesheet, "Formula" type.  However, in the "Enter Formula" area, there appears to be no way to select the fields of Tasks.  This is particularly hard to wrap my brain around, because the NUMBER of Tasks is never set.  Even if you could select fields of Tasks in the formula, one Timesheet could have 3 Tasks (meaning that three "Hours Spent" values would have to be added together) while another Timesheet could have 19 Tasks (meaning that nineteen "Hours Spent" values would have to be added together).  My gut is telling me I'm going to have to delve into Apex and iterate through however many Tasks there are, since it's variable.  I'm ready to get my hands dirty with Apex, but how do you reference Tasks, much less Custom Fields of Tasks, in code?

 

Thanks.

 

---

 

P.S. I'm a super newbie without any background in anything relevant to salesforce.com.  This is a personal project that I'm pursuing to become more fluent in the system as a whole.  As this is for fun/practice, I'm not at all concerned with any kind of flawed business logic, as I'm sure there are conventions/rules I'm unaware of.  At the moment, it's just an issue of learning the rules of custom fields.

DevAngelDevAngel

Hi mward,

 

You have discovered that Activities do not behave like other related lists.  Due to this you will likely need to create your own custom object to contain the Tasks for your timesheets.  Should be too hard or take too much time.  Since Activities are not specific to any particular object, I think it makes sense not to have fields on the Task that don't relate to the other objects.  For instance, if you have the Hours Spent field on the task object, then when adding a task for a Contact record, you see that field in the task layout, even it's not used for that paticular context.

 

Cheers,

mwardmward

Thanks so much for your easy-to-understand reply.

 

I went ahead and made my own "Tasks" custom object, and as such, have solved my problem :)