You need to sign in to do that
Don't have an account?
Sainath 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.
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.
Could try this code?
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.
I hope you find this solution helpful. If it does, please mark as Best Answer to help others too.
Regards.
All Answers
Could try this code?
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.
I hope you find this solution helpful. If it does, please mark as Best Answer to help others too.
Regards.
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.
The issue was in the if that checks trigger event statement. Please try this code:
Best regards.
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.