0

In my account object, I have a custom field aNo. This should do the same functionality as an auto number.

public class incrementHandler{
    set <Id> accId;
    list <Account> aList;
    public incrementHandler (){
        aId = new set <ID>();
        aList = new list <Account> ();
    }

    public void onInsert( list<Account> newList){
        integer count = 0;
        for(Account a : newList){
            accId.add(a.Id);
            }
            count = accId.size();
            a.aNo = count;
            aList.add(a);
        }
        update aList;
   }
}

The code is not working. Where am I going wrong?

1
  • 2
    why don't you just set it up as an autonumber? – Novarg Nov 20 '15 at 13:29
1

Write trigger on before insert .

public class incrementHandler
{
    public void onInsert( list<Account> newList){

    List<Account> lstAcc = [SELECT Id,aNo FROM Account Order BY Createddate DESC LIMIT 1];
    Integer intCounter = lstAcc.size() != 0 ? lstAcc[0].aNo : 0;
    for(Account objAccount: newList)
    {
        intCounter ++;
        objAccount.aNo = intCounter;
    }

    }
}

This will solve your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.