+ Start a Discussion
NikiG22NikiG22 

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

Devendra@SFDCDevendra@SFDC

 

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

Cory CowgillCory Cowgill

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.

NikiG22NikiG22

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?

 

trigger CountRelatedRS on Contact (Before insert, Before update) {

//Resume_Subscriptions__c are the related records
//Contact is the "main" record
//Idea is to update a field on the main record whenever an edit is made to a related record

    Contact  [] scl = Trigger.new;
    String sid = null;
    sid = scl[0].id;
        
         Integer i = [select count() from Resume_Subscriptions__c where Status__c = 'Active']; 

        // update the master record
    Contact [] s =[select id from Contact where id = :sid];
 
    s[0].Active_Contact_RS_Count__c= i; 
        // write to database
        update s[0];

}

 

Devendra@SFDCDevendra@SFDC

 

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

 

 

 

NikiG22NikiG22

thank you! but im now getting the following error when im creating a contact?

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger CountActiveRS caused an unexpected exception, contact your administrator: CountActiveRS: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0: Trigger.CountActiveRS: line 16, column 7

 

Devendra@SFDCDevendra@SFDC

 

I hope you are done with trigger CountRelatedRS.

 

Can you please post trigger code for CountActiveRS?

 

Thanks,
Devendra

 

NikiG22NikiG22

here is my code

 

trigger CountActiveRS on Contact (Before insert, Before update) {

//Resume_Subscriptions__c are the related records
//Contact is the "main" record
//Idea is to update a field on the main record whenever an edit is made to a related record

    Contact  [] scl = Trigger.new;
    String sid = null;
    sid = scl[0].id;
        
         Integer i = [select count() from Resume_Subscriptions__c where Status__c = 'Active']; 

        // update the master record
    Contact [] s =[select id,Active_Contact_RS_Count__c from Contact where id = :sid];
 
    s[0].Active_Contact_RS_Count__c= i; 
        // write to database
        update s[0];

}

 But im getting the error i mentioned above

Devendra@SFDCDevendra@SFDC

 

Hi NikiG22,

 

Instead of (before insert, before update) , use (after insert, after update) and check what hapens.

 

Thanks,

Devendra

NikiG22NikiG22

I get this huge error

 

rror: Invalid Data.
Review all error messages below to correct your data.
Apex trigger CountActiveRS caused an unexpected exception, contact your administrator: CountActiveRS: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 003W0000002FkBLIA0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CountActiveRS: maximum trigger depth exceeded Contact trigger event AfterInsert for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL] Contact trigger event AfterUpdate for [003W0000002FkBL]: []: Trigger.CountActiveRS: line 18, column 9