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

Set Opportunity CloseDate field with trigger


I am trying to somehow default the 'CloseDate' field in the Opportunity as the users have absolutely no need for the field. Basically, I want to remove the requirement that the user must put something in this field.

I have tried everything I can think of using workflow rules, etc. and have even written a 'before' trigger to give the field a value so that the record will save. This does not appear to work, and we still get the 'Invalid Data' error since a required field is not being filled in.

Is this field somehow special in that I cannot even get to it with a before trigger? Is there ANY way to hardcode/default this field?

Before triggers do not run until after the user clicks save and because closedate is a system required field on opportunity there is no way to get by it without providing a value. Further, because it is a standard field there are no default value formula options.

You could create a Visualforce page for creating opportunities, override the new button/link on opportunity and with a little Apex code behind the page you can default the value.
thanks for the post Andrew.  i was afraid someone was going to say something like this. 

I do realize that there is a VisualForce answer to this.  However, It seems like an awful lot of effort to simply put a value in a field, really.    For my own technical curiosity, what is the reason for the validation rules/required fields being executed before the 'before' triggers? Isn't one of the goals of the 'before' triggers to allow pre-processing of a record before it is committed to the data store? (I admit some naievete here)  The additionally frustrating part is that I have no other options: I cannot even change the field label (which could also be made to support the user's requests)

To your VisualForce point - I do understand the solution that you metioned, but is there any way to shortcut the creation of the VF page by referencing some 'new opportunity page' template?  That is, can I create a VF page against the Standard Opportunity controller and somehow have all the default 'new opportunity'  fields and layout just appear, or does it have to be rebuilt from scratch?

Final question, is there any way to override the 'new' button to simply use the 'New Opportunity' URL with a passed in URL parameter for the CreateDate field??

thanks much for all the information