Update RecordFinder value with dependsOn a different recordFinder

I have 3 models. Company, Deal, Agent

An agent can belong to both a company and a deal. And a company has many deals.

I have 2 record finders on the deal form.

# deal/fields.yaml

company:
  label: Company
  type: recordfinder

agent:
  label: Agent
  type: recordfinder
  dependsOn: company

I want to update the Deal Agent field based on the selected Company’s Agent

I assume this should be done using dependsOn and use the filterFields() method on the Deal model. But for some reason, this doesn’t seem to be working for me.

// Deal.php

public function filterFields($fields, $context = null) {
    // I'm getting the company here. So this is all good.
    $company = Company::find($fields->company->value);

    // But I'm struggling to actually set the agent on the deal.
    // Neither of these work for me.
    $fields->agent->value = $company->agent;
    $fields->agent->value = $company->agent->id;
}

Am I missing something?

I have a similiar scenario and use the changeHandler method:

Works well!

Thanks @marco.grueter. I had considered this approach also, but still feel like we should be able to set the value of a record finder in the filterFields() method.

Hey @neil-impelling

This looks like a bug/oversight. We’ve added support for this in v3.6.28. The correct approach should be:

$fields->agent->value = $company->agent->id;

A test has been added to the test plugin here:

I hope this helps.

3 Likes

Nice. Thanks man. Great work as always :+1:

1 Like