Listen to the related combo selection event

I am working in the Cases module and need to automatically populate other form fields when an item is selected in a combo box (relational field in SuiteCRM 8) that data comes from the DB. According to the official documentation, this could be achieved using updateValueBackend, but it also mentions that it currently does not work for relational fields.

Do you have any ideas or suggestions on how I could implement this functionality? I would greatly appreciate any guidance

If you don’t need it to happen on the front end, I’d suggest a logic hook. You could do an after relationship add hook.

Hi @pstevens, Thank you for your reply
Correct, the hook doesn’t work for me because I need it to execute on the frontend when a value is selected in the relational field to populate the other fields, but at this point, the record hasn’t been saved yet.

Is there a way to detect the selected value in Angular and send a request to the server, or something similar?

I think Logic hook has before save options too.

Hi @rsp
the event occurs in the frontend when the record has not been saved then there is no way to execute the hook.
when the user selects an item from a relational field fill the other fields in the create formmulary.
what hooks do you see there ?

Oh, I do not know if that is possible.

In SuiteCRM 7 I would do it easy with JS but I have no idea how to do this in 8.

Hello, I am able to update a field that depends upon a relate field. Here are the steps to implement this.

  1. Added a process handler class with the following definition in {ROOT}\extensions\defaultExt\modules\Cases\Process\Service\Fields\UpdateDescription.php
<?php

namespace App\Extension\defaultExt\modules\Cases\Process\Service\Fields;

use ApiPlatform\Core\Exception\InvalidArgumentException;
use App\Process\Entity\Process;
use App\Process\Service\ProcessHandlerInterface;

class UpdateDescription implements ProcessHandlerInterface {
	
	protected const MSG_OPTIONS_NOT_FOUND = 'Process options are not defined';
    protected const MSG_INVALID_TYPE = 'Invalid type';
    public const PROCESS_TYPE = 'update-description';
	
    public function __construct()
    {
			
    }
	
    public function getProcessType(): string
    {
        return self::PROCESS_TYPE;
    }

    public function requiredAuthRole(): string
    {
        return 'ROLE_USER';
    }
	
	public function getRequiredACLs(Process $process): array
    {
        $options = $process->getOptions();
		$module = $options['module'] ?? '';
		
		$id = $options['id'] ?? '';
		
		$editACLCheck = [
			'action' => 'edit',
		];
		
		if($id != ''){
			$editACLCheck['record'] = $id;
		}
		
		return [
			$module => [
				$editACLCheck,
			],
		];
    }	
	public function configure(Process $process): void
    {
        $process->setId(self::PROCESS_TYPE);
        $process->setAsync(false);
    }	
	public function validate(Process $process): void
    {
        $options = $process->getOptions();
		$record = $options['record'];
		$attributes = $record['attributes'];
		$name = $attributes['name'] ?? '';
		
		if(empty ($name)){
			
			throw new InvalidArgumentException(self::MSG_OPTIONS_NOT_FOUND);
		}
    }	
	public function run(Process $process)
    {
		$options = $process->getOptions();
		$record = $options['record'];
		$attributes = $record['attributes'];
		$account = $attributes['account_name'] ?? '';
		//$account_object = json_encode($account);
		$description= 'updated description with account 
				name as '. $account['name']. ' and id = '. $account['id'];
		
        $responseData = [
            'value' => $description,
        ];

        $process->setStatus('success');
        $process->setMessages([]);
        $process->setData($responseData);
    }	
}
  1. Update ‘description’ field with the following logic code in {ROOT}\public\legacy\custom\modules\Cases\metadata\detailviewdefs.php

‘account_name’ is an existing field of type ‘relate’

        4 =>
        array(
          0 =>array('name' => 'description',
			'logic' => [
				'account_name_descr' => [
					'key' => 'updateValueBackend',
					'modes' => ['edit','create'],
					'params' => [
						'fieldDependencies' => ['account_name'],
						'process' =>'update-description',
						'activeOnFields' => [
							'account_name'=> [['operator' => 'not-empty']],
						],
					],
				],
			],
		  ),		  
        ),
  1. Run command : php bin/console cache:clear
  2. Output:

Please let me know if this is helpful. Any improvements or suggestions are most welcome.

3 Likes

Hi @Harshad

Thank you very much for your response. I just tested it, and it works perfectly—many thanks! However, I have a question: the code updates all the fields except for those of the “related” type. Some of the fields I need to populate are of this type, and even though I send the value’s ID from the backend, it doesn’t seem to recognize it. Do you have any recommendations?
image

Thanks @hipolito, I also tried to set the values of relate field, but failed. This is what the documentation says about the relate field updates. I am trying another approach and will share.

1 Like

Hi @Harshad
I have explored other approaches to achieve this, but none have been successful. If you manage to find a solution, I would greatly appreciate your assistance. Thank you very much.

In SuiteCRM 8 the UpdateValueBackend actions have no support for related fields. I have made a PR for adding this feature which allows UpdateValueBackend actions to update the related fields.

I hope this feature gets added to the SuiteCRM core repo

1 Like