I'd like to increment fields based on the quote field, so when a quote is created, a custom field will increment depend on the region (each field will have format of "ZO - {000}). Each region should have it's own series of Unique numbers. For example: if it's a "US" region quote, "Quote Number1" custom field will increment (. If it's a "ROW" region quote, "Quote Number2" custom field will increment.
Standard "Autonumber" field works on any record creation, and not customized by record types..
UPDATE: I modified a trigger posted as an answer (@user2590, see below your modified code). I want the autonumbers to work only for "Invoice" record type in Quotes ("Quote record type should be affected"). I assume it is simpler than the original code but somehow it doesn't work...I'm getting: "System.NullPointerException: Attempt to de-reference a null object". Can you have a look and see what's not working?
trigger SampleAutoNumberRegion on Quote (before insert){
String[] name = new String[]{'Invoice'};
Map<Id, RecordType> regions = new Map<Id,RecordType>([SELECT Name from RecordType WHERE sObjectType = 'Quote' AND Name IN :name]);
AggregateResult res = [SELECT MAX(US_Invoice__c) max1, MAX(ROW_Invoice__c) max2 FROM Quote WHERE RecordType.Name IN :name ];
Integer max1 = Integer.valueOf(((String)res.get('max1')).substringAfter('- ') );
Integer max2 = Integer.valueOf(((String)res.get('max2')).substringAfter('- ') );
for(Quote theQuote : trigger.new){
if(thequote.Opportunity.Account.Region__c.contains('US')){ //region corresponds to 'US'
String num1 = String.valueOf( (max1 != null) ? ++max1 : 1 );//will use the incremented value of max unless max was null (else use 1 instead)
for(Integer i=0; i< (3 - num1.length() ); i++){ //should pad with leading zeros to ensure format of {000}
num1 = '0' + num1;
}
theQuote.US_Invoice__c = 'ZO - ' + num1;
}
else if(!thequote.Opportunity.Account.Region__c.contains('US')) {
String num2 = String.valueOf( (max2 != null) ? ++max2 : 1 );
for(Integer i=0; i< (3 - num2.length() ); i++){ //should pad with leading zeros to ensure format of {000}
num2 = '0' + num2;
}
theQuote.ROW_Invoice__c = 'ZO - ' + num2;
}
}
}
thequote.Opportunity.Account.Region__c
will return null, and therefore .contains() throws an exception. You will need to query the database to get the Region__c value from the Account, and then use the queried value to determine the region to use.