Case Updates Threaded Variable?

In my email template there are variables like $aop_case_updates_description, that outputs the latest update in the email template. This works. acase_name which works, and $acase_case_number which works.

However, there is also $acase_aop_case_updates_threaded which doesn’t seem to work. (I think it did previously but not sure how long ago, I just noticed it).

Does anyone know wherethis comes from and how it gets populated. I tried searching for that variable in the code, but didn’t find it. Is it from the bean?

@pgr I know you did a lot of work with these variables in your plugin. Can you point me in the right direction where to look?

There’s different code depending on where the variable is getting replaced: compose window, campaigns, etc

Which one do you need?

You don’t find it when searching because it’s a concatenation of module name and variable…

Some old notes of mine on variable replacement…

Code flow 1 (Compose)
-----------------
EmailsController actions_save 
> replaceEmailVariables 
> EmailTemplate::parse_email_template
> preg_match_all
> substr_replace

Code flow 2 - Campaigns, queue, wizard
--------------------------------------
modules/EmailMan/EmailManDelivery.php (no class)
EmailMan::sendEmail
> EmailTemplateParser::parseVariables
>preg_match_all
>str_replace

Thanks @pgr, this is the one that’s the problem, it recogizes it as a variable because it outputs blank instead of the name, but it doesn’t seem to work and I’m looking for where it comes from to troubleshoot:

$acase_aop_case_updates_threaded

I don’t see any DB value for the threaded case updates or any variable that populates this value anywhere. It would either have to be stored as a DB value for it to available in the email, or somehow added to the bean at the point in time where the email is created. I tried a few things and was not able to populate the value at the time of the email.

So, I kind of cheated. I made a custom text field threaded_updates_c and and a before save hook that populates this field with a decending timeline of case updates. Then I just output this custom field in the email.

It works! When I have more time I’d like to explore how I would add that value to the bean when the email is created. This could be super useful if it was possible to populate short codes in emails via a custom function. Not sure if that’s possible but that would be great. It’s too bad there was something like “an on email send” hook.

You didn’t tell me where your email is created yet… campaigns, compose, etc?

The email is created in modules/AOP_Case_Updates/CaseUpdatesHook.php (I think!)

I tried inserting the variable in this function using parsed_entities and another function that pulled the list of case updates, but couldn’t get it to work:

(this is the original)

public function sendCaseUpdate(AOP_Case_Updates $caseUpdate)
    {
        global $current_user, $sugar_config;
        $email_template = BeanFactory::newBean('EmailTemplates');

        $module = null;
        if (isset($_REQUEST['module'])) {
            $module = $_REQUEST['module'];
        } else {
            LoggerManager::getLogger()->warn('Requested module is not set for case update');
        }

        if ($module === 'Import') {
            //Don't send email on import
            LoggerManager::getLogger()->warn("Don't send email on import");

            return;
        }
        if (!isAOPEnabled()) {
            LoggerManager::getLogger()->warn("Don't send email if AOP enabled");

            return;
        }
        if ($caseUpdate->internal) {
            LoggerManager::getLogger()->warn("Don't send email if case update is internal");

            return;
        }
        $signature = [];
        $addDelimiter = true;
        $aop_config = $sugar_config['aop'];
        if ($caseUpdate->assigned_user_id) {
            if ($aop_config['contact_email_template_id']) {
                $email_template = $email_template->retrieve($aop_config['contact_email_template_id']);
                $signature = $current_user->getDefaultSignature();
            }
            if ($email_template->id) {
                foreach ($caseUpdate->getContacts() as $contact) {
                    $GLOBALS['log']->info('AOPCaseUpdates: Calling send email');
                    $emails = [];
                    $emails[] = $contact->emailAddress->getPrimaryAddress($contact);
                    $caseUpdate->sendEmail(
                        $emails,
                        $email_template,
                        $signature,
                        $caseUpdate->case_id,
                        $addDelimiter,
                        $contact->id
                    );
                }
            }
        } else {
            $emails = $caseUpdate->getEmailForUser();
            if ($aop_config['user_email_template_id']) {
                $email_template = $email_template->retrieve($aop_config['user_email_template_id']);
            }
            $addDelimiter = false;
            if ($emails && $email_template->id) {
                LoggerManager::getLogger()->info('AOPCaseUpdates: Calling send email');
                $caseUpdate->sendEmail(
                    $emails,
                    $email_template,
                    $signature,
                    $caseUpdate->case_id,
                    $addDelimiter,
                    $caseUpdate->contact_id
                );
            }
        }
    }