Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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;
}
share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

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;
}
share|improve this answer
3  
+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 at 18:05
    
Good point @eyescream –  Doug B Jan 3 at 9:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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