This is the final part of the tutorial to Control values of a multiple link depending on the values of another multiple link, you can see Parts 1 and 2 here:
https://forum.espocrm.com/forum/deve...-multiple-link
https://forum.espocrm.com/forum/deve...-multiple-link
Step 5
Create the back-end entry point class custom/Espo/Custom/EntryPoints/ConditionalLinkMultiple.php that will receive the ajax call specified in the code described in Step 4 of this tutorial https://forum.espocrm.com/forum/deve...-multiple-link, will execute an sql command on the database and will return a list of entity ids to use as the filter for displaying the list of available options for the multi link field.
Notice that this script also contains 2 commented statements that can be used for trouble shooting to make sure that the data passed by the ajax call is correct and that the final sql statement is correct.
For more on debugging PHP code see this post https://forum.espocrm.com/forum/deve...gging-php-code
Step 6
The final step is to clear cache and rebuild the Espo instance
This tutorial does not cover the layout specifications for the main ("Work Order") entity, which will depend on your preferences, but in our case the modified Work Order entity detail display looks like this:
https://forum.espocrm.com/forum/deve...-multiple-link
https://forum.espocrm.com/forum/deve...-multiple-link
Step 5
Create the back-end entry point class custom/Espo/Custom/EntryPoints/ConditionalLinkMultiple.php that will receive the ajax call specified in the code described in Step 4 of this tutorial https://forum.espocrm.com/forum/deve...-multiple-link, will execute an sql command on the database and will return a list of entity ids to use as the filter for displaying the list of available options for the multi link field.
PHP Code:
namespace Espo\Custom\EntryPoints;
use \Espo\Core\Exceptions\NotFound;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\BadRequest;
class ConditionalLinkMultiple extends \Espo\Core\EntryPoints\Base
{
public static $authRequired = true;
// default action
public function run()
{
// convert the POST JSON input received into a PHP associative array
$requestPayload = file_get_contents("php://input");
$payload = json_decode($requestPayload, true);
// $GLOBALS['log']->debug('ConditionalLinkMultiple Entry Point payload:', [$payload]);
$action = $payload["action"];
if(!$action) {
throw new BadRequest('ConditionalLinkMultiple.php "action" parameter is missing');
}
switch ($action) {
case 'getOptionListFilter':
// build the sql command from inputs received
$modelKeyValue = $payload['modelKeyValue'];
$modelTable = $payload['modelTable'];
$modelCriteriaTable = $payload['modelCriteriaTable'];
$criteriaTable = $payload['criteriaTable'];
$criteriaTargetTable = $payload['criteriaTargetTable'];
$targetTable = $payload['targetTable'];
$modelKey = $payload['modelKey'];
$modelReferenceKey = $payload['modelReferenceKey'];
$criteriaKey = $payload['criteriaKey'];
$criteriaReferenceKey = $payload['criteriaReferenceKey'];
$targetReferenceKey = $payload['targetReferenceKey'];
$targetKey = $payload['targetKey'];
$sql = 'SELECT '.$targetTable.'.'.$targetKey;
$sql.= ' FROM '.$targetTable;
$sql.= ' INNER JOIN '.$criteriaTargetTable.' ON '.$targetTable.'.'.$targetKey.' = '.$criteriaTargetTable.'.'.$targetReferenceKey;
$sql.= ' INNER JOIN '.$criteriaTable.' ON '.$criteriaTable.'.'.$criteriaKey.' = '.$criteriaTargetTable.'.'.$criteriaReferenceKey;
$sql.= ' INNER JOIN '.$modelCriteriaTable. ' ON '.$criteriaTable.'.'.$criteriaKey.' = '.$modelCriteriaTable.'.'.$criteriaReferenceKey;
$sql.= ' INNER JOIN '.$modelTable.' ON '.$modelTable.'.'.$modelKey.' = '.$modelCriteriaTable.'.'.$modelReferenceKey;
$sql.= ' WHERE '.$modelTable.'.'.$modelKey.' = "'.$modelKeyValue.'"';
$sql.= ' GROUP BY '.$targetTable.'.'.$targetKey;
// $GLOBALS['log']->debug('ConditionalLinkMultiple Entry Point sql:', [$sql]);
// execute the sql command
$pdo = $this->getEntityManager()->getPDO();
$data = $pdo->query($sql)->fetchAll();
$response = [];
foreach($data as $row){
$response[] = $row['id'];
}
// return data set
echo(json_encode($response));
break;
default:
//code to be executed if $action is different from all labels;
}
}
}
For more on debugging PHP code see this post https://forum.espocrm.com/forum/deve...gging-php-code
Step 6
The final step is to clear cache and rebuild the Espo instance
This tutorial does not cover the layout specifications for the main ("Work Order") entity, which will depend on your preferences, but in our case the modified Work Order entity detail display looks like this:
Comment