+ Start a Discussion

Need Trigger Update

Requirement : 


I need to calculate total number of days to close for the each opportunities.


Criteria - 


I have a custom field Days to close


Whenever a opportunity record reaches stagename = closed won or lost


-  it should update the field Days to close by calculating the total number of days between the record created and stagename = closed won or lost


Sorry if it is confusing, as a newbie i cant explain clearly , still if anybody knows the situation pls help me to create the trigger to update.


Thanks in advance


You can just use a formula field to do this for you....



   Type: Number

   Formula: CloseDate - CreatedDate





   Type: Text

   Formula: TEXT(CloseDate - CreatedDate) & ' days to close'

thanks for the response , good solution too :-)

but it did not meet my requirement I guess, y because is I need to calculate exactly the record saved date ie stage = lost or won.

close date is just approximate date for opportunity which will be entered during each opportunity creation .

close date and stage fields are not identical , have no relation too if im not wrong .

welcome anymore suggestion .

Well if that's the case when you loop through your results inside your trigger it should work out the same way...


// for this senerio I'm assuming your Opportunity Close Date is called CloseDateDuration
for(Opportunity opp :trigger.new){
// here we are checking to see if the Opportunity
// is closed and the value has yet to be populated
// so it does not get over-written. if( opp.IsClosed && null != opp.CloseDateDuration__c && '' != opp.CloseDateDuration ){ opp.CloseDateDuration__c = System.Today() - CreatedDate; } }


sorry again, its not working for me,

I have created workflow for this ,
opp.stage = closed won or
opp.stage = closed lost

field to update - days to close
formula = today - datevalue(createdDate)

its working fine , whenever I update stage field days to close counts the days ;-)

the pbm is its useful for new records only , but I have 2000 old records if I edit old records it counts till today :-(

thank god I got solution after surfing a lot,

there s standard report type called opp field history , it has some fields like from stage and to stage for tracking purposes , awesome options , reduced lot of pains and saves me time :-)

thanks Alex for help :-)