You need to sign in to do that
Don't have an account?
Rollup Summary APEX Trigger
Good Afternoon:
Here is my situation:
I have one custom object Sites__c
I have a second custom object Subscriber__c.
Subscriber__c has a custom lookup field Address_ID__c
Address_ID__c.Fiber_Release__r is the same value as Site__c.Name although the two objects are not related.
What I want to do is to count the number of Subscriber__c records there are for a given Site__c using the logic above but without doing lookups and Master-Details.
Subscriber__c records are updated monthly via a bulk upsert process using the Apex Data loader and requires too much manual work for lookups.
I have no idea where to begin. Any help is appeciated.
Thanks,
Matt
You will need an apex trigger which will perform the rollup calculation
http://boards.developerforce.com/t5/Apex-Code-Development/HELP-Calculate-the-Sum-of-a-RelatedList/m-p/471483#M86395
Thanks, I am reading through it now. Some of it looks pretty foreign to me.
In order for that to work, so I have to have a relationship between Sites__c (where I want the aggregate value updated) and Subscriber__c (where I want the value to come from)?
Hampton
But yeah, it would be ideal if we could establish relationships between the two objects so that we can ensure there is no many-many mappings between the two objects.
No worries...just trying to avoid Master-Detail and lookup relatiosnhips if at all possible due to how we are updating/importing the data.
I appreciate all your help.
Hampton
Although I said its good have relationships, its not impossible to acheive using just the names.
I have modified the regular code to handle names. Its still not complete as it needs some work to be done, but maybe you can start off with this.
I will play around with this and see if I can make heads and tails of it :)
Here is what I have so far, I hope I am close :)
I am getting Compile Error: Variable does not exist: Address at line 20 column 76
Based on my original problem statement, am I heading down the right path?
Thanks,
Hampton
Hi Hampton,
To solve that compile error:
Repalce Address in "site SiteToUpdate = new site(Name = (String)(AddressDetail.get(Address)));" with AddressDetail.Address
May be you like to consider these before going ahead:
1. The first thing: Are the name unique? If so, the solution of using trigger will be helpful, else you need a unique identifier between the two tables and write the trigger based on it.
2. If you run this trigger for bulk, it will throw an error, because, select query is inside the for loop
Happy Coding
site SiteToUpdate = new site(Name = (String)(AddressDetail.get(Address)));
==>
site SiteToUpdate = new site(Name = (String)(AddressDetail.get('Address')));
I was thinking about his on my way into work this morning.
The records in the Subscriber__c object only get Upserted once a month. And it is a total of ~350k records each time. Would it make more sense or even be possible to do this update via a scheduled batch job?
I have one scheduled for the 6th of each month to delete out Subscriber__c records that were not updated with the data load. I could run this one a day later and update the Unit counts on Subscriber__c?
Thoughts? Possible? Better solution given the additional details?
Thanks,
Hampton
I almost have this I think... I am getting an error stating:
Error: Compile Error: Field is not writeable: Site.Name at line 20 column 47
What it should be doing there is pulling back all of the Site__c records where Site__c.Name=Address.
Thanks,
Hampton
Change that to point to the field which stores the address values.
As for your initial query, I would still go with a trigger as it would be a more elegant solution which will work on the monthly load as well as on ad-hoc changes.