Add the ability to use fields in related records in PDF

Hi,

Iā€™ve created a couple of custom modules Candidate and Courses.
and defined one to many relationship Candidate(1) ==>(*) Courses
now i want to create a PDF template based on Candidate and all associated Courses fields.

From HTML point of view (from where we create pdf) I would like to add something like

Course1.fieldA
Course2.fieldA
for each Course associated.

When I request the pdf creation from a Candidate (something like generate letter) I would like to load all courses associated
and add these fields in the final template automatically. Now:

  1. Where and How I can load all associated courses for Candidate instance?
  2. Where and How I can insert those values in a final template?

Iā€™m a very beginner, but for me is sufficient to know the right place to find Candidate Bean instance, load all associated Courses instance and found the
right place to insert these values.

Regards,

Mario

This will be very hard if you are beginner and there is too much involved to explain it all here. Hopefully what I have written below will be of some help.

http://developer.sugarcrm.com/2012/03/23/howto-using-the-bean-instead-of-sql-all-the-time/

http://developer.sugarcrm.com/2011/02/23/howto-using-get_linked_beans-method-to-grab-related-records/

The basic steps you will have to carry out:

Create a custom a copy of generatePdf.php here:

custom/modules/AOS_PDF_Templates/generatePdf.php

Rename the copy MyCustomPdf.php

Then create a custom button on your custom module that calls the functions in your custom copy of MyCustomPdf.php.
(this will involve creating a custom controller and adding a custom action that includes MyCustomPdf.php)

edit MyCustomPdf.php to work with the fields in your custom module. (this this would involved getting the Candidate instance and then getting all the linked courses via the get_linked_beans method or an sql query).

Create custom/modules/AOS_PDF_Templates/views/view.edit.php

edit the view.edit.php to include fields from your custom module so that they can be added to a pdf template.

Create a new pdf template for your module

1 Like

Hi Andy,

Thanks for these instructions. Iā€™ve ran into a quick snag however with these two instructions:

[b]1. "Then create a custom button on your custom module that calls the functions in your custom copy of MyCustomPdf.php.
(this will involve creating a custom controller and adding a custom action that includes MyCustomPdf.php) "

and

  1. ā€œCreate custom/modules/AOS_PDF_Templates/views/view.edit.php
    edit the view.edit.php to include fields from your custom module so that they can be added to a pdf template.ā€[/b]

I have my MyCustomPdf.php all ready and pulling in the related records nicely so if I can get a few quick pointers on how to add that custom button/controller/action and how to get the related fields into the template that would help me out a lot ā€“

Thanks again!

Hi Andy,

This is not an elegant solution (nor upgrade safe) but essentially hereā€™s what I did to accomplish what rossi was asking about above:

  1. Make sure you have the ā€œGenerate Letterā€ option working on your custom module, ā€œdetail viewā€ page: see this thread for that information: https://suitecrm.com/forum/developer-help/1379-no-preview-option-in-pdf-templates?limitstart=0

  2. Hereā€™s where I believe there may be a better way of doing this, but I was able to get it working:

formLetterPdf.php is the file that gets executed when a template is selected from the ā€œGenerate Letterā€ button above.

Edit formLetterPdf.php in /modules/AOS_PDF_Templates to include an ā€œifā€ statement specific to only include your custom module type (after the "if(isset($_REQUEST)ā€¦ else{ $recordIds = explode(ā€™,ā€™,$_REQUEST);} statement:

if ($module_type === ā€˜your_custom_moduleā€™)

Copy everything from ā€œ$template = new AOS_PDF_Templates();ā€ and down in this file and put inside of this new if statement (just copy and leave the old code there for the ā€œelseā€ statement).

Above what you just copied, to pull in the related records for a particular record, use this:

$example = BeanFactory::getBean(ā€˜your_custom_moduleā€™, $_REQUEST[uid]);
$related_records = $example->get_linked_beans(ā€˜related_linkā€™);

Where ā€œrelated_linkā€ would be the link field defined in the moduleā€™s vardef file (see this post: https://suitecrm.com/suitecrm/blog/entry/get-linked-beans-in-suitecrm-7-4-1-onwards)

Now that you have your related records pulled into the array $related_records youā€™ll have to build a new array that will include the fields from your related records for use in your PDF. I accomplished this with use of a ā€œforeachā€ statement:

$relatedarr = array();
$rowcount = 0;
$colcount = 0;

foreach($related_records as $related_record){

$relatedarr[$rowcount][$colcount] = $related_record -> some_field_name;
$relatedarr[$rowcount][$colcount+1]= $related_record -> some_other_field_name;
.
.
.
$rowcount++;
}

This also allowed me to keep a record of how many rows ($rowcount) I would eventually need to print out in the PDF to contain the related records.

Once you have your ā€œrelatedarrā€ array built you can use this to build the HTML variable ($newhtml) that needs to be inserted into the $pdf->writeHTML($newhtml) line in the copied text mentioned above to produce the PDF.

Make sure and create an empty template of type ā€œyour_custom_moduleā€ to use as the template to select after the ā€œGenerate Letterā€ popup appears. The HTML for the PDF is completely written in the formLettterPdf.php file so adding anything in this template will not be used when the file executes. You can, however, use the header and footer settings in this empty template as these are still passed through in your new ā€œifā€ statement above.

As noted above, this is NOT upgrade safe and will only work with one template and one module type, but for my purposes it worked out really well. Someone with more experience can probably clean up/improve my process above but I thought Iā€™d share this to help anyone get started ā€“

Thanks guys and look forward to hearing your feedback.

2 Likes

Thanks for the concept, Daniel.
Works great with the added bonus of full control over the HTML output.

I was just wondering if you ever discovered an upgrade safe way to do this. Just putting the files in the custom/modules doesnā€™t appear to workā€¦ (as I was expecting anyway)

Serg

Hello All,

I know this one is a quite old thread. But if anybody wants to have relational data in PDF or word file.
This add-on may helpful for you. Export relational data in word or pdf format