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
Vidya H 4Vidya H 4 

please help me to write code for this

I have Created a field on Account called Stage with values - In Progress, Closed. If the stage is changed to closed the trigger should auto update stage of all opportunities for that account to closed/won and close date to current day.
Best Answer chosen by Vidya H 4
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you try the below trigger on Account Object.
 
trigger AccountStage on Account (After Update) {
    
    Set<Id> accountIds = new Set<Id>();

    for(Account ac : Trigger.new)
    {
         if(ac.Stage__c =='Closed')
              accountIds.add(ac.Id);
    }

     List<Opportunity> oppsToUpdate = new List<Opportunity>();

     for(Opportunity opp : [select id, StageName from Opportunity where AccountId in: accountIds])
     {
          opp.StageName='Closed - won';
          oppsToUpdate.add(opp);
     }

     update oppsToUpdate;

}

If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you try the below trigger on Account Object.
 
trigger AccountStage on Account (After Update) {
    
    Set<Id> accountIds = new Set<Id>();

    for(Account ac : Trigger.new)
    {
         if(ac.Stage__c =='Closed')
              accountIds.add(ac.Id);
    }

     List<Opportunity> oppsToUpdate = new List<Opportunity>();

     for(Opportunity opp : [select id, StageName from Opportunity where AccountId in: accountIds])
     {
          opp.StageName='Closed - won';
          oppsToUpdate.add(opp);
     }

     update oppsToUpdate;

}

If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Did the above trigger logic worked? If so please mark it as best answer.

Thanks,
 
Vidya H 4Vidya H 4
@Sai Praveen  
its working fine thank you
Vidya H 4Vidya H 4
@Sai Praveen 
could you please help me to write batch class for this
I have Created a field called Funding on Quote with values - Partial , Full and NA .
and i Created a field called Funding on Opportunity.
Write a batch class to rollup the unique funding field values from Quote to Opportunity if Opportunity stage is closed/won. Ex: if an opportunity has 6 quotes with funding Full, Full, NA, Partial ,NA, Partial then field on opportunity should be in format - Partial,Full,NA.