Record Naming Convention Trigger in Salesforce

Trey AroseSalesforce, Technical TipsLeave a Comment

Record Naming Convention Trigger in Salesforce

Out of the box Salesforce functionality does not allow for a naming convention on the Opportunity object. When creating an Opportunity, the user must enter a name for that Opportunity. If your business process requirements demand a set naming convention, asking the user to input a specific name every time is tedious and unreliable. In this post, we are going to show you how to implement a system generated naming convention, using fields found on the Opportunity object.

You will need to agree upon which fields will comprise your naming convention based on your business process. Once you know which fields to incorporate, you can begin structuring the trigger. In this example, we are going to use Field1 and Field2.

You want to begin by setting your trigger to fire after insert, so that values in the selected fields are populated and available for use.

Next you’ll need to query the Opportunity Object for the created record. You can do so by using the following line, utilizing the syntax Trigger.newMap.keySet():

List<Opportunity> olis = [SELECT Id, Field1, Field2 FROM Opportunity WHERE Id IN: Trigger.newMap.keySet()];

This will return our two field values for the naming convention. Now it’s time to set the Opportunity Name and Nickname for the selected Opportunity. You will need to encompass the logic in a for loop, to handle the values stored in the olis list. Here we are taking the user inputted value of Opportunity Name and moving it to a custom Nickname__c field. After moving the value, we then create the new Opportunity Name by combining Field1 and Field2. You will then need to finish up by updating the record, finalizing the changes.

for (Opportunity o :olis){

o.Nickname__c = o.Name;

o.Name = o.Field1 + o.Field2;


update olis;

If you are interested in Salesforce or need help, we would love to talk with you about your needs and challenges, contact us today.

Leave a Reply

Your email address will not be published. Required fields are marked *