Using beans to create PDF with specific filename in SuiteCRM 7.11.10

Hello all,

I apologize - I am new to coding with SuiteCRM and there are some things changed from version to version, that I don’t quite understand yet.
So please bear with me!

So far we used older version of SuiteCRM and we had the following customization in file

line 78 (where it was $file_name = str_replace(" ", “_”, $template->name) . “.pdf”:wink:

$somecustomvariable01 = 'Not specified';

if($module_type == 'Contacts')
	$contact = new Contact();
	$somecustomvariable01 = str_replace(" ","_",$contact->name);
} else{
	$account = new Account();

	$somecustomvariable01 = str_replace(" ","_",$account->name);

	$file_name = date("Y-m-d").'_'.str_replace(" ","_",$template->name).'_'.$somecustomvariable01.".pdf";

which allowed us to save files via Action->Generate Letter, with file names, depending the module we used include either Account name or Contact name.

After upgrading to the latest SuiteCRM, the current code doesn’t work anymore, as many functions replaced by bean.

We tried some code like the following for some specific Accounts and Contacts IDs

if ($bean->module_dir == 'Accounts') {
		$bean01 = BeanFactory::getBean('Accounts', 'b23e226e-05d2-8251-23e3-5887dde4555e');
		$somevariable01 = $bean01->name;
		$somecustomvariable01 = str_replace(" ","_",$somevariable01);
else if ($bean->module_dir == 'Contacts') {
		$bean02 = BeanFactory::getBean('Contacts', '9eeqe348-e43c-e324-2242-52425e345a35');			
		$somevariable02 = $bean02->last_name;
		$somevariable04 = $bean02->first_name;
		$somecustomvariable01 = str_replace(" ","_",somevariable02).'_'.str_replace(" ","_",$somevariable03);
$file_name = date("Ymd").'_'.str_replace(" ", "_", $template->name).'_'.$somecustomvariable01. ".pdf";

And we get the desired filename structure.

We tried to change the ID field (eg the ‘b23e226e-05d2-8251-23e3-5887dde4555e’) so it gets it dynamically, depending the module we were, like
$current_id = $bean->fetched_row[‘id’]
etc, with no success.

Is there a way to get current visited Account or visited Contact ID, in that field, so we get the correct file name?

Thank you in advance!

Hi, welcome to the Community! :tada:

From just a quick glance at the code I’d say there can be multiple records, and they are saved here in an array:

Try iterating that array, or checking if it has a single record, and using the first item.

If this is not it, then I would try to examine what else is coming in $_REQUEST

Thanks a lot for answering pgr,

Will try your solution and get back with an answer!

1 Like


Unfortunately I tried to replace
the ‘b23e…55e’ and ‘9eeq…5a35’ in

$bean01 = BeanFactory::getBean(‘Accounts’, ‘b23e226e-05d2-8251-23e3-5887dde4555e’);
$bean02 = BeanFactory::getBean(‘Contacts’, ‘9eeqe348-e43c-e324-2242-52425e345a35’);

with dynamic fields, but I cant get the current Account or Contact ID, so I get the desired result.

In line 65 as listed above, you are inside the loop that goes through the beans in the response. You have everything you need there, no need to go fetching for beans later. So just reference $val adding your code inside that loop:

while ($val = DBManagerFactory::getInstance()->fetchByAssoc($result, false)) {
    array_push($recordIds, $val['id']);
    if ($val->module_dir == 'Accounts') {
		$somevariable01 = $val->name;
		$somecustomvariable01 = str_replace(" ","_",$somevariable01);
    else if ($val->module_dir == 'Contacts') {
		$somevariable02 = $val->last_name;
		$somevariable04 = $val->first_name;
		$somecustomvariable01 = str_replace(" ","_",somevariable02).'_'.str_replace(" ","_",$somevariable03);

When the loop finishes, you’ll have those variables filled with the values from the last iteration, meaning the last selected record.

Then outside the loop you can work out your file name.

Unfortunately, I tried your proposal by using the exvtly following code, with no result.
Files return with variable $somecustomvariable01 always blank (Either being in Account or in Contact)

if (isset($_REQUEST['current_post']) && $_REQUEST['current_post'] != '') {
    $order_by = '';
    $mass = new MassUpdate();
    $mass->generateSearchWhere($_REQUEST['module'], $_REQUEST['current_post']);
    $ret_array = create_export_query_relate_link_patch($_REQUEST['module'], $mass->searchFields, $mass->where_clauses);
    $query = $bean->create_export_query($order_by, $ret_array['where'], $ret_array['join']);
    $result = DBManagerFactory::getInstance()->query($query, true);
    $uids = array();
    while ($val = DBManagerFactory::getInstance()->fetchByAssoc($result, false)) {
        array_push($recordIds, $val['id']);
		if ($val->module_dir == 'Accounts') {
			$somevariable01 = $val->name;
			$somecustomvariable01 = str_replace(" ","_",$somevariable01);
		else if ($val->module_dir == 'Contacts') {
			$somevariable02 = $val->last_name;
			$somevariable03 = $val->first_name;
			$somecustomvariable01 = str_replace(" ","_",$somevariable02).'_'.str_replace(" ","_",$somevariable03);
} else {
	$recordIds = explode(',', $_REQUEST['uid']);
$template = BeanFactory::getBean('AOS_PDF_Templates', $_REQUEST['templateID']);

if (!$template) {
    sugar_die("Invalid Template");

$file_name = date("Ymd").'_'.str_replace(" ", "_", $template->name).'_'.$somecustomvariable01 . ".pdf";

I’m afraid that “bean” returns variables with different way than in the past.

Sorry, I am programming just by looking at code, not running it, I guess something must be escaping me. This should be a simple PHP issue, though.

Exactly from where in the UI do you trigger this code? Which module, which buttons do you click?

Sorry for the late reply.

The modules used are Accounts and Contacts

1.1 When we are in one Account, we go to Actions->Print to PDF
1.2 We select one of our templates and wait for the file to be saved

2.1 When we are in a Contact, we go to Actions->Print to PDF
2.2 We select one of our templates and wait for the file to be saved

In either of them, we don’t get the Account or Contact name in the saved file. The variable doesn’t return via the code mentioned, in either case.

Sorry, I meant to help further but I am afraid I don’t have enough time for such a detailed assistance, I would have to start coding for you. Can you get some help from a more experienced PHP developer in your organization?