Field Mapping from customers module to a custom module

@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.

@lucid
It work not correct:

  • the logichook “after_relationship_add” work for all modules wiche have relationship with “Moves_Moves”.
  • the field “$bean->moves_moves_accounts” isn’t id type, it’s link type.

I see a few ending in ida, idb in the cache for relationships, what does an ID usually look like?

It is may be them. There is documentation: https://docs.suitecrm.com/developer/vardefs/#_defining_vardefs
and you can look at files: vardefs.php .

ok I think I sorted that part out, but that doesn’t fix the error that it doesn’t recognize

$bean_account = new Accounts()

nor

$bean_customer = new Customers();

So the linking wouldn’t work because it happens afterwards

Hi again, I was working on another project in the meantime. Just coming back to this issue now. I searched vardefs and found an id_name for the relationship between the customers module and the moves_moves module. When I use the singular ‘$bean_account = new Account;’ I do not get any errors, but I’m still stuck with what could be going wrong here.

$relateID = 'moves_moves_accountsaccounts_ida'; $bean_account = new Account(); $bean_account->retrieve($relateID); $bean->movescustomerphone = $bean_account->phone_office;

So now I have no errors in my PHP file, but it still confuses me why the customer’s phone number isn’t there. Is there a way I can see if this retrieve function is even pulling anything? I’m assuming it’s just making a new null Account and not filling any of the fields from the database. I don’t know how to proceed at this point, tried a lot of methods to get this done and it seems very simple but nothing is working.

@lucid
“moves_moves_accountsaccounts_ida” isn’t id of module. It is id of relationship. I can’t help you if I don’t see naming of your fields.