How to update multi-select field on screen during editing of new record

I have a custom module with 3 sub-modules. 2 of them have identical multi-select fields named “testing”. The third module includes a one to many relationship with the second module. Once that relationship is added, an after_relationship_add triggers the following code:

<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class EquipmentUpdate
{
        function TestingLink($bean, $event, $arguments)
	{
	     if($arguments['related_module'] == "INV_Inv_Type") {
		$bean->testing = $arguments["related_bean"]->testing;
        	}
    	}
}
?>

which sets the 3rd module testing field equal to the 2nd module testing field. This works correctly and as soon as I hit save, the inherited testing information shows up on the screen. I would like to have the testing information populate as soon as the relationship is added.

Most of the information about refreshing on-screen values is based on a custom controller. However, unless I am mistaken, that won’t work here because the record hasn’t been saved yet so there is no record to retrieve. So I am thinking I need to add the js directly to the php function called by after_relationship_add but I am not sure what the right code is.

The closest thing I have found is:

	$model = BeanFactory::getBean($_REQUEST["module"], $_REQUEST["record"]);
	$field_to_update = "testing";
	$field_value = $model->testing;
	$js=<<<EOQ
	<script>
		$( document ).ready(function() { 
			document.querySelector('div[type="enum"][field="$field_to_update"]').innerHTML = "$field_value";
		});
	</script>
	EOQ;
	echo $js;

but I haven’t been able to figure out how to make it work with a multi-select field and I noticed that the viewdefs have specific code for multi-select fields so perhaps I need to be referencing a standard function rather than modifying the innertext but I don’t know what it is.

Thanks a lot
SuiteCRM version 7.11.15

@steveS

If I understand correctly all relationships of modules.
May be you should add to your hook ‘after_relationship_add’ the code:

$bean->save();

I think that it’s hook for modules with many relationships

I guess I could do that though it wouldn’t be my first choice. That would make this module inconsistent with all others in that you would not have to hit the save button to save the current record. Since this is a new record you would lose the ability to cancel once that relationship was added. I would prefer it just update the field on the page and the user would still have to click the save button to actually save it but if that’s not possible I guess I can just save it.

@steveS
If you want copy data to new record when you push on

  1. ‘create’ button in subpanel:
  • Make the special button file custom/include/generic/SugarWidgets/SugarWidgetSubPanelTopButtonQuickCreate.php. See files in directory include/generic/SugarWidgets for exmples.
  1. button select in edit form:

This is great. Thank you. I was able to reproduce the example and then use it for my module but I have only been able to get it to work with text fields. Would I have to do something different for a multi-select? Thanks again.

@steveS
I see no limits if you will work with objects of modules and will not use data from DB directly.
If I will have free time I will test it.

wondering if anybody has figured out how to populate a multi-select on screen directly from the DB?

Thanks.