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
Arati PalliathuArati Palliathu 

Trigger to count only unique records in related list?

 Hi, I am trying to have to have a field on a Master Object that counts the number of records with unique user field in a related list from a Detail object.  How could I do this with a trigger? 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Arati,

May I suggest you please refer the below link for reference. Hope it helps.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
Arati PalliathuArati Palliathu
Unfortunately, that trigger is designed for objects with a Lookup relationship, as opposed to the Master-Detail relationship that my objects have. 
jigarshahjigarshah
Arati,

Here is how you can achieve this.

1. Create a number field on the child records which is set to a value of 1 or 0 depending on your uniqueness logic. Lets call it UniquenessCount.
2. Create another Rollup Summary field on the Parent object, to SUM the UniquenessCount field value within the Rollup Summary.

Please mark the thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
Arati PalliathuArati Palliathu
Hi, What sort of uniqueness logic should i be using? 
jigarshahjigarshah
Simplest way to do so is using a compound key or an individual field on the object which would be marked as a Unique field. Check the Requires a Unique Value check box to true on field metadata. This will permit only unique records being inserted and thus help achieve your goal of aggregating unique values using roll-up. Regards, Jigar Shah
Arati PalliathuArati Palliathu
Sorry to ask so many questions, but how can i access field metdata? Through Workbench?
Arati PalliathuArati Palliathu
Also, I am trying to build a trigger as suggested by Rahul but the trigger on my child object gives me the following error: 
Error: Compile Error: Illegal assignment from Schema.SObjectField to Id at line 6 column 67

trigger triggerParent on TreatmentProcedure__c (after insert, after update, after delete, after undelete) {
  Map<Id,Treatment__c> parents = new Map<Id,Treatment__c>();
  if(Trigger.new<>null)
    for(TreatmentProcedure__c c:Trigger.new)
      if(Treatment__c.Unique_Radiologist_Count_Tesy__c<>null)
        parents.put(Treatment__c.Unique_Radiologist_Count_Tesy__c,new Treatment__c(Id=Treatment__c.Unique_Radiologist_Count_Tesy__c));
  if(Trigger.old<>null)
    for(TreatmentProcedure__c c:Trigger.old)
      if(Treatment__c.Unique_Radiologist_Count_Tesy__c<>null)     
        parents.put(Treatment__c.Unique_Radiologist_Count_Tesy__c,new Treatment__c(Id=Treatment__c.Unique_Radiologist_Count_Tesy__c));
  update parents.values();
}

where Treatment Procedure is the child object, Treatment is the parent object and Unique_Radiologist_Count_Tesy__c is the field on the treatment object  that needs to be populated 

How can i fix this error?