Skip to content

August 4, 2015

1

Salesforce: Workaround to Assign Asset Name with Auto Increment Unique Value

by Joe Kuan

In Salesforce, when creating an Asset entry, the Name field is a mandatory text field. For some users, this field has no particular purpose but cannot be left empty. For others, they may want to set the field with unique incremental id. In order to assign the Name field with an auto incremental value, a workaround solution is to first create a custom numeric field, (e.g.) Auto Update Id, with auto increment property.

screen

Next step is to create a trigger which sets the Name field to the Auto_Update_Id value. Goes to Customize -> Assets -> Triggers and creates a trigger with the following simple code:

trigger SetNameToAutoIncrId on Asset (after insert) {

    for(Asset a: trigger.new){
       for (Asset ua: [ Select Name, Auto_Update_Id__c FROM Asset where Id = :a.id ]) {
           ua.Name = ua.Auto_Update_Id__c;
           update ua;
       }
    }
}

Since the auto increment value won’t be assigned until an Asset record is created, therefore we have to create a trigger with after insert event. Also Salesforce does not allow users to modify the triggered record in after insert event. So that we need to issue another SOQL query, assign the result to another Asset object and then update the record to the DB.

Although the user still needs to input something into the mandatory Name field, the insert operation will automatically overwrite the Name field with an unique numerical id.
 

Advertisements
1 Comment Post a comment
  1. Jasonlhy
    Apr 25 2017

    thanks, solved my problem

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: