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
Anoop Kumar 31Anoop Kumar 31 

Trigger : account has multiple contacts(with custom field age__c), i want to show youngest and oldest contact age in custom account fields youngest__c and oldest__c respectively.

I have tried this :
trigger Surajtrg on Contact (after insert, after update, after delete)
{
    set<id> act=new set<id>();
    List <Account> lstAccountsToUpdate = new List <Account>();
    if(trigger.isInsert ||trigger.isUpdate)
    {
        for(contact cc:trigger.new)
        {
            act.add(cc.AccountId);
         }
    }
    if(trigger.isDelete)
    {
        for(contact cc:trigger.old)
        {
            act.add(cc.AccountId);
        }
    }
    List<Account> shocksList = [SELECT Id,Youngest__c,(SELECT id,MIN(Age__c) FROM Contact) from Account where Id IN: act]; 
for(Account acc:shocksList) //(Select Id from Contacts)  represents size of Contacts        
{
    Account accObj = new Account ();
    accObj.Id = acc.Id;
   accObj.Youngest__c= acc.MIN(Age__c);
    lstAccountsToUpdate.add(accObj);
}
    Update lstAccountsToUpdate; 
}
 
Best Answer chosen by Anoop Kumar 31
v varaprasadv varaprasad
Hi Anoop,


Try This : 
 
trigger Surajtrg on Contact (after insert, after update, after delete)
{
    set<id> act=new set<id>();
    if(trigger.isInsert ||trigger.isUpdate)
    {
        for(contact cc:trigger.new)
        {
            act.add(cc.AccountId);
         }
    }
	
    if(trigger.isDelete)
    {
        for(contact cc:trigger.old)
        {
            act.add(cc.AccountId);
        }
    }
	
    Map<Id, Account> shocksList = new Map<Id, Account>([SELECT Id,Youngest__c, oldest__c from Account where Id IN :act]);
	
	List<AggregateResult> arResults = [select AccountId aid, max(Age__c)max,min(Age__c)min from Contact where AccountId IN :act group by AccountId];
	
	

	
	for(AggregateResult ar : arYoungest){
    
	string accid = (string)ar.get('aid');
	//Testing purpose i have added debugs ,please remove it if works fine.
    
	system.debug('==accid=='+accid);
    system.debug('==accid=='+(decimal)ar.get('min'));
    system.debug('==accid=='+(decimal)ar.get('max'));
        
	shocksList.get(accid).Youngest__c = (decimal)ar.get('min');
	shocksList.get(accid).oldest__c = (decimal)ar.get('max');
	}
	
	Update shocksList.values(); 
}

Note: Add undelete also so it will work for undelete records aswell.

trigger Surajtrg on Contact (after insert, after update, after delete , after undelete)
 

  if(trigger.isInsert ||trigger.isUpdate || trigger.isUndelete)



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 SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1