How do you autopopulate a related field in custom module from a related field of another module?

I'm fighting against this issue since a couple of weeks and I can't figure out a way to do that.

I have a custom module for the orders management and I need to produce invoices automatically.
I'm following the example of 'ConverToInvoice.php' from Quotes module so, if I click on that button (under the Edit button in Detail View) I should create a new Invoice whose fields are pre-filled and  editable.
I have to grab fields' values from current order management.
My first problem is that a lot of those fields are related, too.

For example, I need to fill a related field called 'Suppliers' in my invoice. The value I need comes from a module (Suppliers) related to the Account, which is related to the current order management.

How can I easily grab that value and put it in my suppliers field?

I tryed to follow this guide  but it didn't worked.. probably I miss something.
I really need help, as soon as possible.

Thank you.

Parents
  • Cristina,
    How many Accounts are related to your OrderManagement record and how many Suppliers are related to an Account? Are these records linked via a relate field or a relationship?

    If they are just relate fields the Account on the OrderManagement can be retrieved using the bean. If $bean is your Order Management bean, and account_id is the relate field for Account on that bean.

    $account = new Account();
    $account->retrieve($bean->account_id);

    Now $account is the bean with all the Account information.
    In turn if Supplier is a relate field on Account and supplier_id is the relate field for Supplier on that bean:

    $supplier = new Supplier();
    $supplier->retrieve($account->supplier_id);

    If instead you have multiple Accounts on an OrderManangement and/or multiple Suppliers on an Account and want to retrieve them all  you can get related beans using the relationship name as described by Jeff Bickart: http://developer.sugarcrm.com/2012/03/23/howto-using-the-bean-instead-of-sql-all-the-time/

    I always find getting the right relationship name a bit tricky so I created a function that given two modules returns the relationship name:
    http://developer.sugarcrm.com/2013/05/29/programmatically-find-the-name-of-the-relationship-between-...
     
    I personally keep this, and other such functions I created, in a utilities folder for reuse but you can also simply include it in the script you are writing.

    So you can do something like (note, I'm guessing your module names).
    (assuming you have $bean = your current Order_Management bean)

    include_once('<your path to my function>/getRelashionshipByModules.php'); $suppliers = array();
    $order_to_account_relationship = getRelationshipByModules ('Order_Management', 'Accounts');
    $account_to_supplier_relationship = getRelationshipByModules('Account', 'Suppliers');
    $bean->load_relationship($order_to_account_relationship);
    //loop through the Accounts linked to the Order
    foreach($bean->$order_to_account_relationship->getBeans() as $account){
        //loop through the suppliers for this account
        $account->load_relationship($account_to_supplier_relationship);
        foreach($account->$account_to_supplier_relationship->getBeans as $supplier){        //check for duplicated by id (the key of the array)        //if you don't already have this supplier in your list, add it.
           if(!array_key_exists($supplier->id, $suppliers)) $suppliers[ $supplier->id] = $supplier->name;
       }
    }
    //the array $suppliers now contains all your suppliers with key the id of the record and value the name of the supplier.

    Note, the code above has not been tested, it's just to give you some ideas.
    Spero che questa risposta ti sia d'aiuto.

    HTH
    FrancescaS
Reply
  • Cristina,
    How many Accounts are related to your OrderManagement record and how many Suppliers are related to an Account? Are these records linked via a relate field or a relationship?

    If they are just relate fields the Account on the OrderManagement can be retrieved using the bean. If $bean is your Order Management bean, and account_id is the relate field for Account on that bean.

    $account = new Account();
    $account->retrieve($bean->account_id);

    Now $account is the bean with all the Account information.
    In turn if Supplier is a relate field on Account and supplier_id is the relate field for Supplier on that bean:

    $supplier = new Supplier();
    $supplier->retrieve($account->supplier_id);

    If instead you have multiple Accounts on an OrderManangement and/or multiple Suppliers on an Account and want to retrieve them all  you can get related beans using the relationship name as described by Jeff Bickart: http://developer.sugarcrm.com/2012/03/23/howto-using-the-bean-instead-of-sql-all-the-time/

    I always find getting the right relationship name a bit tricky so I created a function that given two modules returns the relationship name:
    http://developer.sugarcrm.com/2013/05/29/programmatically-find-the-name-of-the-relationship-between-...
     
    I personally keep this, and other such functions I created, in a utilities folder for reuse but you can also simply include it in the script you are writing.

    So you can do something like (note, I'm guessing your module names).
    (assuming you have $bean = your current Order_Management bean)

    include_once('<your path to my function>/getRelashionshipByModules.php'); $suppliers = array();
    $order_to_account_relationship = getRelationshipByModules ('Order_Management', 'Accounts');
    $account_to_supplier_relationship = getRelationshipByModules('Account', 'Suppliers');
    $bean->load_relationship($order_to_account_relationship);
    //loop through the Accounts linked to the Order
    foreach($bean->$order_to_account_relationship->getBeans() as $account){
        //loop through the suppliers for this account
        $account->load_relationship($account_to_supplier_relationship);
        foreach($account->$account_to_supplier_relationship->getBeans as $supplier){        //check for duplicated by id (the key of the array)        //if you don't already have this supplier in your list, add it.
           if(!array_key_exists($supplier->id, $suppliers)) $suppliers[ $supplier->id] = $supplier->name;
       }
    }
    //the array $suppliers now contains all your suppliers with key the id of the record and value the name of the supplier.

    Note, the code above has not been tested, it's just to give you some ideas.
    Spero che questa risposta ti sia d'aiuto.

    HTH
    FrancescaS
Children
No Data