+ Start a Discussion

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

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;
      Trigger.old[i].No_of_Days_in_this_Stage__c = j;



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.

Best Answer chosen by RaoS
Khan AnasKhan Anas (Salesforce Developers) 

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.


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