+ Start a Discussion
RaoSRaoS 

Formula to return number of days an opportunity is in a particular stage

Hi ,
I have a requirement to calculate number of days an opportunity is in particular stage.  IF the stage changes then the variable should reset to zero.  If it is in the same stage then it should increment. 

For this I have written the following trigger but it is giving me "Expression cannot be assigned" error.

trigger updateNoofDaysInOppStage on Opportunity (before insert) {
 integer i=0;
integer j;
    for(Opportunity p : Trigger.new)
    {
        if(Trigger.old[i].StageName != Trigger.new[i].StageName){

        Trigger.new[i].No_of_Days_in_this_Stage__c = 0;
    }
     else
     {
      j=Integer.ValueOf(Trigger.old[i].No_of_Days_in_this_Stage__c);
      j++;
      Trigger.old[i].No_of_Days_in_this_Stage__c = j;

      }
i=i+1;
    }

}

I tried different ways to overcome this but could not.

Let me know.

Thanks.

Rao
Ajay K DubediAjay K Dubedi
Hi,
You do not need to write the trigger for this requirement as you can achieve this through Workflow.

Follow this link:
https://help.salesforce.com/articleView?id=000213870&type=1

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi