4

Here's an interesting problem (that I hope someone has a solution to!)

I have a Case that gets created in an Apex controller. The page has a button which then sends an email about this Case you just created. Works well, so far so good.

One field on this case is a boolean. The person who made this email template didn't want "true/false" in the email. They wanted "yes" or "no", so they created a formula field that writes Yes if my bool is true and No if false.

This also works perfectly fine, and the yes/no formula is used in the email.

However, the formula doesn't seem to populate in time to send the email.

I have debugs around the Case insert that verify bool == true before insert, and after insert, the formula field is still null. I receive the email, and the value is null. I go in and check on my record, and it's "Yes"! (Or "No" if false. Like I said, this works fine).

So I am assuming that there's something with the timing here. How can I approach this so that I can ensure I get the value reflected in the Email? Am I correct in my assumption of the issue?

  • 1
    if you are already sending the email in code why not just calculate it in the code? why make an extra field to hold the value? – Tim Willis Apr 5 '18 at 22:52
  • Are you using templates or building the email manually? – Adrian Larson Apr 5 '18 at 22:55
  • 1) This was already given to me to work on as such, so I didn't make this field proxy situation. Just how it's set up currently 2) I was considering hard coding it, but that doesn't seem very scalable so ideally, anything could just use this field (the Yes/No) which would be better abstraction in my opinion 3) The template is used, yes. It references the Y/N value – Natalie Paige Apr 5 '18 at 23:08
5

A formula field can only be "executed" when the record is either opened, requeried, or refreshed. If you've performed a new record save DML operation, you'll need to do a controller page refresh before that field can be populated for use with your email template when you do the "send".

Important to note that I'm making the assumption that you're likely working with a "new" record here. If not, you'll need to explain further why this isn't working and provide some code.

  • it is a new record! Thank you! Ended up using recalculateFormulas() – Natalie Paige Apr 6 '18 at 15:20

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.