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.
ShowAll Questionssorted byDate Posted
SunnyShiny

# Caclulate a field everyday

Hello,

How can I do to calculate a field everyday with a calcul based on the today date?

Thanks

Best Answer chosen by Admin (Salesforce Developers)
sandeep@Salesforce

One thing is left.

Yes this also will give updated values in reports

### All Answers

Suresh Raghuram

where you want to do this  i mean in formula field. trigger or class

if formula field.

IF(DATE.NOW() > CreatedDate, Sum(a,b), 0);

I just said SUM for Understanding get right one over there.

If Trigger or Apex Class.

Date today = Date.Today();

Date cDate = Date.Value(CreatedDate);

Integer a,b,c;

IF(today.daysBetween(cDate) > 0){

c= a+b;

}

If this answers your question make this as a solution.

SunnyShiny

Ok for a formula field but will that be updated automatically everday?

sandeep@Salesforce

There is already one attribute given in formula field " TODAY() " so you can use it.  now please what exactly you need to show number of days between created Date and today or any thing else ?

Note: formula field always gets calculatted whenever it gets read/accessed by user so Dont worry about it.

SunnyShiny

yes difference between created date and today.

SunnyShiny

Thanks for your answer. Is it also updated when a user consult a report ?

damienowen

A formula field is not a stored value in the database. When ever a record is viewd or queried by any means the formula field is evaluated at run-time. So if you have a report that is auto emailed daily and the formula uses TODAY() then the value of TODAY() will always equate to the date when the report runs. It should give you what you want.

sandeep@Salesforce

Hi,

to calculate you should use below formula fiels( Text Type):

TEXT(FLOOR((NOW()-CreatedDate)))

Please let me know in case of any other issue

sandeep@Salesforce

One thing is left.

Yes this also will give updated values in reports

This was selected as the best answer