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;
}
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;
}
  • 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 Jan 2 '14 at 18:05
  • Good point @eyescream – Doug B Jan 3 '14 at 9:52

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.