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?
rsp
22 November 2024 00:19
4
I think Logic hook has before save options too.
Intro Logic hooks allow you to hook into various events in SuiteCRM to fire custom code. This can allow you to, for example, make a call to an external API, or to create a new record if certain events occur.
Types Logic hooks can occur in three...
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 ?
rsp
22 November 2024 00:50
6
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.
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);
}
}
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']],
],
],
],
],
),
),
Run command : php bin/console cache:clear
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?
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
salesagility:develop
← Cecropia:feature/relate-field-support-update-value-backend-action
opened 02:31PM - 28 Nov 24 UTC
## Description
This PR introduces support for updating values through b… ackend actions for relate fields in SuiteCRM 8. Previously, the system did not support updating these fields, limiting the functionality of working with relationships in modules. With this enhancement, relate fields can now be managed more comprehensively via the backend.
## Motivation and Context
The official SuiteCRM documentation previously mentioned that backend value updates were not supported for relate fields. This change aims to extend the current capabilities and ensure that relate fields are also manageable in the backend. This is particularly beneficial when creating custom logic to update the relate field.
### Specific Implementation for Relate Fields
To address the limitation of updating relate fields, the backend requires a specific response structure to ensure proper updates. The `run` method in the backend handler must provide the response data in the following format to update relate fields:
```php
public function run(Process $process)
{
$options = $process->getOptions();
$record = $options['record'];
$attributes = $record['attributes'];
$responseData = [
'value' => json_encode([
'id' => '72773dab-a9ee-e545-702a-674652570e09', // The related record ID
'name' => 'Test', // The related record name
]),
];
$process->setStatus('success');
$process->setMessages([]);
$process->setData($responseData);
}
```
## How To Test This
1. **Create or Use Existing Module**: Create a module with relate fields using the Module Builder.
2. **Apply Backend Update Logic**: Use the custom backend handler to update a relate field through the backend.
3. **Check Updates**: Ensure that the relate field updates correctly without any issues. Validate the change by reviewing the database and UI.
### Example Test:
- Create a new Contact record.
- Relate the Contact to an Account.
- Update a value in the relate Account field via backend logic using the appropriate structure.
- Verify that the change is successfully reflected in both the database and the SuiteCRM UI.
## Types of changes
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
### Final checklist
- [x ] My code follows the code style of this project found [here](https://docs.suitecrm.com/community/contributing-code/coding-standards/).
- [x ] My change requires a change to the documentation.
- [x ] I have read the [**How to Contribute**](https://docs.suitecrm.com/community/contributing-code/) guidelines.
1 Like