Setting the value of a Related Field in

Hello as I detailed in this post I want to access the information from other modules and put them into the fields. My task is to automatically fill the Accounts field, which takes information from the Accounts Module. I am using Suitecrm 8.4

My question is: What kind of value do I have to return to the field?
So far I have tried passing the id, name and the Bean itself back as a return value none of them have worked so far.


Example what I mean once the Projektpositionen gets filled the Accounts field should automatically get its value.
My code:

<?php

namespace App\Extension\eks\modules\eks_maintenance\Service\Fields;

use ApiPlatform\Core\Exception\InvalidArgumentException;
use App\Process\Entity\Process;
use App\Process\Service\ProcessHandlerInterface;
use BeanFactory;
use SugarBean;
use Sugarcrm\Util\LoggerManager;
use App\Engine\LegacyHandler\LegacyHandler;


class SearchContact extends LegacyHandler implements ProcessHandlerInterface
{
    protected const MSG_OPTIONS_NOT_FOUND = 'Process options are not defined';
    public const PROCESS_TYPE = 'search_contact';
    public const HANDLER_KEY = 'search_contact';

    public function getProcessType(): string
    {
        return self::PROCESS_TYPE;
    }
    public function requiredAuthRole(): string
    {
        return '';
    }

    public function getRequiredACLs(Process $process): array
    {
        return [];
    }

    public function configure(Process $process): void
    {
        $process->setId(self::PROCESS_TYPE);
        $process->setAsync(false);
    }

    public function validate(Process $process): void
    {
    }

    public function getContact($itemid){
        $this->init();
        $maintenancebean = BeanFactory::getBean('eks_maintenance',$itemid);
        $maintenancebean -> load_relationship('eks_project_position_eks_maintenance_1');
        $maintenancebean -> load_relationship('eks_project_position');
        $projectpositionid = $maintenancebean -> eks_project_position_eks_maintenance_1 -> get(); // projectid[0] ist der Wert der Relation
        // Wert der Relation ca9273ac-7989-a470-f051-653a95b7f804
        $projectpositionbean = BeanFactory::getBean('eks_project_position',$projectpositionid[0]);
        $projectpositionbean -> load_relationship('eks_project');
        $projectpositionbean -> load_relationship('eks_project_position_eks_projects_1'); // do liegt der Hund begraben us irgend einem Grund findet es bei eks_project_position_eks_projects_1 keine Relation obwohl des us da Vardef kut
        $projectid = $projectpositionbean -> eks_project_position_eks_projects_1 -> get(); // nvmd i bin an schlampiger kopierer
        $projectbean = BeanFactory::getBean('eks_projects',$projectid[0]);
        $projectbean -> load_relationship('eks_projects_accounts_3');
        $accountid = $projectbean -> eks_projects_accounts_3 -> get();
        $accountbean = BeanFactory::getBean('Accounts',$accountid[0]);
        $this->close();
        return $accountbean->id;

    }
    public function run(Process $process)
    {
        $options = $process->getOptions();
        $record = $options['record'];
        $recordid = $record['id'];
        $attributes = $record['attributes'];
        $value = $attributes['eks_project_position_eks_maintenance_1_name']['name'];
        $maintenanceid = $attributes['eks_project_position_eks_maintenance_1_name']['id'];
        $contactid = $this->getContact($recordid);
        $responseData = [
            'value' => $contactid
        ];

        $process->setStatus('success');
        $process->setMessages([]);
        $process->setData($responseData);
    }

    public function getHandlerKey(): string
    {
        return self::HANDLER_KEY;
    }
}

Hey @dwaltsch,

From this comment it looks like relate fields aren’t supported on update-value-backend logic:

On the docs it says:

At the moment this does not work for relate fields

Though, maybe there is another way to do it without using field ‘logic’ entry

1 Like

Thanks for the reply guess I’ll try to save the data directly to the bean and update to see if it works

There is a bit of a hack I have discovered :smiley:

If you place the id field of the relate field on the page not sure what you r accounts relate field is called but say its somehting like this:

accounts_c

if you check your vardefs you will have a field like accounts_id_c, this holds the ID of the related account.

So put that on the page somewhere (a tab that you never view for example). Then add your Process logic to that field (so it does your backend logic and that returns just the ID into this field). When you save, it will save everything with your linked account. It doesnt update the nice picker box live (it will when you save), however this field will be updated automatically if you use the picker box.

This will all need to be done in your detailviewdefs.php file obviously.

(hope that makes sense)

Regards
Mark

2 Likes

In SuiteCRM, we need to automatically populate a related field using backend logic.
When an Account is selected, we want the Primary Contact of that Account to be automatically set in a related Contact field.
Currently, implementing this through the backend process is becoming difficult.
What is the proper way to achieve this in SuiteCRM?

I would do this with workflow. That’s the easiest. In suitecrm 7 I would use Smarty or the built in search feature and relate one field to another. In SuiteCRM 8 not sure how to do on the front end. Only via workflow or before save hook in the back end.

I am not sure if something like this could work:

it’s the same kind of field dependency, but in your case you want to fill a value. Have you tried something similar?

Thank you for your response.

However, the suggested solution does not work for the related field. We had already posted this query a couple of months ago, but the issue still persists.

It is currently very difficult to manage and work with related fields. We would appreciate proper guidance or an alternative solution to handle this functionality effectively.

@clemente.raposo

What exactly is the logic you are trying to implement?

Here’s how I would do it for as a before save hook in the back end.

In this example the field “support level” from the account is going to populate the “support level” in the case.

class before_save_class
    {
        function before_save_method($bean, $event, $arguments)
        {

           $account = new Account(); // So this links $account to the Accounts module?
           $account->retrieve($bean->account_id); // This is the label of the field in Accounts that has the account name - which is what we search for in Cases when creating a new case, so once this account is selected, the below should be filled in?

           // $bean is your current record being saved. 
           // $account is the related account.
           $bean->support_level_c = $account->support_level_c; // These are the field in Accounts that I want to be shown in Cases once the Account has been selected (before saving the case)


        }
   }