Field Mapping from customers module to a custom module

I have a custom module set up that takes information about a service we provide, and would like to be able to have my ‘customer phone’ field mapped from the customer module, would this be possible through workflow, relationships, or would I need to add custom code for that to happen?

@lucid
It is possible. Only you should make field ‘customer phone’ in your ‘custom module’.

I have that part, all the fields are created how they should look. I was wondering how the code would look for logic hooks to be able to select a customer and have it auto-fill in the customer phone field

@lucid
I wrote you in other post: Selecting a user that you would like to allow to see a document
If you want to use logichook, I recommend to use ‘after_save’. You can look at the example here:
Related Field not work

ok, thank you. I’ll try it out and let you know how it goes

1 Like

Hi again, I tried out the method that you linked me to and was having an issue. Here is my code and a few screenshots of what’s going on.

Logic Hook (I added it in the correct place after the after_save Array was made):

$hook_array[‘after_save’][] = Array(1, ‘’, ‘custom/modules/Customers/CopyPhone.php’, ‘PhoneGrab’, ‘copy’);

CopyPhone.php (excluding opening and closing php tag):

class PhoneGrab{
function copy(&$bean, $event, $arguments){
$moves_bean = new Moves_Moves();
$moves_bean->retrieve($bean->accounts_moves_moves_1);
$moves_bean->customerphone_c = $bean->customerphone_c;
}
}

I made new fields in studio before trying out the code, as well as set a new one to one relationship between my custom module “Moves_Moves” and the module “Customers”. The field being named customerphone_c in both modules, and the relationship being called “accounts_moves_moves_1”. I also tried adding the customer to the move from both inside the editview of the moves module, and the editview of the customers module, thinking it might’ve been backwards the way I edited the code.

Anyways, it’s kind of strange that in the customer module is shows that I’ve selected this move, but the logic hook hadn’t acted on the field in the moves module. Any direction I should take from here? (screenshots of the relationship linking them, but the field not filled below)

@lucid
It’s correct but you should call the function “save”.

class PhoneGrab{
    function copy(&$bean, $event, $arguments){
        $moves_bean = new Moves_Moves();
        $moves_bean->retrieve($bean->accounts_moves_moves_1);
        $moves_bean->customerphone_c = $bean->customerphone_c;
        /* add code */
        $moves_bean->save();
    }
}

Just tried that out and it put me into a save loop after saving the entry into moves module, how could i stop that from happening?

@lucid
I looked at you code and screenshots more carefully. I see any problems and want to ask some question:
When do you want copy phone: after save record in module “Moves_Moves” or “Customers” or both time?

both would be nice, but I only really need it after I save the record in the ‘Moves_Moves’ module. When you fill it out it assumes you have a customer record ready to select.

@lucid
At first about save the record in the ‘Moves_Moves’, I recommend another way. Look at posts:


and

@lucid
At second about save the record in the ‘Customers’.
You should make the file logic_hook.php in
custom/modules/Customers/
If I correctly understand your the file logic_hook.php in
custom/modules/
and it work for all modules.
The code of your function ‘copy’ should be another:

class PhoneGrab{
    function copy(&$bean, $event, $arguments){
        $moves_beans=$bean->get_linked_beans('accounts_moves_moves_1', 'Moves_Moves');
        foreach ($moves_beans as $moves_bean) {
            $moves_bean->customerphone_c = $bean->customerphone_c;
            $moves_bean->save();
        }
    }
}

@lucid
Third, if you don’t want to follow my first recommendation, you can use the next logic hook. Attention it is “before_save”
the file custom/modules/Moves_Moves/logic_hooks.php

$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1,'Before save','custom/modules/Moves_Moves/PhoneGrab.php','PhoneGrab','copy');

the file custom/modules/Moves_Moves/PhoneGrab.php

class PhoneGrab{
    function copy(&$bean, $event, $arguments){
        $bean_customer = new Customers();
        $bean_customer->retrieve(<id_of_link_from_Customers_to_ Moves_Moves >);
        $bean->customerphone_c=$bean_customer-> customerphone_c;
    }
}

Hi again, I tried both auto-populating and the before_save logic hook and didn’t have luck with either. I’m leaning towards using logic hooks because I would like to practice using them before using APIs that will take requests with logic hooks. I tried the most recent logic hook example you replied with but I’m still having a problem with when I select a customer the field doesn’t fill in, and when I try to use the save button it just shows me a white page and when I refresh the record isn’t saved. Any help?

You’re probably just getting a PHP error.

In php.ini there’s an entry error_log, check that file for any messages at the time of your white screen.

@lucid
Do you replace this text to the real id field?

was looking into that, I can’t seem to find what the name of the ID is when calling it in code

@lucid
There is full information

  • about fields in files:
    cache/modules/<module_name>/<object_name>vardefs.php
  • about relationships:
    cache/Relationships/relationships.cache.php

You should check all fields name.

I found something interesting when looking at this problem, the module ‘customers’ seems to be a renamed version of what was originally ‘accounts’, since the relationship doesn’t mention customers and there are no folders set up under the name ‘customers’. But in studio it shows under than name. I tried using both

$account_bean = new Accounts();

and

$customers_bean = new Customers();

both giving me an error in error.log in my apache2 folder stating ‘Uncaught Error: Class ‘Customer’ not found in PhoneGrab.php’ or ‘Accounts’, depending on which one I use. There may be an issue with the way that I am relating them, but here are a couple screenshots of my relationship, and the fields and see if anything it striking out at you as wrong.

This is the phone field for the Moves_Moves module
movescustomerphone moves_moves module
This is the phone field for the Customers module
phone_office customer module
This is the Many to One relationship, relative to the Moves Module. So Many moves per one customer.
relationships

The code I’m using I changed from before_save and after_save, to after_relationship_add.

logic_hooks.php, from inside /custom/modules/Moves_Moves/

$hook_array[‘after_relationship_add’] = Array();
$hook_array[‘after_relationship_add’][] = Array(1, ‘copy phone hook’, ‘custom/modules/Moves_Moves/PhoneGrab.php’, ‘PhoneGrab’, ‘copy’);

PhoneGrab.php, /custom/modules/Moves_Moves

class PhoneGrab{
function copy(&$bean, $event, $arguments){
$customer_bean = new Customers();
$customer_bean->retrieve($bean->moves_moves_accounts);
$bean->movescustomerphone = 1234;
}
}

With this code I was just trying to attempt to set the field with code, which it hasn’t worked. Sorry if this seems super drawn out, just trying to make sure I get it right before we try to build on top of it.

If I remove

$customer_bean = new Customers();
$customer_bean->retrieve($bean->moves_moves_accounts);

and just leave

$bean->movescustomerphone = 1234;

Once I click on a customer the field autofills with 1234, so this seems like a relational issue. And I think it’s having to do with the creating of a new Object for the Customer. But I checked the cache for field names and relationship names and they look right to what I was using. I tried using ID but maybe I had not used it correctly.