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
Sainath VenkatSainath Venkat 

trigger to count child records in self lookup relationship(same object)

Hello Guys,

I am working on one scenario in which an object is having self lookup relationship and need to count records based on record types.

I have an object called "Property_Assignment__c" which has two record types.
1) Primary Property Assignment
2) Secondary Property Assignment
Its also having a self lookup relationship and this lookup field "Primary_Assignemnt__c" is available on "Secondary Property Assignment" page layout which is lookup to "Primary Property Assignment".
Now if I create any records in "Secondary Property Assignment" record type then I need to count and save in "Number_Of__Dependents" field in "Primary Property Assignment".
Can anyone help me to solve this problem if possible please.
 
Best Answer chosen by Sainath Venkat
Adilson Arcoverde JrAdilson Arcoverde Jr
Hi Sainath,

Could try this code?
 
trigger PropertyAssignmentTrigger on Property_Assignment__c (after insert) {
    if( Trigger.isInsert && Trigger.isAfter ) {
        String rtSecondary = Property_Assignment__c.SObjectType.getDescribe().getRecordTypeInfosByName().get('Secondary Property Assignment').getRecordTypeId();

        Set<String> primaryAssignmentsIds = new Set<String>();

        for( Property_Assignment__c assignment : Trigger.new ) {
            if( assignment.RecordTypeId == rtSecondary && assignment.Primary_Assignemnt__c != null ) {
                primaryAssignmentsIds.add( assignment.Primary_Assignemnt__c );
            }
        }

        List<Property_Assignment__c> primaryAssignments = [Select Id, Number_Of__Dependents__c, (Select Id from Secondary_Assignments__r) from Property_Assignment__c where Id in :primaryAssignmentsIds];
        for( Property_Assignment__c primaryAssignment : primaryAssignments ) {
            primaryAssignment.Number_Of__Dependents__c = primaryAssignment.Secondary_Assignments__r.size();
        }

        if( primaryAssignments.size() > 0 ) {
            update primaryAssignments;
        }
    }
}

