0

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;
2
  • How many records do you have that match the query criteria? If it's showing only one '0' that means you have more than 1000 records returning from the query since you're initializing the nextInvNum variable to the record count it will start assigning from your 4th if statement. Commented Apr 8, 2014 at 6:31
  • Keith's answer beat me out... you need to use if ..else :)
    – Prady
    Commented Apr 8, 2014 at 7:08

4 Answers 4

2

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;
0

You need to use if-else conditional statement instead of simple if statements.

1
  • That's a very short answer (It got automatically reported as low quality). Could you possibly explain further or include the specific code changes as an example ? Commented Apr 8, 2014 at 8:53
0

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

0

// 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;
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .