here i created one code which will retrieve record from "Account" object and create records in "abc" object based on selected fields. i used "AggregateResult" to add numbers with current year and that will increase record by record.
now the issue is i want to show 5 zero leading a no. e.g, 2014-000001 2014-000010 and so on... i did the logic but its showing like 2014-01 or 2014-010...only one '0' i am not getting what going wrong...Please help me out....
Apex code:
Integer Month = (system.today().addDays(30)).month();
Integer Year = system.today().year();
Integer Day = system.today().day();
List<Account> accList = [Select Id,StartDate__c,endDate__c,Amount__c From Account where CALENDAR_MONTH(endDate__c) = :currentMonth and CALENDAR_YEAR(endDate__c) < = :currentYear and DAY_IN_MONTH(endDate__c) = :Day];
List<AggregateResult> argList = [select count(id) cnt from abc__c where Number__c like '2014%'];
integer nextInvNum = (integer)arList[0].get('cnt');
nextInvNum++;
List<abc__c> abcList = new List<abc__c>();
for(Account a : accList) {
abc__c ab = new abc__c();
ab.Account__c = a.Id;
ab.StartDate__c = a.StartDate__c;
ab.endDate__c = a.endDate__c;
ab.Amount__c = a.Amount__c;
string nextInvNumStr = '';
if(nextInvNum < 10)nextInvNumStr = '00000'+nextInvNum;
if(nextInvNum < 100)nextInvNumStr = '0000'+nextInvNum;
if(nextInvNum < 1000)nextInvNumStr = '000'+nextInvNum;
if(nextInvNum < 10000)nextInvNumStr = '0'+nextInvNum;
if(nextInvNumStr == '')nextInvNumStr = ''+nextInvNum;
ab.Number__c = (system.today().year() +'-'+ nextInvNumStr);
nextInvNum++;
abcList.add(ab);
}
insert abcList;
nextInvNum
variable to the record count it will start assigning from your 4thif
statement.