Domanda

Is it possible to add related module's fields to the PDF Template? I'm trying to get the Account's name when creating a PDF template for Opportunity module. I have tried $account.name but it does not bite. Any ideas?

È stato utile?

Soluzione

This can be applied to any module.

Create this file: custom/modules/Opportunities/sugarpdf/sugarpdf.pdfmanager.php

Paste the code below

Repair and rebuild

<?php

if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once('include/Sugarpdf/sugarpdf/sugarpdf.pdfmanager.php');

class OpportunitiesSugarpdfPdfmanager extends SugarpdfPdfmanager
{

    function preDisplay()
    {
        parent::preDisplay();
        $this->set_account();
    }

    function set_account()
    {
        $account=array();
        if($this->bean->account_id)
        {
            $account_object=new Account();
            $account_object->retrieve($this->bean->account_id);
            $account_object = (array) $account_object;
            $account=$account_object["fetched_row"];
        }
        $this->ss->assign('account', $account);
    }
}

You can now get fields from the related account by calling

{$account.name}, {$account.billing_street_address}

inside the PDF Manager.

Altri suggerimenti

Follow up to original (awesome) answer:

  1. In the newest version of Sugar 7, custom relationships are now available on the Links dropdown in PDF Manager, so custom code provided by karlingen is no longer necessary for populating PDF with a related record where the parent module is the "Many" in the "One to Many" relationship (ie, Links section in dropdown only provides relationships where the related module would only have a single record).

  2. There is currently an enhancement request to allow references to the "Many" side of a relationship for PDF Templates, similar to product_bundles array for Quote PDFs. This can be accomplished using karlingen's approach, but with just a little extra work. Here's how it would look for getting the related Cases for a Contact record:

Create the custom file:

/custom/modules/Contacts/sugarpdf/sugarpdf.pdfmanager.php

with contents:

<?php

if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');


require_once('include/Sugarpdf/sugarpdf/sugarpdf.pdfmanager.php');


    class ContactsSugarpdfPdfmanager extends SugarpdfPdfmanager
    {

        public function preDisplay()
        {
            parent::preDisplay();
            $this->getCases();  
        }

        protected function getCases() 
        {
            $this->bean->load_relationship('cases');
            if($this->bean->cases) {
                $cases_list = $this->bean->cases->getBeans();

                $cases = array();
                foreach ($cases_list as $case) {
                    $caseFields = PdfManagerHelper::parseBeanFields($case, true);
                    $cases[] = $caseFields;
                }

                 $this->ss->assign('cases', $cases);
            }
        }
    }

With this in place, you won't get "cases" under links, but you can add the references directly in the PDF template, like:

{foreach from=$cases item="acase"}
    {$acase.name}
{/foreach}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top