+ Start a Discussion
Andreas GerogiannisAndreas Gerogiannis 

Creating unique integer id from SF account id

Hi to all,

I am working on a project that I have to crate a unique id ..integer.. by converting salesforce id from alphanumeric to integer every time a new account is saved or updated-and-field-is-empty. I have written a trigger creating that field by removing letters from salesforce id but I receive a read only error when I try to update account record (after insert, after update). If I use it before insert, before update then salesforce id hasn't been created yet. How do I handle this? Ideas? PS. quite new to Apex ... 
Best Answer chosen by Andreas Gerogiannis
Shashikant SharmaShashikant Sharma
You need to write a future method for it form after insert trigger. Future methods get executed asynchronously ( mostly with in few seconds ).

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_future.htm

In winter 15 release there is "Submit and Monitor Jobs for Asynchronous Execution with the Queueable Interface" that will be an alternative to future methods for this sort of problem.

All Answers

AK-2AK-2
You probably could do this easily through a workflow rule and adding a field update step in the workflow.

If you want to use a trigger - you will need to create a copy of the object and then call update. For an example, look at this thread

http://salesforce.stackexchange.com/questions/23891/need-a-update-trigger-to-fire-after-record-insert
Andreas GerogiannisAndreas Gerogiannis
I have instantiated a list of accounts and then when I have done my updates and try to insert the llist ...

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UniqueID: execution of AfterInsert

caused by: System.FinalException: Record is read-only

On another note, how is it possible to turn clear alphanumeric from letters and convert it to numeric with field update?
Shashikant SharmaShashikant Sharma
You need to write a future method for it form after insert trigger. Future methods get executed asynchronously ( mostly with in few seconds ).

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_future.htm

In winter 15 release there is "Submit and Monitor Jobs for Asynchronous Execution with the Queueable Interface" that will be an alternative to future methods for this sort of problem.

This was selected as the best answer
AK-2AK-2
Andreas,

The object ids in salesforce is a string. Each character in the id can be a  0-9 number or upper/lower case character (although you can ignore first three characters since they identify the record's object type). So I am not sure how you will convert them to numbers. My reply was more about trying to get around the problem you had with the trigger update.

If you need a numeric id on each record of an object, I would say add an auto number field to the object.


Andreas GerogiannisAndreas Gerogiannis
Thank you both.

AK: I thought of that earlier.  I will see if I can use something like that.

Shashikant: you gave me good idea ..  I already researched about future methods and will try them just to add to my experience.