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
Aaron RodgersAaron Rodgers 

How to update CloseDate on an oppertunity if StageName = "Closed Won"

Hi All,

Im new to Salesforce & Validation rules.
How do I go about creating a validation rule for an oppertunity so that it updates the CloseDate to Today when the StageName changes to "Closed Won"??
Best Answer chosen by Aaron Rodgers
preti tyagipreti tyagi
Hi,
    You can create a workflow rule or write a trigger on Opportunity.
Workflow rule:
1. Rule criteria "created, and any "time it’s edited to subsequently meet criteria" 
2. Give the rule Criteria  as StageName equals Closed Won
3.create a immediate action field update for CloseDate to Today
3.Activate the Rule

Trigger:
trigger OpportunitycloseddateTrigger on Opportunity (before Insert) {
for (Opportunity opp : trigger.new)
    if (opp.StageName== 'Closed Won')
        {
          opp.CloseDate= Date.today();
         }

All Answers

revathi krishnanrevathi krishnan
Hi,
You can't do this using Validation rule..

You can create a Workflow rule on Opportunity with these simple steps..

1. Rule criteria "created, and any "time it’s edited to subsequently meet criteria" 
2. Give the rule Criteria  as StageName equals Closed Won
3.create a immediate action field update for CloseDate
3.Activate the Rule
 
preti tyagipreti tyagi
Hi,
    You can create a workflow rule or write a trigger on Opportunity.
Workflow rule:
1. Rule criteria "created, and any "time it’s edited to subsequently meet criteria" 
2. Give the rule Criteria  as StageName equals Closed Won
3.create a immediate action field update for CloseDate to Today
3.Activate the Rule

Trigger:
trigger OpportunitycloseddateTrigger on Opportunity (before Insert) {
for (Opportunity opp : trigger.new)
    if (opp.StageName== 'Closed Won')
        {
          opp.CloseDate= Date.today();
         }
This was selected as the best answer
Sagar PareekSagar Pareek

trigger ClosedDateTrigger on Opportunity (before Insert) {
for (Opportunity opp : trigger.new)
    if (opp.StageName== 'Closed Won')
        {
          opp.CloseDate= Date.today();
         }
revathi krishnanrevathi krishnan
If it is possible to achive by Declarative method, then we should use that. if you write tirgger, you have to write test code to cover that and code is difficult to maintain/change and it counts to the number of lines allowed in your org