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
SunnyShinySunnyShiny 

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@Salesforcesandeep@Salesforce

One thing is left. 

 

Yes this also will give updated values in reports

All Answers

Suresh RaghuramSuresh 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. 

SunnyShinySunnyShiny

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

sandeep@Salesforcesandeep@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.

SunnyShinySunnyShiny

yes difference between created date and today.

SunnyShinySunnyShiny

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

damienowendamienowen

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@Salesforcesandeep@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@Salesforcesandeep@Salesforce

One thing is left. 

 

Yes this also will give updated values in reports

This was selected as the best answer