I just wanted to create an SObject (Account) as a placeholder.

here's my code:

Account acc = new Account();

acc.ContactId = contact.Id;
acc.Name = 'Sample Account';

System.debug(acc.ContactName__c); // Output is null. This field is a formula. Contact.FirstName & " " & Contact.LastName

Is there a way to generate the value for that formula without inserting the record?

2 Answers 2


Recalculation of cross object formulas is not supported. There is no programmatic, generic way to do what you are asking.

  • Yeah, I figured. Just need to confirm. Thank you!
    – Carl Ortiz
    Apr 28, 2022 at 7:25

If you're just wondering how to calculate a formula without "having the object inserted", take to transactions:

Account acc = new Account();
acc.Contact__c = contact.Id;
acc.Name = 'Sample Account';
SavePoint sp = Database.setSavepoint();
insert acc;
acc = [SELECT ContactName__c FROM Account WHERE Id =: acc.Id];
List<Account> l = [SELECT ContactName__c FROM Account WHERE Id =: acc.Id];
System.assertEquals(0, l.size());
  • 1
    That's great. But kinda slows the process.
    – Carl Ortiz
    Apr 28, 2022 at 7:24

You must log in to answer this question.

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