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
DeveloperDeveloper 

adding name prefix through trigger

1. At asset creation, it should take the following value: product.pen ++ "_" ++ Account.Name (truncated to max 80 characters) ++ "_" ++ Creation Date
2.This is a standard field and should be read-only.

Could you please help me with this 
Best Answer chosen by Developer
Adilson Arcoverde JrAdilson Arcoverde Jr
Hi,

The best approach is to use a trigger on Opportunity before insert, because Name field will be editable. Try this code:
trigger AssetTrigger on Asset (before insert) {
	if(Trigger.isBefore && Trigger.isInsert) {
		Set<String> productsIds = new Set<String>();
		Set<String> accountsIds = new Set<String>();
		for( Asset newAsset : Trigger.new ) {
			productsIds.add( newAsset.Product2Id );
			accountsIds.add( newAsset.AccountId ); 
		}
		
		Map<String,Product2> productsMap = new Map<String,Product2>( [Select Id, Pen__c from Product2 where Id in : productsIds] );
		Map<String,Account> accountsMap = new Map<String,Account>( [Select Id, Name from Account where Id in :accountsIds] );
		
		for( Asset newAsset : Trigger.new ) {
			newAsset.Name = productsMap.get( newAsset.Product2Id ).Pen__c + '-' + productsMap.get( newAsset.AccountId ).Name.left(80) + '-' + Datetime.now(); 
		}
		
	}
}

I hope you find this solution helpful. If it does, please mark as Best Answer to help others too.

Best regards.