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;
Attribution to: sumit
Possible Suggestion/Solution #1
If you take an example of say 5, all your if(nextInvNum
tests are true and each one does its assignment with the last one (that only adds a single '0') winning.
One way to fix this is:
if(nextInvNum < 10)nextInvNumStr = '00000'+nextInvNum;
else if(nextInvNum < 100)nextInvNumStr = '0000'+nextInvNum;
else if(nextInvNum < 1000)nextInvNumStr = '000'+nextInvNum;
else if(nextInvNum < 10000)nextInvNumStr = '0'+nextInvNum;
else if(nextInvNumStr == '')nextInvNumStr = ''+nextInvNum;
Attribution to: Keith C
Possible Suggestion/Solution #2
You need to use if-else conditional statement instead of simple if statements.
Attribution to: sunny
Possible Suggestion/Solution #3
Simple solution is to pad with zeros and then trim - too many if/else makes code difficult to read and more error prone. e.g.
integer nextInvNumStr=84;
string output = system.today().year() +'-'+ (('00000'+nextInvNumStr).right(6));
system.debug(output);
output is 2014-000084
Attribution to: Richard Durrant
Possible Suggestion/Solution #4
// I am using Aggregate result but that functionality not working properly
// i Write code like this. This working properly.
// what is the use of Aggregateresult
trigger aggregate_result_is_not_working on ChildAccount__c (after insert, after update, after delete) {
list<ParentAccount__c> acc=[select id, Childs_Amount__c, (select id, Rec_Amount__c from ChildAccounts__r) from ParentAccount__c];
list<ParentAccount__c> act=new list<ParentAccount__c>();
for(ParentAccount__c aa:acc){
aa.Childs_Amount__c=0;
for(ChildAccount__c cc:aa.ChildAccounts__r){
aa.Childs_Amount__c= aa.Childs_Amount__c + cc.Rec_Amount__c;
}
act.add(aa);
}
update act;
}
Attribution to: Chvl Narayana
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32129