[SalesForce] Write to an autonumber field or not require entry of text name/id field

So I had a Name field with text and had populated it with Account Names. Upon converting back to an autonumber, all of the existing data had stayed the same and all new records received the autonumber.

Is it possible for me to change the datatype in Apex to text, then on-save trigger, to update the field, then change back to an autonumber? I like the Autonumber in that the user doesn't need to fill in random text on entry HOWEVER, I like how meaning full the text is.

So the process would go as follows:

  1. Object has autonumber
  2. user creates record for object
  3. entry form opens up and the user fills it out
  4. a trigger is fired on save which turns the autonumber to a text, the trigger then changes the text of the auto number field to the text ([date] + ': ' + Owner.name)
    5) the autonumber is turned back to an autonumber

The reason I think this is possible is because I had a name field that was updated via trigger. Then, at some point, I needed to change the field to an autonumber. After changing it to an auto number all of the existing records kept their old value (date + ': ' + owner.name)

So I was hoping to have a more desctiptive field by using text for the key BUT I don't want to force the user to enter a "token" value just to save the record to have the trigger overwrite it.

Example for how it works now with a name field:

  1. user opens record and fills in a date field (which is the only thing I'm interested in)
  2. The user has to fill in a name field which is unnecessary (for the user, in a mobile setting this is problematic or at least not elegant)
  3. User saves
  4. name field is overwritten (date + ': ' + owner.name)

Example for how it works with autonumber field:

  1. user opens record and fills out date
  2. user saves (but now autonumber is not useful in Salesforce1) and list views

How I'd like it to work

  1. user opens record and fills out date (and does not have to fill out a name field)
  2. user saves
  3. trigger updates "key" field to (date + ': ' + owner.name)

perhaps a quick action can do this? However, this is a master object therefore, I'd have to trigger the action via a tab. Perhaps this could be done with a flow? I'm not sure how they can look in lightning

Otherwise, I was thinking this was possible because I had text with all the key fields filled in. Then changed to autonumber and the old key fields stayed and only new records had the autonumber.

So i'm looking for the best of both worlds

Best Answer

If you used a custom setting of type list to hold the value of your autonumber field as it's being generated between trigger executions, you could use a trigger to sequentially generate new numbers and insert them into the text field as new records are created, then update the custom setting at the end of the trigger with the newest value of the current "count" of records.

The caveat here being that if another instance of the same trigger is called while one is still executing, you could wind up with two records (or a lot of records actually) that have the same autonumber value. You'd want to have a test to check to see if another trigger instance was executing. If it was, you'd need to add logic to handle that. Off hand, I'm not certain how you'd want to handle that situation. I'd need to think about it. It would greatly depend on what kind of trigger platform, if any, that you were using.

Edit

Obviously, you can't be changing your field types back and forth between Autonumber and text. What you can do however is keep your field as a text field and format it using a trigger or workflow to appear however you'd like as it sounds as though the auto-number isn't of importance to you.

Since the Name field is required and you're using a Lightning component, it wouldn't seem to be that difficult to grab the $User.Name (assuming the user who does the save is the owner) along with the date either from a Global Variable or JS and concatenate them together into a string to populate the Name field with during your Save action.

You could presumably also do that by populating the Name field with a static string in your component and then have workflow update the Name field for you following the Save; saving yourself the need for a trigger to determine the correct value for [date] + ': ' + Owner.name.

Related Topic