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!

  • Do the receipt number ids need to be consecutive, or just unique? – Jeremy Nottingham Jun 20 '13 at 18:36
  • @Jeremy : It should be consecutive. – Vignesh Damodharan Jun 21 '13 at 1:27
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

| improve this answer | |

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.