i have a trigger that will update a custom date field in my case but it seems like my calculation is wrong.

This is my trigger:

c.SLA_Cont_Time__c = System.now() - c.SLA_Days_mins__c;

c.SLA_Cont_Time__c is my custom date/time field and c.SLA_Days_mins__c is a numeric field that i need to minus it to get latest value. but my c.SLA_Cont_Time__c keep getting the System.now() value. why it will not take the after subtraction value?

  • Please try to find specific tags and avoid using very general tags such as 'salesforce-crm'. This helps you attract the right people to your question. – Samuel De Rycke Jan 30 '14 at 20:47

Try taking a look at the date instance methods.

You'll find a method called addDays that'll do the trick. Also, System.now() returns a datetime rather than a date, so if you want today's date, use date.today().

c.SLA_Cont_Time__c = date.today().addDays(-1*c.SLA_Days_mins__c);

// I multiplied c.SLA_Days_mins__c by -1 before so we subtract days

For reference:

  • Actually i need this field for my entitlement. so i need it as a date time value as my entitlement count against my business hour. c.SLA_Days_mins__c actually is a minute that i calculate from 2 date field. I wanted to know how many mins used for my entitlement milestone then continue from there. That's why i need to minus c.SLA_Days_mins__c – user2074703 Feb 1 '14 at 10:24


c.SLA_Cont_Time__c = (System.now().getTime() - c.SLA_Days_mins__c.getTime())/1000;

Because getTime() :

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this DateTime object.

but why do you need Data/Time field? Did you considered use formula?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.