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
Trinityed78Trinityed78 

Help / Pointers Needed With Apex Trigger Development

Hello all! 

 

I've been trying to create a workflow rule, to add two fields together to create a new date value (in a separate custom field. 

 

So here's the background:

 

Field Names:

 

Deal End - a date field which notes when our product will end its run on our client's website. 

 

Number of Days - a formula field that looks to the contract to calculate the number of days in the contract life span in months. For example - 3 = 92 days

 

Date To Contact - a formula date field which adds Deal End + Number of Days to give us the new date after the product last featured. 

 

 

What I tried:

 

Initially it was all working well, until I discovered that the contract life span could be amended at the last minute (from 3 to 4 months - for example). But while the Number of Days changed, the Date To Contact value remained the same as the previous value. It would only changed when someone edited the record. So unfortunately workflow rules won't work here (or so I was told by Salesforce.com.

 

 

HELP??

 

Could an apex trigger, in effect, add the Deal End and the Number of Days field to produce a self-updating date value, without the user having to edit the custom object record? If so, what could this trigger look like?

 

 

 

Hope someone can help??

 

 

Ed.

 

 


Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Ah, sorry, misread your original post.

 

Yes, formula fields should update automatically when the underlying field is changed.

 

Just to confirm, the Date to Contact is a formula field, rather than a field set by a formula from a workflow?

 

All Answers

bob_buzzardbob_buzzard

A trigger couldn't be used in the way that you describe, as like workflows they only fire when a record is updated (or deleted, inserted etc).  What you are describing is a formula field.  I'm assuming that you've been down that route and can't use a formula field for this.

 

In this case you would need to create a trigger on the contract object so that when the lifespan changed, it located all records where the Deal End date was based on this and updated the Deal End date accordingly.

Trinityed78Trinityed78

Hi Bob,

 

Thanks for the reply. The deal end date works totally independent of the contract, as sometimes our products may appear for a month, but our contract with our client may be 1 year, which means that we can feature every other month. 

 

The Date to Contact field is already a formula field, which should update itself automatically, but it fails to do so. 

 

The formula is Deal End + Number of Days

 

When the number of days changes then the Date to Contact should also refresh, shouldn't it? 

 

 

goabhigogoabhigo

You can do it with workflow itself, I am not sure.  Give this a try:

Date to contract should be a date field.

 

  • Evaluation criteria: Every time a record is created or edited
  • Rule criteria: formula evaluates to true
  • ISCHANGED( Number_of_days__c)
  • Save
  • Update field : Date to Contract - Deal End + Number_of_days__c
Does this help you??

 

If not trigger is the only way.

Trinityed78Trinityed78

@ abhi_the1

 

Thanks for your reply. Yes I did try this method, but salesforce.com told me that the workflow won't kick in, because it required someone to physically edit and save the record for the workflow to work. 

 

 

I'm a bit puzzled :(

bob_buzzardbob_buzzard

Ah, sorry, misread your original post.

 

Yes, formula fields should update automatically when the underlying field is changed.

 

Just to confirm, the Date to Contact is a formula field, rather than a field set by a formula from a workflow?

 

This was selected as the best answer
Trinityed78Trinityed78

Sorry, I've made a complete school-boy error. 

 

I created a blank date field, and a formula workflow to help populate it, but I can't believe I didn't think to make a date formula field with the calculated value! 

 

I guess I thought that if it was calculated then it couldn't be used in the time frame filter in reports, but of course it can. 

 

 

Thanks Bob, you made me re-think my logic! :)

 

 

As for Salesforce.com, I can't believe they didn't recommend that I create this field, instead of a workflow. 

 

Oh well...

goabhigogoabhigo

Hey its working in my developer org. I just created some fields and tested !!!

May be you have not filled the date field ;)

 

goabhigogoabhigo

Fine then ;)