Monday, December 13, 2010

Enforcing Record Name Convention in Salesforce without Apex

One can enforce a record naming convention with a few steps in Salesforce, sans Apex!
  1. Create a formula field called Expected Name with a formula that evaluates to the name you want to see.
  2. Create a validation rule called "Name Expected" with error formula: Name != Expected_Name__c

Enforcing Record Name Uniqueness in Salesforce without Apex

I discovered today that it is possible to enforce record name uniqueness in Salesforce without using any Apex code!

All that is needed is a simple validation rule with the error formula set to the following:
Name = VLOOKUP( $ObjectType.Object.Fields.Name , $ObjectType.Object.Fields.Name , Name ) && Id <> VLOOKUP( $ObjectType.Object.Fields.Id , $ObjectType.Object.Fields.Name , Name )

However, there doesn't seem to be an easy to to perform a case-sensitive comparison in the formula builder. Another thought would be to do the following to enforce name uniqueness:
  1. Create a text field called Unique Name on the object, and add the case-sensitive uniqueness constraint.
  2. Create a workflow field update called Unique Name that just sets Unique_Name__c to Name.
  3. Create a workflow rule called Set Unique Name that's triggered every time a record is created or updated.
  4. Activate the workflow rule.

Last edited on 8/10/2011.

Friday, December 10, 2010

Field Update in Before Trigger Propagates to After Trigger

I confirmed a simple fact in version 20.0 Apex triggers today, that will guide me when writing future trigger code.

If a lookup field is not specified in the DML operation and will not be populated via any other non-trigger means, then a System.debug(Trigger.new); will show that the trigger does not have access to the lookup field value.

However, if the lookup field is populated or updated in the before trigger, then the after trigger will be able to access the new value!

For example, I have a Campaign Feed object that has a Lookup(Campaign) field with an API name of Campaign__c. In my before insert trigger on Campaign_Feed__c, I set Campaign__c to the Id of a newly created Campaign. As a result I am able to then use Campaign__c in my after insert trigger, even though I didn't initially specify a Campaign__c value when inserting the Campaign Feed record.