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
RaoSRaoS 

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

Hi,
I need to calculate number of days an opportunity is in a particular stage. Once the stage is changed this field should reset to zero.

I have created a custom number field and the following trigger to do the same.

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 am getting an error "Illegal assignment from Decimal to Integer" at the line which is in bold.  

Instead I tried to do 
Trigger.old[i].No_of_Days_in_this_Stage__c = Trigger.old[i].No_of_Days_in_this_Stage__c + 1;

Then it is throwing error saying "Expression cannot be assigned".

Let me know where I am going wrong.

Thanks
Rao
Best Answer chosen by RaoS
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

You can use workflow or process builder instead of a trigger. Please refer to the below link which might help you further with the above requirement.

https://success.salesforce.com/answers?id=90630000000hd7zAAA

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas