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
thomasGEthomasGE 

How to count cross object without link

I am totally new to Apex programming. Therefore I need some help. Here is the scenario: I want to count user licences per company. One country could have many companies. Object exists. I thought it would be a good idea to have the fields sfdc licence total, sfdc licence used and sfdc licence remaining on the company object (just like in the system overview). Then I want to have a country object related to it and let the same fields be there and have them updated with roll up summary. This should not be a real problem. But:

now I want to count user licences (on the user object there are fields like company name (picklist) and country and user licence. I want to count the number of salesforce licences used. How do I do that now with a trigger or other code? The number of licences per company counted should be written directly in the corresponding field on the company object and then the difference be calculated automatically (total - used = remaining). If remaining is 0 the system should throw an error message. 

Example: company A has 100 salesforce licences and 50 chatter free. Creating a new user the system now recalculates the used licences, eg. 90, the remaining then 10. And still no chatter free used, so remaining 50. 

The object structure should be country object as a collector for all company objects. How can I do it? I know there are workbooks and tutorials but it seems to be a bit more complicated as there is no way to have the user object related to other objects.

Thanks so much for helping me out...

best regards Thomas