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
WheelerForceWheelerForce 

Opportunity Naming Convention

Does anyone have the code I need to create an opportunity naming convention?

I would like the opportunity name to be populated at creation by "Account Name, Business Segment, Closed date".  Business segment is a field on the opportunity.  example "Acme Products Engine Sales 29/10/2015"

Your help is appreciated. 
Vishal Negandhi 16Vishal Negandhi 16
Write a trigger on before insert of Opportunity records..

Code below:
 
trigger OpportunityTrigger on Opportunity(before insert){
	Set<Id> AccountIds = new Set<Id>();
	Map<Id, String> AccountMap = new Map<Id, String>();
	for(Opportunity opp : trigger.new){
		AccountIds.add(opp.AccountId);
	}
	
	for(Account acc : [Select Id, Name From Account WHERE Id IN :AccountIds]){
		AccountMap.put(acc.Id, acc.Name);
	}
	
	String oppName = '';
	for(Opportunity opp : trigger.new){
		oppName = AccountMap.get(opp.AccountId) + ' ' + opp.Business_Segment__c + ' ' + opp.CloseDate.format();
		opp.Name = oppName;
	}
}
Please note, based on this trigger whenever user creates a new opportunity, the name entered by them will be over-ridden by the name set in this trigger. 
 
WheelerForceWheelerForce
Thanks Vishal

I will give it a go.