1

I've a trigger on a custom object Object_A__c. Now, I need to populate a receipt number id in a field receipt_id__c.

There are two types of receipts X and Y namely. I need to populate the receipt_id__c with X-001, X-002,etc., and similarly Y-001, Y-002, etc.,. i thought of using AutoNumber field but got stuck since its the same object I'm working on and the auto number field increments for each record irrespective of the type of receipts. I need to handle bulk insert as well.

How should I proceed now?

Thanks in advance!

2
  • Do the receipt number ids need to be consecutive, or just unique? Commented Jun 20, 2013 at 18:36
  • @Jeremy : It should be consecutive. Commented Jun 21, 2013 at 1:27

1 Answer 1

2

It is possible to populate receipt_id__c with {X-0000} or {Y-0000} depending on receipt type. You can create two separate custom objects, let's say: seqX and seqY, add two lookup relations to the Object_A__c then create a formula field (this is your receipt_id__c) as follows:

 CASE(TEXT(DocumentType__c), 
   'TypeX', seqX_ref__r.DocX_num__c, 
   'TypeY', seqY_ref__r.DocY_num__c, 
    '')

You'll also need to write a trigger that creates new seqX or seqY and assign it to the Object_A__c accordingly (to seqX_ref__c or to seqY_ref__c).

But there is one serious issue - autonumber does not guarantee that will be consecutive i.e. when you insert an object and an exception or validation error occurs you'll see gaps in the sequence.

You can find trigger implementation here: CreateSequence.apex

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .