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
Preeti Khanna 10Preeti Khanna 10 

Update count on custom object based on records created in another object

Hi All,

Need your help as I am new to salesforce.

I have a ObjectA in which I have a field user ,start date,end date and count field .I need to update the count field based on below criteria from asset object.

(To display no. of assets from asset object -criteria is created by user from asset object(based on owner field)  should match with user of object A and installationdate of asset object should be between startdate,eend date of target object and asset status should be installed.
User will enter in target object -user name,start date,end date and based on that count field should get updated from assets object.If any assets is deleted then it should be updated accordingly.

Thanks in advance!

Regards
Preeti

 
JyothsnaJyothsna (Salesforce Developers) 
Hi ,

You can achieve this functionality in two different ways 
Step 1
  • Create a master-detail relationship between the two objects. 
  • Create a roll-up summary field on parent object with count function.
Step 2:
  • Create a lookup relationship between the two objects. 
  • Create a custom field on the parent object.
  • Write the trigger on Child object.
For example, I want to create a custom field  (no_of_contacts) on Account object. Write a trigger on contact object.
 
trigger ContactCount on Contact (after insert, after update, after delete) {
    Map<Id, List<Contact>> AcctContactList = new Map<Id, List<Contact>>();
    Set<Id> AcctIds = new Set<Id>();    
    List<Account> AcctList = new List<Account>();
    List<Contact> ConList = new List<Contact>();
    
    if(trigger.isInsert || trigger.isUPdate) {
        for(Contact Con : trigger.New) {
            if(String.isNotBlank(Con.AccountId)){
                AcctIds.add(Con.AccountId);  
            }   
        }  
    }
    
    if(trigger.isDelete || trigger.isUPdate) {
        for(Contact Con : trigger.Old) {
            AcctIds.add(Con.AccountId);     
        }  
    }           
    
    if(AcctIds.size() > 0){
        ConList = [SELECT Id, AccountId FROM Contact WHERE AccountId IN : AcctIds];
        
        for(Contact Con : ConList) {
            if(!AcctContactList.containsKey(Con.AccountId)){
                AcctContactList.put(Con.AccountId, new List<Contact>());
            }
            AcctContactList.get(Con.AccountId).add(Con);      
        }                           
        
        System.debug('Account Id and Contact List Map is ' + AcctContactList);
            
        AcctList = [SELECT No_of_contacts__c FROM Account WHERE Id IN : AcctIds];
        
        for(Account Acc : AcctList) {
            List<Contact> ContList = new List<Contact>();
            ContList = AcctContactList.get(Acc.Id);
            Acc.No_of_contacts__c= ContList.size();
        }    
        
        System.debug('Account List is ' + AcctList);
        update AcctList;    
    }

}
 


Regards,
Jyothsna
 
Preeti Khanna 10Preeti Khanna 10
Hi Jyothsna ,

Thanks for your help!
I cannot create master details relationship as we already have 2 on object.

I have one query..Please suggest whether we need to write code on asset object or custom object becuase in my case there in no lookup relationship between the two.
I need to update count field on custom object when user is selected and start date and end date is entered by user.Then system will go to asset object and will match user name and filter date field on asset object with startdate date and end date field of custom object.Count field will be updated when user selects startdate and end date in object/when asset is updated in specified date period by a specified user..
Because asset status changed to installed suppose on 5th May but in custom object there was no record to calculate no. of records.
Now if in custom object on 31st May user make a record with start date as 1 st may and end date as 31st may to find no. of assets which are installed between 1st may and 31st may so that in count field count will be updated for assets where status is installed between 1st may and 31st may.

Thanks in advance!