3

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

  • No thats not posaible using native solution. – Tushar Sharma May 24 '16 at 15:54
  • 1
    Are you asking if you can use a trigger to generate and maintain a "sequential autonumber" that's populated in a text field for new records or are you wanting to somehow update all the old records to have sequential autonumbers, before converting to autonumber? – crmprogdev May 24 '16 at 16:05
  • @crmprogdev yes, that's the field. It's more readable on the list view for clicking as a name field but it requires entry. I'd like the best of both worlds: autonumber on entry, trigger to make it readable for lists. – tjcinnamon May 24 '16 at 16:07
  • I can say, I was able to spoof it using a quick action in Salesforce1. However, that does not help desktop entry. – tjcinnamon May 24 '16 at 20:29
3

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.

| improve this answer | |
  • Couldn't you use a hierarchy setting to maintain the highest number yet posted and just bump it every time? – Adrian Larson May 24 '16 at 16:34
  • The issue is, if I have a name field then the user has to fill something out. I'm also building everything in lightning and keeping everything on the client side "in the box". Otherwise, I'd vForce putting a default value in there then use a trigger to update before-insert/before-update – tjcinnamon May 24 '16 at 16:47
  • @AdrianLarson You could do it with either one. Hierarchy custom settings are best suited for use with profiles of users. One can just as easily move a value from one field in a list to another field in the same custom setting list, so I don't see any advantage to using a hierarchy. On the name issue tjcinnamon, I'd think your lightning client side js controller should be able to populate a random or static default name. – crmprogdev May 24 '16 at 17:00
  • Are you guys talking about re-creating the autonumber function in a name field? Or are you talking about treating the autonumber field like the name field? – tjcinnamon May 24 '16 at 17:16
  • 1
    Is quite different than what you initially seemed to be asking. Have edited my answer. – crmprogdev May 24 '16 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.