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
chandrashekar Jangitichandrashekar Jangiti 

Hi Folks..Need help on trigger on Opportunity Object

"for Opportunity Stage changes to 'Closed Won' today then if we are trying to update today's date(if Closed date selected apart from today's date) that time do not take today's date in closed date filed and should take prior date value in Closed date".


Thanks,
Chandu
Malika Pathak 9Malika Pathak 9

Hi Chandu,

Please find solution.  .Need help on trigger on Opportunity Object

trigger OpportunityTrigger on Opportunity (before update) {
    
    
    if(trigger.isBefore && Trigger.isUpdate){
        for(opportunity op:trigger.new){
            if(op.StageName=='Closed Won'){
                if(op.closeDate==Date.today() || op.CloseDate>Date.today()){
                    op.closeDate.addError('Enter prior date of today');
                }
            }
            
        }
    }
}

Please let me know it is working or not.

If it is working please mark best answer so that other people would take reference from it.

Thanks

chandrashekar Jangitichandrashekar Jangiti
Hi Malika thanks for quick response. But here I don’t want to validate and display any error, I want to update prior date.

thanks 
Chandu
chandrashekar Jangitichandrashekar Jangiti
Just should not take today’s date if Stage changes to Clised Won today 
Malika Pathak 9Malika Pathak 9

Hi Chandu,

How is this?

trigger OpportunityTrigger on Opportunity (after undelete, before delete,before update) {
    
    
    if(trigger.isBefore && Trigger.isUpdate){
        for(opportunity op:trigger.new){
            if(op.StageName=='Closed Won'){
                op.CloseDate=Date.today()-1;
            }
            
        }
    }
chandrashekar Jangitichandrashekar Jangiti
Hi Malika , again thanks for reply..
But  in this code "op.CloseDate=Date.today()-1;" it is not updating any other date and it is taking prior to today date . My scenario is it should not take today's date and it should update with any other date if we select other than todays date, when we select todays date then only it should update with which  date we has already in closed date value that we are considering as prior date not yesterday's date..

high level logic:

"Rule Criteria: (Opportunity: StageEQUALSClosed Won) AND (Opportunity: Close Date EQUALS TODAY) 
Description: Salesforce defaults the "closed date" to today when an opportunity is
                         closed/won. We use the field as effective date and don't want it changed


Thanks,
Chandu