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, 2015 at 13:29

1 Answer 1

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.

You must log in to answer this question.

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