You need to sign in to do that
Don't have an account?
Anton van Dijk
count number of records (lookup) in a field
Hi guys,
I have 2 custom objects (ObjectA__c and ObjectB__c)
in ObjectA__c I have created a lookup field for ObjectB__c.
My problem occurs when I want to have a field CountUsed__c in ObjectB__c that counts the number of times the object is used in the lookup field in ObjectA__c.
I've tried creating a formula field, but not it's not clear to me what the formula should be.
I also tried creating an Apex trigger. However, I couldn't get the select statement to work.
I want a field in ObjectB__c that is readonly and counts the number of time the object is used in the lookupfield @ ObjectA__c
Please advice the correct way to approach this :)
I have 2 custom objects (ObjectA__c and ObjectB__c)
in ObjectA__c I have created a lookup field for ObjectB__c.
My problem occurs when I want to have a field CountUsed__c in ObjectB__c that counts the number of times the object is used in the lookup field in ObjectA__c.
I've tried creating a formula field, but not it's not clear to me what the formula should be.
I also tried creating an Apex trigger. However, I couldn't get the select statement to work.
I want a field in ObjectB__c that is readonly and counts the number of time the object is used in the lookupfield @ ObjectA__c
Please advice the correct way to approach this :)
Fixed the bug. Please try the below trigger and let me know.
Regards
All Answers
You can try this app
https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EO7F9UAL
-Prakash
Try the below trigger on objectA__c
Let me know if you have any issues.
Regards
You can achieve it in two ways.
1 - Convert lookup relationship to Master Detail and then create a Rollup Summary field in ObjectB__c which will count ObjectA__c Object records.
2 - Create a Number field in ObjectB__c which will store the total count of ObjectA__c and then create a new formula field which will hold the newly create field value then add this formula field into the layout and remove Number filed. This way you will find a field which is read-only.
3 - Built a trigger which will be responsible for counting ObjectA__c records and then updating ObejectB__c records with the total value.
Sample code for the trigger is given below and in the below code I am counting no of Attachments into Account Object.
Thanks,
Amit Singh.
Thank you for your quick responses.
I don't want to be reliant on apps, so that's not an option.
The Master-Detail relationship is not an option either, since it makes the field required on ObjectA. In my case the field could be empty too.
The combination of number and formula field doesn't feel like a clean option, because I'll have more fields that don't provide more useful data.
I tried the trigger provided by Eldon, however, I'm getting an error when trying to update an ObjectA record.
Try with below code.
Let me know if this do the trick :)
Thanks,
Amit Singh.
Just at line 10 isInsert and isUndelete are no functions, I removed the "()" part.
Working as intended except for 1 situation:
deleting the field value without deleting the record.
inserting a new ObjectA record, updating it with a ObjectB value, deleting the whole ObjectA record are all good.
However when I try to delete the ObjectB value without deleting the ObjectA record I get the following error:
Can you try this code let me know the output
Regards
unfortunately this isnt working.
Line 33 needs a little fix:
Also I'm still getting the error when deleting an ObjectB__c value from the field in ObjectA__c.
I also noticed that if I switch 1 value for another, the old value doesnt get updated in count.
If the above code didnt work try another method like below,
Here ObjectB__r is the relationship name and
the helper class to avoid recursive calling of above trigger,
Here you will get the count when you do any operation on ObjectB__c
Regards
Anton,
Thanks,Try the below code and let me know the outcome :)
Amit Singh.
the new code doesn't give any errors.
assigning a relation updates the count by +1.
however, when I remove the relationship I need a -1 on the count, it doesnt substract after an update to the ObjectA record.
Fixed the bug. Please try the below trigger and let me know.
Regards
Try below code.
Thanks,
Amit Singh.
Thank you for the fix.
There was one more bug which would not update count while deleting the last child :) Please see the below code which satisfies all your req,
Regards