You have to replace Secondary_Assignments__r for the real value. You coud find the child relationship name in Primary_Assignemnt__c definition (see the image below to assist you identifying the correct name.

User-added image 

I hope you find this solution helpful. If it does, please mark as Best Answer to help others too.

Regards.

All Answers

Adilson Arcoverde JrAdilson Arcoverde Jr
Hi Sainath,

Could try this code?
 
trigger PropertyAssignmentTrigger on Property_Assignment__c (after insert) {
    if( Trigger.isInsert && Trigger.isAfter ) {
        String rtSecondary = Property_Assignment__c.SObjectType.getDescribe().getRecordTypeInfosByName().get('Secondary Property Assignment').getRecordTypeId();

        Set<String> primaryAssignmentsIds = new Set<String>();

        for( Property_Assignment__c assignment : Trigger.new ) {
            if( assignment.RecordTypeId == rtSecondary && assignment.Primary_Assignemnt__c != null ) {
                primaryAssignmentsIds.add( assignment.Primary_Assignemnt__c );
            }
        }

        List<Property_Assignment__c> primaryAssignments = [Select Id, Number_Of__Dependents__c, (Select Id from Secondary_Assignments__r) from Property_Assignment__c where Id in :primaryAssignmentsIds];
        for( Property_Assignment__c primaryAssignment : primaryAssignments ) {
            primaryAssignment.Number_Of__Dependents__c = primaryAssignment.Secondary_Assignments__r.size();
        }

        if( primaryAssignments.size() > 0 ) {
            update primaryAssignments;
        }
    }
}

You have to replace Secondary_Assignments__r for the real value. You coud find the child relationship name in Primary_Assignemnt__c definition (see the image below to assist you identifying the correct name.

User-added image 

I hope you find this solution helpful. If it does, please mark as Best Answer to help others too.

Regards.
This was selected as the best answer
Sainath VenkatSainath Venkat
Hello Adilson Arcoverde Jr,

Hope you doing good.

Thanks you so much for helping me out on this problem.

Thanks a lot for the quick reply and I made changes to the code which you have helped me and is working fine once I insert but the count should decrease once if I delete any secondary Property assignments are deleted.

I did change the code ans works on delete too. Below is my code.
trigger PropertyAssignmentTrigger on Property_Assignment__c (after insert, after delete) {
     String rtSecondary = Property_Assignment__c.SObjectType.getDescribe().getRecordTypeInfosByName().get('Secondary Property Assignment').getRecordTypeId();
Set<String> primaryAssignmentsIds = new Set<String>();
    if(trigger.isAfter){
    if( Trigger.isInsert) {
        for( Property_Assignment__c assignment : Trigger.new ) {
            if( assignment.RecordTypeId == rtSecondary && assignment.Primary_Assignemnt__c != null ) {
                primaryAssignmentsIds.add( assignment.Primary_Assignemnt__c );
            }
        }
    }
    if(Trigger.isDelete){
        for(Property_Assignment__c assignment : Trigger.old ){
             if( assignment.RecordTypeId == rtSecondary && assignment.Primary_Assignemnt__c != null ) {
                primaryAssignmentsIds.add( assignment.Primary_Assignemnt__c );
            }
        }
    }
            

        List<Property_Assignment__c> primaryAssignments = [Select Id, Number_Of_Dependents__c, (Select Id from Property_Assignments__r) from Property_Assignment__c where Id in :primaryAssignmentsIds];
        for( Property_Assignment__c primaryAssignment : primaryAssignments ) {
            primaryAssignment.Number_Of_Dependents__c = primaryAssignment.Property_Assignments__r.size();
        }

        if( primaryAssignments.size() > 0 ) {
            update primaryAssignments;
        }
    }
    }

 
Adilson Arcoverde JrAdilson Arcoverde Jr
Hi Sainath,

The issue was in the if that checks trigger event statement. Please try this code:
trigger PropertyAssignmentTrigger on Property_Assignment__c (after insert, after delete) {
    String rtSecondary = Property_Assignment__c.SObjectType.getDescribe().getRecordTypeInfosByName().get('Secondary Property Assignment').getRecordTypeId();
    Set<String> primaryAssignmentsIds = new Set<String>();
    
    if( Trigger.isInsert && Trigger.isAfter ) {
        for( Property_Assignment__c assignment : Trigger.new ) {
            if( assignment.RecordTypeId == rtSecondary && assignment.Primary_Assignemnt__c != null ) {
                primaryAssignmentsIds.add( assignment.Primary_Assignemnt__c );
            }
        }
    } else if(Trigger.isAfter && Trigger.isDelete){
        for(Property_Assignment__c assignment : Trigger.old ){
             if( assignment.RecordTypeId == rtSecondary && assignment.Primary_Assignemnt__c != null ) {
                primaryAssignmentsIds.add( assignment.Primary_Assignemnt__c );
            }
        }
    }

    List<Property_Assignment__c> primaryAssignments = [Select Id, Number_Of_Dependents__c, (Select Id from Property_Assignments__r) from Property_Assignment__c where Id in :primaryAssignmentsIds];
    for( Property_Assignment__c primaryAssignment : primaryAssignments ) {
        primaryAssignment.Number_Of_Dependents__c = primaryAssignment.Property_Assignments__r.size();
    }

    if( primaryAssignments.size() > 0 ) {
        update primaryAssignments;
    }
}

Best regards.
Sainath VenkatSainath Venkat
Hi Adilson Arcoverde Jr,

I have followed your code and updated to work on all scenarios, insert update and delete.

Can you help me with test class if possible. I am sharing the complete code below.
trigger PropertyAssignmentTrigger on Property_Assignment__c (after insert, after update, after delete) {
String rtSecondary = Property_Assignment__c.SObjectType.getDescribe().getRecordTypeInfosByName().get('Secondary Property Assignment').getRecordTypeId();
Set<String> primaryAssignmentsIds = new Set<String>();
    if(trigger.isAfter){
    if( Trigger.isInsert) {
        for( Property_Assignment__c assignment : Trigger.new ) {
            if( assignment.RecordTypeId == rtSecondary && assignment.Primary_Assignemnt__c != null && assignment.Status__c==true) {
                primaryAssignmentsIds.add( assignment.Primary_Assignemnt__c );
            }
        }
    }
            if( Trigger.isUpdate) {
        for( Property_Assignment__c assignment : Trigger.new ) {
            if( assignment.RecordTypeId == rtSecondary && trigger.newmap.get(assignment.id).Status__c!=trigger.oldmap.get(assignment.id).Status__c) {
                primaryAssignmentsIds.add( assignment.Primary_Assignemnt__c );
            }
        }
    }
    if(Trigger.isDelete){
        for(Property_Assignment__c assignment : Trigger.old ){
             if( assignment.RecordTypeId == rtSecondary && assignment.Primary_Assignemnt__c != null && assignment.Status__c==true) {
                primaryAssignmentsIds.add( assignment.Primary_Assignemnt__c );
            }
        }
    }
            

        List<Property_Assignment__c> primaryAssignments = [Select Id, Number_Of_Dependents__c, (Select Id, Status__c from Property_Assignments__r) from Property_Assignment__c where Id in :primaryAssignmentsIds AND  Status__c = true];
        for( Property_Assignment__c primaryAssignment : primaryAssignments ) {
            primaryAssignment.Number_Of_Dependents__c = primaryAssignment.Property_Assignments__r.size();
        }

        if( primaryAssignments.size() > 0 ) {
            update primaryAssignments;
        }
    }
    }