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
Arpit Gupta 62Arpit Gupta 62 

when a Account field Type is updated , then update Description from its related Opportunity i.e created date is oldest & if Description is not blank . update Acc Description from its oldest Opty's Description


when a Account field  Type is updated , then update Description from its related Opportunity i.e  created date is oldest  & if Description is not blank .

update Acc Description from its oldest  Opty's Description 
AFSAR KHAN 7AFSAR KHAN 7
Hi Aprit,

Please find the following code.

rigger AccountFieldTypeUpdate on Account (before Update)
{
    set<ID> account_Id=new set<ID>();
    List<Opportunity> opps = new List<Opportunity>();
for (Account MyAccount : trigger.new)
{
    if (MyAccount.Type != trigger.oldMap.get(MyAccount.Id).type)
    {
        account_Id.add(MyAccount.Id);
        }
}
   
    if(account_Id.size()>0)
    {
        list<Opportunity> Opportunity_list=new list<Opportunity>([ select id, Description from Opportunity where AccountId IN:account_Id order by CreatedDate DESC limit 1]);
    for (Account MyAccount1 : trigger.new)
{
                for(Opportunity opp1 : Opportunity_list)
                {
                    if(opp1.Description != null)
                    {
                        MyAccount1.Description = opp1.Description;
                    }
                }
            }    
    }
}

Note: The above code will not work on bulk updates.