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
Patrick8020Patrick8020 

Created a custom field for a user which adds up total account hours

I have a field within each account called: Allocated_Hours__c . This is a forumla which basically works out the number of hours each account gets. Within the User area I then have a field which displays the number of hours a user has in there month. What I want to be able to do is:

Add up the total number of hours a user has for their accounts and subtract that against the number of hours they have.

For example:

Peter has 14 accounts at 10 hours each = 140 hours. He gets allocated 150 hours so therefore he has 10 hours spare.

Any help would be much appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
vbsvbs
When I said manually, I meant programatically. This is something which can be achieved via a trigger. This is a tested and frequently used solution so no need to reinvent the wheel:
http://www.anthonyvictorio.com/salesforce/roll-up-summary-trigger/

All Answers

vbsvbs
Patrick - What you need here is a simulation of the rollup summary feature. The relationship being lookup you will have to build this programatically. There are two approaches:
1. If the totals do not need to be reflected instantly on User object then you can use Batch Apex with scheduling at intervals as you need OR
2. Add a trigger on the Account object after insert, update, delete, undelete. You will need to manually calculate the totals and update the relevant totals field on User object.
Patrick8020Patrick8020

Thanks for your reply.

It doesn't have to be real time however I do not think updating manually would be effective. The thing that is annoying is that the Roll Up summary is not available through the user object. I just want to SUM the total number of hours a user has for an account

vbsvbs
When I said manually, I meant programatically. This is something which can be achieved via a trigger. This is a tested and frequently used solution so no need to reinvent the wheel:
http://www.anthonyvictorio.com/salesforce/roll-up-summary-trigger/
This was selected as the best answer
Patrick8020Patrick8020

Yes I agree. However I am really struggling to do this as I am not an advanced salesforce developer. Where would I edit these pieces of code - through the formula area or is it under an advanced dev bit ?

Or equally would you recommend I leave alone as it is complicated? 

vbsvbs
This would be the task of a developer and preferably done by someone who knows triggers and class behaviour. If you are a novice read up further or engage someone suitable to get this done.
Patrick8020Patrick8020

Thanks for your time in replying to this - I will mark your answer as complete.

Thanks