+ Start a Discussion

Populate an empty required field before validation rules

Hey all,
Here's my problem :
In my Custom Object Customer__c, I would like to populate my Record Name with 2 others fields. This works fine with a Trigger, but only with updates. When I try to create a new record, it tells me that the Record Name is a required fields. I guess that validation rules come before trigger actions.
Does anyone can explain me how to make it ?
Thx a lot,
You must have an "After Insert" trigger implemented. If that's the case, all validation rules are executed prior to the after triggers. Since you want to pre-populate a required field when a new record is created, change the trigger to an "Before Insert" trigger and it should execute prior the system and user generated validation rules.
I'm using a Before Insert trigger, here's the code :
trigger updateCustomerRecordName on Customer__c (before insert, before update) {
 Customer__c[] c = trigger.new;
 c[0].Name = c[0].First_Name__c + ' ' + c[0].Last_Name__c;


Maybe i'm doing wrong ?

Message Edited by yao on 09-18-2008 08:06 AM
I'm running into a similar problem with the before trigger not being fired before the validation is occurring.

trigger populateCompanyName on Lead (before insert, before update) {
for (Lead lead : System.Trigger.new) {
Datetime today = datetime.now();
String name = Lead.LastName.length() < 4? Lead.LastName : Lead.LastName.substring(0,4);
Lead.Company = name.toUpperCase() + '-' + today.format('yyyyMMdd');

Gotcha. I made the assumption that you'd have the user still enter an initial value for that required field, but I see that's not what you want. In that case, you could write either a small s-control or VisualForce page that assigns a static (initial) value to that required field as new records are created. There must be a cleanner and more elegant way to achieve what you're trying to do, but I'm not aware of :)

This is how I would do it: I'd have either an s-control or VisualForce page that overwrittes the standard "New" button for your custom object, and in that code, assign an initial value to your "Name" required field. Then simply use your Apex trigger to change that required field to the values that were assigned to the First and Last Name fields within that record (you already have this built). If you decide that you want to go this route, here's a snippet of what the s-control would look like:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <script type="text/javascript" src="/js/functions.js"></script>
    <script src="/soap/ajax/12.0/connection.js"></script>
      function loadCustomObj() {
     var myURL = parent.frames.location.href;
     parent.frames.location.replace(myURL + '&tsk5=This will auto-populate&nooverride=1');                      
  <body onload="loadCustomObj()">

Again, since you're extending the standard New button functionality of your object, you'll need to overwritte it and then associate it with this s-control. All the above code does is to assign the value of the field variable named tsk5 to "This will auto-populate". You want to change tsk5 to whatever variable name is mapped to your required field for the Name. When the user clicks on the New button to create a new Customer record, they should see that static string auto-populated to your required field. Then they would enter the first and last names, and once the record is saved, your trigger should fire and change the Name field to First + Last Name. 

Message Edited by HL-Dev on 09-19-2008 02:29 PM
Thx a lot, it works fine ;)

Message Edited by yao on 09-22-2008 04:29 AM