Copy relationships between modules

Hello

I’ve created a custom module ‘Party Category’ to customize my quotes and invoices depending on the chosen category. I’ve created a one-to-many relationship between these modules and my custom module, which all works fine, the only problem is that I have to manually select my Party Category everytime.

I managed to edit /custom/modules/Leads/metadata/convertdefs.php to include a box to select my party category when I convert the lead, but when I open the opportunitie afterwards the field is empty, no relationship made, what did I do wrong?


$viewdefs['Opportunities']['ConvertLead'] = array(
    'copyData' => true,
    'required' => false,
    'default_action' => 'create',
    'templateMeta' => array(
        'form'=>array(
            'hidden'=>array(
            )
        ),
        'maxColumns' => '2', 
        'widths' => array(
            array('label' => '10', 'field' => '30'), 
            array('label' => '10', 'field' => '30'),
        ),
    ),
    'panels' =>array (
        'LNK_NEW_OPPORTUNITY' => array (
            array (
                'name',
                'currency_id'
            ), 
            array (
                'sales_stage',
                'amount'
            ),
            array (
                'date_closed',
                ''
            ),
            array (
                'dj_feestcategorieen_opportunities_1_name',
		'fp_event_locations_opportunities_2_name'
            ),
            array (
                'description'
            ),
        )
    ),
);

It’s the same story when I create a quote for the opportunity, I have to manually select my party category in the quotes screen … But I wouldn’t know where to edit, there is no convertfile in the opportunity-module folder.

Lastly I tried editing the ConvertToInvoice.php-file to copy from quote to invoice, but that didn’t work either:



    if (!(ACLController::checkAccess('AOS_Invoices', 'edit', true))) {
        ACLController::displayNoAccess();
        die;
    }

    require_once('modules/AOS_Quotes/AOS_Quotes.php');
    require_once('modules/AOS_Invoices/AOS_Invoices.php');
    require_once('modules/AOS_Products_Quotes/AOS_Products_Quotes.php');

    global $timedate;
    //Setting values in Quotes
    $quote = new AOS_Quotes();
    $quote->retrieve($_REQUEST['record']);
    $quote->invoice_status = 'Invoiced';
    $quote->total_amt = format_number($quote->total_amt);
    $quote->discount_amount = format_number($quote->discount_amount);
    $quote->subtotal_amount = format_number($quote->subtotal_amount);
    $quote->tax_amount = format_number($quote->tax_amount);
    if ($quote->shipping_amount != null) {
        $quote->shipping_amount = format_number($quote->shipping_amount);
    }
    $quote->total_amount = format_number($quote->total_amount);
    $quote->dj_feestcategorieen_aos_quotes_1 = dj_feestcategorieen_aos_quotes_1;


    $quote->save();

    //Setting Invoice Values
    $invoice = new AOS_Invoices();
    $rawRow = $quote->fetched_row;
    $rawRow['id'] = '';
    $rawRow['template_ddown_c'] = ' ';
    $rawRow['quote_number'] = $rawRow['number'];
    $rawRow['number'] = '';
    $dt = explode(' ', $rawRow['date_entered']);
    $rawRow['quote_date'] = $dt[0];
    $rawRow['invoice_date'] = date('Y-m-d');
    $rawRow['total_amt'] = format_number($rawRow['total_amt']);
    $rawRow['discount_amount'] = format_number($rawRow['discount_amount']);
    $rawRow['subtotal_amount'] = format_number($rawRow['subtotal_amount']);
    $rawRow['tax_amount'] = format_number($rawRow['tax_amount']);
    $rawRow['date_entered'] = '';
    $rawRow['date_modified'] = '';
    $rawRow['dj_feestcategorieen_aos_invoices_1'] = $rawRow['dj_feestcategorieen_aos_quotes_1'];
    if ($rawRow['shipping_amount'] != null) {
        $rawRow['shipping_amount'] = format_number($rawRow['shipping_amount']);
    }
    $rawRow['total_amount'] = format_number($rawRow['total_amount']);
    $invoice->populateFromRow($rawRow);
    $invoice->process_save_dates =false;
    $invoice->save();

Any help would be much appreciated.

Check this link:

http://cheleguanaco.blogspot.com/2012/03/sugarcrm-cookbook-adding-related.html

Thanks for the help, this is what I got:

require_once('modules/Opportunities/Opportunity.php');
$rel_name = 'DJ_Feestcategorieen';
$record_id = $dj_feestcategorieen_opportunities_1; 
$bean->load_relationship($rel_name); 
$bean->$rel_name->add($record_id);

two questions though:

  • Do I add this in /custom/modules/Leads/metadata/convertdefs.php ?
  • How do I set the $record_id

from /opt/bitnami/apps/SuiteCRM/htdocs/custom/modules/Opportunities/Ext/Vardefs/vardefs.ext.php

$dictionary["Opportunity"]["fields"]["dj_feestcategorieen_opportunities_1"] = array (
  'name' => 'dj_feestcategorieen_opportunities_1',
  'type' => 'link',
  'relationship' => 'dj_feestcategorieen_opportunities_1',
  'source' => 'non-db',
  'module' => 'DJ_Feestcategorieen',
  'bean_name' => 'DJ_Feestcategorieen',
  'vname' => 'LBL_DJ_FEESTCATEGORIEEN_OPPORTUNITIES_1_FROM_DJ_FEESTCATEGORIEEN_TITLE',
  'id_name' => 'dj_feestcategorieen_opportunities_1dj_feestcategorieen_ida',
);

You should create an create an after save logic hook on the custom folder of Leads.

Here is the documentation for logic hooks: https://docs.suitecrm.com/developer/logic-hooks/