3

I want to update the name field of newly inserted record with auto number field value.But auto number field is showing as null.But when I see the account record I could see the value of auto number field value on account page.

public PageReference MakeAnonymous(){
Account acc = new Account(Name='Anon');
insert acc;
system.debug('account number..'+ acc.Auto_Number__c);
acc.Name = acc.Name + '-'+ acc.Auto_Number__c;
update acc;
}

1 Answer 1

9

The value of auto_number__c is set when you insert the record. You would need to query the database to see what the value is.

For example

public PageReference MakeAnonymous(){
Account acc = new Account(Name='Anon');
insert acc;
acc = [select auto_number__c, name from account where id = :acc.id];
system.debug('account number..'+ acc.Auto_Number__c);
acc.Name = acc.Name + '-'+ acc.Auto_Number__c;
update acc;
}
2
  • 7
    +1 but I'd say it in a bit more generic way - only Id value returns when you insert a record. It's your job to fetch back any values that were set on the database level (autonumbers, which record type was selected, what values were set by specifying a default val. on a field, what was set using workflows or triggers, who is the new record owner...)
    – eyescream
    Commented Jan 2, 2014 at 18:05
  • Good point @eyescream
    – Doug B
    Commented Jan 3, 2014 at 9:52

You must log in to answer this question.

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