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

Does a Before Insert trigger work to populate required fields?

It would seem that the answer is no - though that doesn't make sense.

Basically, I am trying to write a before insert trigger to populate the Name (official) field of a custom object.  I have read a number of messages that are very close to this one - but not quite. Some folks recommend creating a VisualForce page, but I am trying to do it on a standard Salesforce form.

When I enter a new record, I get a message back saying "Invalid Data", "You Must Enter a Value". Now, this trigger works fine if I have it autopopulate a different, non-required field, it's fine. However, if I try to use it with the object's Name field or even another field that I have set to required, it fails. Does this sort of process not work , or am I mssing something?

trigger updateEnrollmentRecordName on Enrollment__c (before insert, after update) {
    List<Enrollment__c> updatedEnrollment = new List<Enrollment__c>();
    for (Enrollment__c e :{
        e.Name = e.Enrollee_Name_Text__c + ' - ' + e.Program_Name__c + ' ' + e.Cycle__c + ' ' + e.Cycle_Year__c;

Many thanks.

Message Edited by ksnyder on 10-02-2008 03:07 PM
ksnyder, do you mean the fields of the object in the database or of the web form on the screen?  The former should work fine, for Name or anything else.  The latter won't.  You could get around that in a few ways (like VisualForce, as you mention) although since it's a custom object, could you make Name an auto-number field instead of a string?  Then it will effectively become non-required.

Thank you for responding.

The fields are displayed on the web form [Ah-hah!  If I removed the field from the form and had it trigger quietly in the background, would it work? - I will try that and post the results].

It isn't crucial that the Name field be populated on the form as the user is entering the record. It's more for later on, when they need to select the record from various list views.

The main reason I am trying to figure this out is so that I can make the Name fields more informative for users. I had originally set it as an autonumber, but having the contact name and some enrollment info in the field would be more friendly for the end user.

Thanks again.
I think that "auto-number" is something that's only enforced at the web UI level, so you might be able to get away with still writing an arbitrary string in your before insert.

You can definitely do an insert quietly at the beginning and then immediately redirect to the standard edit page of the new record (which will have the values you wrote).  It breaks workflow and stuff for that object, but if it's just your custom object, you're fine.

See if you get any better answers also!  It seems like with a custom object, there may be a way to set a default or just hide it using FLS or remove Name / Number from the page layout.  For some standard fields, you can't take them out of the page layout, but you can still use FLS to make it illegal for the user to see them, which avoids having it show up in the editor.
I thought I could get away with removing the Name field and having it all happen silently in the background - but it can't be removed.

When you say that you would insert quietly at the beginning and  then redirect to  the standard edit page, is this most effectively done via S-Controls or Visualforce pages?  I am leaning toward Visualforce pages.

Thanks .