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
Suneel B 9Suneel B 9 

Need After delete to update the last created record in custom field

Hai guys i have created a custom field in account which shows the latest created opportinity in it. Now if i created a record s1 first and s2 next where s2 is the latest created record and it will display s2 in the custom field. Now if i delete s2 the last created would be s1 and s1 has to be displayed.
Now below is the program with after insert and after update i need after delete.
Can anyone help me out with this,
trigger OppUp on Opportunity (after insert,after update, after delete) 
{
  Set<Id> SetId = new set<Id>();
  map<id, Account> acmap = new map<id, Account>();
  for(Opportunity Opp: trigger.new)
  {
  SetId.add(Opp.Accountid);
  }
  
  List<Account> opplist = [SELECT id, OptyName__c,(Select id,Opportunity.Name,Opportunity.CreatedDate,Opportunity.AccountId FROM Account.Opportunities order by createddate) FROM Account where Id IN :SetId];
  
  for(Account Acc: opplist)
  {
     for(Opportunity opp: acc.opportunities)
     {
        acc.OptyName__c = opp.name;
        acmap.put(acc.id, acc);
     }
  }
   for(Opportunity Opp: trigger.old)
    {
    
 }

  update acmap.values();
 }
Best Answer chosen by Suneel B 9
Neetu_BansalNeetu_Bansal
Hi Suneel,

Use this code:
trigger OppUp on Opportunity (after insert,after update, after delete)
{
	Set<Id> SetId = new set<Id>();
	Map<id, Account> acmap = new Map<id, Account>();
	
	if( trigger.isInsert || trigger.isUpdate )
	{
		for( Opportunity Opp : trigger.new )
		{
			SetId.add( Opp.Accountid );
		}
	}
	
	if( trigger.isDelete || trigger.isUpdate )
	{
		for( Opportunity Opp : trigger.old )
		{
			SetId.add( Opp.Accountid );
		}
	}
  
	List<Account> opplist = [ SELECT id, OptyName__c, ( Select id,Opportunity.Name, Opportunity.CreatedDate, Opportunity.AccountId FROM Account.Opportunities order by createdDate DESC ) FROM Account where Id IN :SetId ];
  
	for(Account Acc: opplist)
	{
		if( acc.opportunities.size() > 0 )
		{
			acc.OptyName__c = acc.opportunities[0].Name;
		}
		else
		{
			acc.OptyName__c = '';
		}
		acmap.put(acc.id, acc);
	}

	if( acmap.size() > 0 )
		update acmap.values();
}
If this post helps, mark this as best answer so will help others in future.

Thanks,
Neetu
 

All Answers

Neetu_BansalNeetu_Bansal
Hi Suneel,

Use this code:
trigger OppUp on Opportunity (after insert,after update, after delete)
{
	Set<Id> SetId = new set<Id>();
	Map<id, Account> acmap = new Map<id, Account>();
	
	if( trigger.isInsert || trigger.isUpdate )
	{
		for( Opportunity Opp : trigger.new )
		{
			SetId.add( Opp.Accountid );
		}
	}
	
	if( trigger.isDelete || trigger.isUpdate )
	{
		for( Opportunity Opp : trigger.old )
		{
			SetId.add( Opp.Accountid );
		}
	}
  
	List<Account> opplist = [ SELECT id, OptyName__c, ( Select id,Opportunity.Name, Opportunity.CreatedDate, Opportunity.AccountId FROM Account.Opportunities order by createdDate DESC ) FROM Account where Id IN :SetId ];
  
	for(Account Acc: opplist)
	{
		if( acc.opportunities.size() > 0 )
		{
			acc.OptyName__c = acc.opportunities[0].Name;
		}
		else
		{
			acc.OptyName__c = '';
		}
		acmap.put(acc.id, acc);
	}

	if( acmap.size() > 0 )
		update acmap.values();
}
If this post helps, mark this as best answer so will help others in future.

Thanks,
Neetu
 
This was selected as the best answer
Suneel B 9Suneel B 9
Thank You Neetu Bansal.