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
Dave The RaveDave The Rave 

Add counter field to records on custom object (related to contacts)

  • I have 2 objects: Contact and Rlayer__c
  • Each contact can have 1 or more records on Rlayer__c
  • I would like to create a counter field on Rlayer (now, and then when a record is created on Rlayer.
  • So the first record related to the contact on Rlayer WHERE sportsclub__c = 'Windsor' = should have a field count__c = 1
  • The 2nd record on Rlayer  related to the contact on Rlayer WHERE sportsclub__c = 'Windsor' = should have a field count__c = 2
  • The 3rd record on Rlayer  related to the contact on Rlayer WHERE sportsclub__c = 'Windsor' = should have a field count__c = 3
How can I achieve this?

Ideally with DML code and 'update' command. I have an idea of DML but do not know who to select records from 2 objects related to each other.

Thanks, Dave


 
v varaprasadv varaprasad
Hi Dave,


Please check below sample class:
 
trigger updateCount on Rlayer__c(before insert){
  set<id> conids = new set<id>();
  
   for(Rlayer__c rl : trigger.new){
       if(rl.contact__C != null){
	     conids.add(rl.contact__C);
	   }
   
   }
   
   list<Rlayer__c> lstRls = [select id, name, count__c from Rlayer__c where contact__C in : conids];

  for(Rlayer__c rl : trigger.new){
      if(lstRls.size() == 0){
	    rl.count__c = 1;
	  }else{
	    rl.count__c = lstRls.size() + 1;
	  
	  }
  
  
  }



}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
Sampath SuranjiSampath Suranji
Hi,
Try a trigger like below.
public class updateRlayerCountHelper {
    public static Map<id,integer> contactCountMap {
        get{
            if(contactCountMap==null){
                contactCountMap= new  Map<id,integer> ();          
            }
            return contactCountMap;
          
        }
        set;
    }
}
 
trigger updateCount on Rlayer__c (before insert) {
    System.debug('called the trigger');
    List<ID> allRlayersId= new list<id>();
    List<Rlayer__c> allRlayers= new list<Rlayer__c>();
    for(Rlayer__c r:trigger.new){
        allRlayersId.add(r.Id);
        allRlayers.add(r);
    }
    for(AggregateResult ar:[select contact__c, Max(count__C)countM from Rlayer__c where id in:allRlayersId group by contact__c ]){
        Id c=(Id) ar.get('contact__c');
        updateRlayerCountHelper.contactCountMap.put(c,(integer) ar.get('countM'));
    }
    integer maxCount=0;
    for(Rlayer__c r:allRlayers){
        maxCount = updateRlayerCountHelper.contactCountMap.get(r.contact__r.Id);
        
        if(maxCount==null){
            maxCount=0;
        }
        r.count__c = maxCount +1;
        
        updateRlayerCountHelper.contactCountMap.put(r.contact__r.Id, maxCount +1);
       
    }
   
}

Best regards
Sampath