Multiple Related Records Into Email/PDF Templates?

Hi there,

Iā€™m hoping to get some help pulling in data from a moduleā€™s related records. Hereā€™s whatā€™s happening: I have a primary module (called ā€œManifestsā€) that has multiple (one to many) related records (called ā€œBookingsā€). Thus each Manifest will have multiple bookings related to it.

When I create an email or PDF template for the ā€œManifestā€ module, only the first related ā€œBookingā€™sā€ information gets pulled into the email/PDF template (Iā€™m guessing the one with the highest id) ā€“ so putting in $booking_name only pulls the first Bookingā€™s name, and so on with the other variables.

Is there a way to pull in all of the related recordsā€™ fields/data into a template? Iā€™m semi-fluent in coding so if I could get pointed in the right direction with where to start or if thereā€™s a simple solution for this I would be most appreciative! Thanks!!

Hi,

I could be wrong here, but this sounds like something similar to the invoice template where it pulls in the related line items from the invoice into the templateā€¦

This is where I would start. looking at how AOS Invoices/Quotes works.

If memory is correct, AOS_PDF_Templates/generate_letter.php is where it generates the template, I think you will want to take a look at that.

Ian.

1 Like

Thanks Ian. Iā€™ll take a look at that today and will let you know how it goes. If I get it working Iā€™ll post it here as I think this will be helpful for others as well.

Thanks again Ian and take care!

Cheers,

Daniel

Hi Daniel,

I am with same problemā€¦

Did you find a solution for this?

Thanks!

1 Like

Hi Samuka,

Yes I was able to do this. Itā€™s not an elegant solution (nor upgrade safe) but essentially hereā€™s what I did:.

  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[ā€˜uidā€™]);} 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.

Thank you for explaining the steps in detail.

I have been trying ur TuT, but stucked at the related_link part. I checked into my custom module vardef file, which is located in the htdocs/modules/ā€œmy-custom-module-nameā€/, there is no any array about the related module, where could I determine what is the correct related_link and the field name (what is field name? I read the article ā€œget-linked-beans-in-suitecrm-7-4-1-onwardsā€, is the field name same as the custom field we create for module? )