Created
December 4, 2012 13:10
Salesforce trigger to generate invoice number, each account has its own running number, format {AccountNumber}-{YYYY}-{MM}-{000}
This file contains 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 InvoiceTrigger3 on Invoice__c (before insert) { | |
if (trigger.isBefore && trigger.isInsert) | |
{ | |
// ---- GENERATE INVOICE NUMBER, AUTO RUNNING NUMBER, EACH ACCOUNT HAS ITS OWN RUNNING NUMBER --- // | |
// ---- INVOICE NUMBER FORMAT : {ACCOUNTNUMBER}-{YYYY}-{MM}-{000} --// | |
// ---- ASSUME THAT ACCOUNT NUMBER IS MANDATORY -- // | |
// ---- BECAUSE WE HAVE YEAR AND MONTH INFORMATION IN THE NUMBER, EASIER IF WE CREATE A FIELD FOR THE NUMBER [ACC RUNNING NO] --// | |
Integer iCharLen = 3; // character length for number | |
Integer iLastNo = 0; // information last running number this year | |
String strZero = '0'; | |
// search list of account | |
String strTemp; | |
Map<Id, Integer> mapAccLast = new Map<Id, Integer>(); // map of last number for an account | |
for (Invoice__c Inv: trigger.new) | |
{ | |
mapAccLast.put(Inv.Account__c, 0); // add default last number | |
} | |
// search latest invoice number | |
List<String> arrStr = new List<String>(); | |
Set<Id> setAccId = mapAccLast.keyset(); | |
Map<Id, String> mapAccNumber = new Map<Id, String>();// map account number for each account id | |
for (AggregateResult ar : [Select Account__c, MAX(Acc_Running_No__c) last_no | |
From Invoice__c | |
WHERE IsDeleted = false AND Account__c IN : setAccId | |
GROUP BY Account__c | |
]) | |
{ | |
strTemp = String.valueOf(ar.get('last_no')); | |
mapAccLast.put(String.valueOf(ar.get('Account__c')), Integer.valueOf(strTemp)); | |
} | |
// get information of account number | |
for (Account A: [SELECT Id, AccountNumber FROM Account WHERE Id IN : setAccId]) | |
{ | |
mapAccNumber.put(A.Id, A.AccountNumber); | |
} | |
// start generate number | |
String strNo = ''; | |
for(Invoice__c Inv: trigger.new) | |
{ | |
iLastNo = 0; // init | |
if (mapAccLast.containsKey(Inv.Account__c)) iLastNo = mapAccLast.get(Inv.Account__c); | |
iLastNo++; // update to next number | |
strTemp = String.valueOf(iLastNo); | |
if (strTemp.length() < iCharLen) strTemp = strZero.repeat(iCharLen - strTemp.length()) + strTemp; // add 0 prefix | |
strNo = ''; | |
//if (mapAccNumber.containsKey(Inv.Account__c)) | |
strNo += mapAccNumber.get(Inv.Account__c) + '-'; | |
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 | |
Inv.Acc_Running_No__c = iLastNo; | |
mapAccLast.put(Inv.Account__c, iLastNo); // update map info | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment