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 asset object with status as installed

I have a custom Object in which I have  a field user and added a field to display no. of assets(for count) created by user in asset object where status is installed.
Like if I have 5 assets created by user A whose status is installed then in custom object field it should be updated as 5.If any of the asset is deleted/updated then it should updated. the count as well.
Same way if user B has 3 assets whose status is installed then for User B in custom object in count field it should show as 3.

Request you to help on priority as I am new to salesforce.
 
Dhanya NDhanya N
Hi Preeti,

Please check this code.

Trigger : 
trigger on Assets assetstrigger(after insert, after delete) {

	assetsHandler objHandler = new assetsHandler ();
	
	if(trigger.isAfter && trigger.isInsert)
        objHandler.onAfterInsertOrDelete(trigger.new);
		
	if(trigger.isAfter && trigger.isDelete)
		objHandler.onAfterInsertOrDelete(trigger.old);
}
Controller :
public with sharing class assetsHandler {

	public void onAfterInsertOrDelete(list<assets> lstAssets) {
		
		list<Custom_Object__c> lstToUpdateInCustomObject = new list<Custom_Object__c>();
		for(assets objAssets: lstNewAssets) {
			
			setCustomObjectId.add(objAssets.CustomObjectId)
		}
		
		AggregateResult[] groupedResults = [SELECT COUNT(Id), Custom_Object__c FROM assets where Custom_Object__c IN :setCustomObjectId AND Status = 'Installed' GROUP BY Custom_Object__c ];

		 for(AggregateResult ar:groupedResults) {

			 Id custid = (ID)ar.get('Custom_Object__c');

			 Integer count = (INTEGER)ar.get('expr0');

			 Custom_Object__c cust1 = new Custom_Object__c(Id=custid);

			 cust1.count__c = count;

			 lstToUpdateInCustomObject.add(cust1);

		   }

		   update lstToUpdateInCustomObject;

		}

	}
}


Thanks,
Dhanya
Preeti Khanna 10Preeti Khanna 10
Hi Dhanya,

Thanks for your help!
Above code is not working and giving compilation errors as well.

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.

I feel we need to write trigger both on asset and custom object.

Please help and waiting for a reply from your side.

Regards,
Preeti