Created
November 30, 2012 16:35
-
-
Save ykurnia/4176831 to your computer and use it in GitHub Desktop.
Salesforce trigger to generate invoice number, reset each month, based on Invoice Date {YYYY}-{MM}-{000}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger InvoiceTrigger2 on Invoice__c (before insert) { | |
if (trigger.isBefore && trigger.isInsert) | |
{ | |
// ---- GENERATE INVOICE NUMBER, AUTO RUNNING NUMBER, RESET EVERY MONTH BASED ON INVOICE DATE --- // | |
// ---- INVOICE NUMBER FORMAT : {YYYY}-{MM}-{000} --// | |
// ---- ASSUME THAT INVOICE DATE CAN BE BACKDATED -- // | |
Integer iCharLen = 3; // character length for number | |
Integer iLastNo = 0; // information last running number this year | |
String strZero = '0'; | |
// search type of invoice month, because possible to have trigger for different month | |
String strMonthCode = ''; | |
String strTemp; | |
Map<String, Integer> mapMonthLast = new Map<String, Integer>(); // map of last number for a month | |
for (Invoice__c Inv: trigger.new) | |
{ | |
strMonthCode = String.ValueOf(Inv.Invoice_Date__c.year()) + '-' + String.ValueOf(Inv.Invoice_Date__c.month()); | |
mapMonthLast.put(strMonthCode, 0); // add default last number | |
} | |
// search latest invoice number | |
List<String> arrStr = new List<String>(); | |
Set<String> setMonthCode = mapMonthLast.keyset(); | |
for (AggregateResult ar : [Select CALENDAR_YEAR(Invoice_Date__c) inv_year, CALENDAR_MONTH(Invoice_Date__c) inv_month, | |
MAX(Name) last_name | |
From Invoice__c | |
WHERE IsDeleted = false AND Month_Code__c IN : setMonthCode | |
GROUP BY CALENDAR_YEAR(Invoice_Date__c), CALENDAR_MONTH(Invoice_Date__c) | |
]) | |
{ | |
strTemp = String.valueOf(ar.get('last_name')); | |
arrStr = strTemp.split('-'); | |
strMonthCode = String.valueOf(ar.get('inv_year')) + '-' + String.valueOf(ar.get('inv_month')); | |
if (arrStr.size() > 2 && arrStr[2].isNumeric()) mapMonthLast.put(strMonthCode, Integer.valueOf(arrStr[2])); | |
} | |
// start generate number | |
String strNo = ''; | |
for(Invoice__c Inv: trigger.new) | |
{ | |
iLastNo = 0; // init | |
strMonthCode = String.ValueOf(Inv.Invoice_Date__c.year()) + '-' + String.ValueOf(Inv.Invoice_Date__c.month()); | |
if (mapMonthLast.containsKey(strMonthCode)) iLastNo = mapMonthLast.get(strMonthCode); | |
iLastNo++; // update to next number | |
strTemp = String.valueOf(iLastNo); | |
if (strTemp.length() < iCharLen) strTemp = strZero.repeat(iCharLen - strTemp.length()) + strTemp; // add 0 prefix | |
strNo = String.ValueOf(Inv.Invoice_Date__c.year()) + '-'; | |
strNo += (Inv.Invoice_Date__c.month() < 10) ? '0' + String.ValueOf(Inv.Invoice_Date__c.month()) : String.ValueOf(Inv.Invoice_Date__c.month()); | |
strNo += '-' + strTemp; | |
Inv.Name = strNo; // update field | |
mapMonthLast.put(strMonthCode, iLastNo); // update map info | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment