+ Start a Discussion
ricky88ricky88 

Different Auto Number for Different Branches for Purchase Orders, Invoices and Official Receipts

We have ONE object with 3 different record types:

 

1. Purchase Orders (PO)

2. Invoices (Inv)

3. Official Receipts (OR)

 

There are three different operating branches that uses these, example as below:

 

1. New York

2. Sydney

3. Paris

 

We want to have different auto-numbers for these 3 different record types for these 3 different branches. Example as follows:

 

NY-PO-1001

NY-PO-1002

SYD-PO-1001

SYD-PO-1002

PA-PO-1001

PA-PO-1002

NY-Inv-1001

NY-Inv-1002

SYD-Inv-1001

SYD-Inv-1002

PA-Inv-1001

PA-Inv-1002

NY-OR-1001

NY-OR-1002

SYD-OR-1001

SYD-OR-1002

PA-OR-1001

PA-OR-1002

 

Using Auto-Numbers in Salesforce produces the result below, which we DO NOT WANT:

 

NY-PO-1001

NY-PO-1002

SYD-PO-1003

SYD-PO-1004

PA-PO-1005

PA-PO-1006

NY-Inv-1007

NY-Inv-1008

SYD-Inv-1009

SYD-Inv-1010

PA-Inv-1011

PA-Inv-1012

NY-OR-1013

NY-OR-1014

SYD-OR-1015

SYD-OR-1016

PA-OR-1017

PA-OR-1018

 

What is the solution without APEX or triggers for this?

 

If there is no standard solution in Salesforce, what do we need to do for APEX / Triggers to resolve the above?

 

SteveBowerSteveBower

Hi, sadly, I do not think there is a way to accomplish this without using an Apex Trigger for the object.  The problem as you clearly see, is having the nine specific autoincrement fields instead of just one.  The other values (NY - PO, etc.)  could be set with a Field Update rule called from a Workflow, however I think that keeping the counters somewhere has to be done with code.

 

It's not a hard trigger, you just need a place to store the nine counters.   Create a new Custom object called "AutoNumberHolder".  That has three fields.  1. an Integer for the autonumber   2. The name of the record type of the object, and 3, the name of the operating branch.

 

Then create nine records for that object, for the various combinations, and set the integer to 0.  

 

 

Then write a Before Insert trigger for the object which merely takes the Record type and the branch information and retrieves the counter for the record you're inserting.  Use that to name your record, bump it up by one, and update the counter.

 

 

It get's a little more complex when you write the trigger to be bulk safe in case you insert a bunch of objects in one call.  In that case you probably just want to retrieve all nine records (locked for update), use them as needed for your insertion, and then update the ones that have changed.

 

Best, Steve.

EnthEnth

You can create just 3 auto number fields, one per branch (with just the format {0000}) and then create a formula field to display with the type and branch prefix.

 

You'll then have a single field to include on reports, pages and emails.

 

OK - just checked your example again and see you'd want 9 separate autonumber fields instead (if that's permitted)- but you should still use a formula field for a single display object.

 

Personally I'd go with the Apex option.

sfdcfoxsfdcfox

Technically, probably the easest way you could do this is by having the numbers for each department be tracked in an entirely other object (a Custom Setting comes to mind). A simple trigger could then be used to assign numbers by region and increment the counters appropriately.

torctorc

I have a similar dilemma:

 

I need to create an auto-number thats formatted as follows:

 

U{00000}-AN

 

Where "U" = a single letter value from a field on the user record, and "AN" is the first two letters of the Account Name.I'm assuming for the account name I would use a formula field like = LEFT( Account.Name , 2) 

 

Also, the auto-number needs to start at 40,000

 

Unlike the example above, I do not need to count separately for each combination, just a solid count from 40000 upwards with dynamic characters.

 

Any ideas? I'm not familiar with Apex so I will need a simple solution if possible. 

arun kumar.ax887arun kumar.ax887

How can we do it with Apex option,  could you please send the sample code, so that it would be easier to understand.

 

 

 

 

 

regards

 

Arun

sfdcfoxsfdcfox

Step 1. Create a Custom Setting (Setup > Develop > Custom Settings) that you'd like to use. Create as many number fields as you need for the various numbers you'd like to track.

 

Step 2. Create a trigger (Setup > Customize/Create > [Object] > Triggers) that runs "before insert". The code might look like this:

 

trigger updateAutoNumber on myObject (before insert) {
  // Provide default values if settings do not exist.
  myCustomSetting__c settings = new myCustomSetting__c(name='myObject',Counter1__c=0,Counter2__c=0,Counter3__c=0);
  // Try and load the custom settings.
  for(myCustomSetting__c setting:[select id,name,Counter1__c,Counter2__c,Counter3__c from myCustomSetting__c where name='myObject'])
    settings = setting;
  // Loop through the records in the trigger.
  for(myObject record:Trigger.new) {
    if(record.CounterType__c == 'A') // whatever condition you'd like to use here.
      record.Name = 'A' + String.valueOf(setting.Counter1__c++);
    if(record.CounterType__c == 'B') // Another condition here.
      record.Name = 'B' + String.valueOf(setting.Counter2__c++);
    if(record.CounterType__c == 'C') // Yet another condition here.
      record.Name = 'C' + String.valueOf(setting.Counter3__c++);
  }
  upsert settings;
}

Note that you'll need to consider if you want to format with padding characters, etc, and of course changing the setting values will change the next auto-increment number (which could lead to duplicate names). Finally, you'll need to provide a page override to the New button for the object to default the name of the record to some value (typically we use something like "[automatically created when saving]"). This is typically done with a simple Visualforce page with the appropriate type of override:

 

<apex:page standardController="myObject">
<script>
window.top.location.href = '{!URLFOR($Action.myObject.New,null,[Name="[automatically created when saving]"],true)}'
</script>
</apex:page>

You will probably want to look at "Help & Training" for the correct usage of URLFOR. Finally, all you need to do is to create a test method to deploy the code to Production, and you'll be set. Please note that the object's Name field must be set to Text, not Auto-Number. In particular, some standard objects have auto-number values as the Name field type, and you won't be able to use this trick on those types of objects, since the field is read-only.

Håkan LöfqvistHåkan Löfqvist

Add a "Next Number" field to your Branch records and make a roll up summary on the Operating Branches for the MAX Next Number (this requires a master detail relation ship between Branch and Operating Braches). Create a workflow for Braches that uses the Operating Branches MAX Next Number to set the Branch Name and also increments the Branch Next Number.

 

I haven't actually tired this, but it should work in theory.

GailGail

I have a similar situation but with many more permutations. Basically, the numbering is:

 

<division><YY>-<#> so would be B12-101, A12-101, G12-101. Then next year, the numbering starts all over again with B13-101 etc. 

 

Any ideas on how to tackle this? I could simply have 3 fields for each but then would have to create the field and update a formula field or workflow rule to use new fields each year. 

laxman.g1.3964314255283564E12laxman.g1.3964314255283564E12
hello ricky88,

i am also planning to generate autonumber same like. if u know the code with trigger plz help me.
AthiSachiAthiSachi
Hello ricky88,

  I have exact same requirement  except that I have 30+ location. Is it possible to achieve without code? Let me know thanks