I have changed a field from number
to auto-number
in an object which contains records. As expected, this field for the existing records lost their contents and are now blank. The business requires that this field should always have a value, even for the existing records. Cloning is not an option, because the existing records have a large number of related records from a large number of objects. I have tried clicking on Edit and Save on the records, but to no avail - the auto-number fields are still blank. How do I populate the auto-number field for these existing records, or how do I logically accommodate a workaround?
-
1Suman you will need to delete all the data from the table using data loader and load the data from beggining. Thats the only alternative i see– Mohith ShrivastavaMay 14, 2013 at 6:51
-
@MohithKumar, thank you for your suggestion! However, the big hiccup I see is the large number of related records from a large number of related objects. Using Data Loader on each of these related objects will be time consuming.– Shumon SahaMay 14, 2013 at 7:21
1 Answer
I'd say your initial field type was wrong :/ You wouldn't lose any data if it was a text field and not a numeric one.
Check https://help.salesforce.com/HTViewSolution?id=000005315 and notes on field conversion.
it is possible to convert a custom auto-number field to a text field, which may then be imported or mass updated as desired. The field may then be converted back to an auto-number.
...
you can safely convert a text custom field into an auto-number field without losing your data.
I did it in the past fairly easily:
- Announce to your users that at some day the field or whole object will be unavailable and that you'll block any inserts and edits.
- Protect your field - change the field level security to hide it, if it's on custom object and you're concerned about operations as a whole - flip the object to "In Development". Maybe even add a validation rule if
$User.Username != 'my username'
(I do that because I'm paranoid and we have batch jobs/data loads that create data... if it's ok to fail the job/ import, make a val. rule. Otherwise you'll have to check & fix any deltas that occured in the meantime). - Flip the field to text & perform the update (Data Loader? Excel connector?)
- Quick sanity check - make a SOQL / report
WHERE MyField__c = null
. - Flip the field back to autonumber setting starting number to "your max + 1".
- Create new record if you can do it in prod to verify all is OK, then delete it. If you want - reset the starting number again.
- Revert change from #2.
P.S. Another option would be to make a NEW field and tick the "Generate Auto Number for existing records" checkbox. But that'd mean you need to include / replace in all reports, report types, mail merge templates etc. that refer to old field + I have no idea how does it do that (sort by creation date?).
-
Wow, now that is one thoroughly detailed solution. No wonder you have reached 7,000! May 14, 2013 at 7:59
-
Thank you for the wonderful answer! In another similar scenario, additionally, the field is being referenced numerous times in Apex. Documentation says You cannot change the data type of a custom field if it is referenced in Apex. Without changing the numerous references in Apex, do you know of any workaround? Or is this new scenario a permanent roadblock? May 14, 2013 at 8:05
-
Thx, but in my opinion at least 4 guys here rock harder than me :) It's a fairly common problem and typical "admin/best practices" question - which we don't have too many. It pays off to spend some time fleshing out the answer to gain more hits for SFSE as a whole :) Besides - the steps will match pretty much any kind of data fix so the actual "auto number-specific" part is small. May 14, 2013 at 8:08
-
1I think you'll have to comment out the references to the field in Apex, VF and formulas, deploy the changes (still making sure tests pass and 75% coverage is met), run data fix, redeploy original code back :/ If it's too much hassle try to convince your business users it's better to make new field, fill with data, flip to autonumber (or use the "Generate..." trick) and deploy Apex that mentions only the new field. May 14, 2013 at 8:12