You need to sign in to do that
Don't have an account?
NikiG22
Count the number of line items on a related object
Good Morning-
I am trying to create a trigger that will count the number if active line items on a contact. Logic is I need to see how many resume subscriptions are active on one contact. Then add a value to my look up field that does not allow the user to select a contact that already has an active sub.
I am traveling all week and need some help creating the trigger.
Any help would be greatly appreciated.
Cheers,
Niki
Hi NikiG22,
Please refer below post, It is kind of a similar requirement.
http://boards.developerforce.com/t5/Apex-Code-Development/Count-Related-Records-with-Apex-Trigger/td-p/164988
Hope this helps.
Thanks,
Devendra
If this is a Master - Detail Relationship than you can use Roll Up Summary fields to accomplish this without having to write a Trigger.
If this is a standard lookup, I would suggest reviewing to see if you can make it a Master - Detail. However, if that is not possible you can of course do this in a trigger. The general pattern will be:
1. Create a Trigger on child object (Subscriptions) that will be active on After Insert, After Upate, After Delete, and After UnDelete
2. Use Aggregate SOQL to perform Count logic for the Parent Records where the child recs meet your criteria.
3. Update parent records using the counts from the Aggregate SOQL.
Don't forget that you need to build your trigger in Sandbox environment, an that you need to build adequate test cases.
Refer to the Force.com Documentation for more.
Thank you for pointing me to that post , but im still having issues? I have it saved but it is not adding the count of active resume subs for my contacts?
Hi NikiG22,
You have not retrieved the field which you need to update in below query.
-------------------------------------------------------------------------
Contact [] s =[select id from Contact where id = :sid];
------------------------------------------------------------------------
It should be,
----------------------------------------------------------------------------------------------------------------
Contact [] s =[select id,Active_Contact_RS_Count__c from Contact where id = :sid];
-----------------------------------------------------------------------------------------------------------------
and then make a update call.
Hope this helps.
Thanks,
Devendra
thank you! but im now getting the following error when im creating a contact?
I hope you are done with trigger CountRelatedRS.
Can you please post trigger code for CountActiveRS?
Thanks,
Devendra
here is my code
But im getting the error i mentioned above
Hi NikiG22,
Instead of (before insert, before update) , use (after insert, after update) and check what hapens.
Thanks,
Devendra
I get this huge error