Get leads converted custom module ID

Hello

When we converted lead to contact, accounts, opportunity records we also have taken those created ID in database lead table in contact_id account_id, opportunity_id
Also, we have a new record in WorkFlow as contact id, account id, opportunity id so we can easily detect and do workflow process…

So I wanna add a new Custom module to this process I wrote above

I investigate how it is working right now for standard modules Contacts, Accounts, Opportunity and trying to add my custom module like copy paste…

What I did:

  1. for appear my custom module in workflow list go to modules/Leads/vardefs.php and

     'people_id' =>
         array(
             'name' => 'people_id',
             'type' => 'id',
             'reportable' => false,
             'vname' => 'LBL_PEOPLE_ID',
             'comment' => 'If converted, people ID resulting from the conversion'
         ),
     'people' => array(
         'name' => 'people',
         'type' => 'link',
         'link_type' => 'one',
         'relationship' => 'people_leads',
         'source' => 'non-db',
         'vname' => 'LBL_PEOPLE',
         'reportable' => false,
     ),
    

The result looks like

  1. for appear my custom module in convert list go to modules/Leads/metadata/convertdefs.php
    and add records of my custom module

     $viewdefs['people']['ConvertLead'] = array(
         'copyData' => true,
         'required' => false,
         'templateMeta' => array(
             'form'=>array(
                 'hidden'=>array(
                 )
             ),
             'maxColumns' => '2',
             'widths' => array(
                 array('label' => '10', 'field' => '30'),
                 array('label' => '10', 'field' => '30'),
             ),
         ),
         'panels' =>array(
             'LNK_NEW_PEOPLE' => array(
                 array(
                     'name',
                 ),
                 array(
                     'description'
                 ),
             )
         ),
     );
    

ok, right now I can create a new record in my custom module BUT it’s not written ID of created a new record anywhere so the next step

  1. Go to modules/Leads/Lead.php and define

public $people_id;
public $people_name;

then I found get_contact(account, opportunity) functions and make another one with my custom module name (and adding logger to see in logs if it’s return anything)

public function get_people()
    {

        if (isset($this->people_id) && !empty($this->people_id)) {
            $query = "SELECT name, assigned_user_id people_name_owner FROM people WHERE id='{$this->people_id}'";

            $GLOBALS['log']->fatal('People ID in get_people(global): ', $this->people_id, $people_id );
            LoggerManager::getLogger()->fatal('People ID in get_people(logger): ', $this->people_id);

            //requireSingleResult has beeen deprecated.
            //$result = $this->db->requireSingleResult($query);
            $result = $this->db->limitQuery($query, 0, 1, true, "Want only a single row");

            if (!empty($result)) {
                $row = $this->db->fetchByAssoc($result);

                if (!is_null($row) && !is_bool($row)) {
                    $this->people_name = $row['name'];
                    $this->people_name_owner = $row['people_name_owner'];
                } else {
                    $this->people_name = null;
                    $this->people_name_owner = null;
                }

                $this->people_name_mod = 'People';
            }
        }
    }

also, there is Convert_lead function where I just add $peopleid

    public function converted_lead($leadid, $contactid, $accountid, $opportunityid, $peopleid)
    {
        $query = "UPDATE leads set converted='1', contact_id=$contactid, account_id=$accountid, opportunity_id=$opportunityid, people_id=$peopleid where  id=$leadid and deleted=0";
        $this->db->query($query, true, "Error converting lead: ");

        //we must move the status out here in order to be able to capture workflow conditions
        $leadid = str_replace("'", "", $leadid);
        $lead = BeanFactory::newBean('Leads');
        $lead->retrieve($leadid);
        $lead->status='Converted';
        $lead->save();

        $GLOBALS['log']->fatal('People ID in converted_lead(global): ',$contactid, $peopleid);
        LoggerManager::getLogger()->fatal('People ID in converted_lead(logger): ',$contactid, $peopleid);

    }

It’s all that I found relate to $contactid, $accountid, $opportunityid
My current result when I did test converting was - creating a new my custom module record BUT it’s still not writing created record ID in database Lead
As you can see on the screenshot it sees, like writing in people_id just empty value (overwrite NULL)

the logger also shows me a empty value.
What could I be missing?
how can I display the ID of the created record and write it to the database

I don’t understand all the details in your issue, but a preliminary question - it seems you’re using the logger to track your code, why not do it like a pro and use XDEBUG and an IDE like PhpStorm or Eclipse? It will take your coding skills to a whole new level.

thanks for the advice,
I thought I can just modify lead module a little for adding my custom module it seems like I need a little modify code - just add 2 new function like already existing function for contacts, accounts, opportunity modules and it seems like working BUT in the result, I sow that in MySQL in lead table column for my custom module_id wrote empty result it was the reason why I just used the logger…

But now I’m going to use XDebug with PhpStorm

About my issue maybe I can give some compact details:
Right now Leads module after converting placed the converted account, contact,opportunity records(if there was converted) in MySQL.
If we go to Mysql-Lead. We can see account_id, contact_id, opportunity_id
Those column use to relate lead and converting account, contact, opportunity records from this Lead

My question is how I can add for this my custom module…

I’m sorry, but I don’t know the specific answer regarding the custom module…

But please be careful not to make too many changes that are not upgrade-safe, otherwise you will have trouble managing your system in the future. Try to make all changes in custom folder (which sometimes is not easy and involves many different tricks).

The debugger will surely help you sort out your issues. It’s different when you can stop the code, step through it, and inspect all variable values. You learn SuiteCRM much faster.

1 Like

I’m trying to debug converted_lead function in modules/leads/lead.php
But even if I use XDebug I can’t catch this function, maybe you know how it can be called?
I tried to use a breakpoint also just use debug mode without a special breakpoint and just see how code execute step by step and I can’t catch this moment in function and how function calling:

$query = "UPDATE leads set converted='1', contact_id=$contactid, account_id=$accountid, opportunity_id=$opportunityid, people_id=$peopleid where  id=$leadid and deleted=0";
        public function converted_lead($leadid, $contactid, $accountid, $opportunityid, $peopleid)
        {
            $query = "UPDATE leads set converted='1', contact_id=$contactid, account_id=$accountid, opportunity_id=$opportunityid, people_id=$peopleid where  id=$leadid and deleted=0";
            $this->db->query($query, true, "Error converting lead: ");
            
            //we must move the status out here in order to be able to capture workflow conditions
            $leadid = str_replace("'", "", $leadid);
            $lead = BeanFactory::newBean('Leads');
            $lead->retrieve($leadid);
            $lead->status='Converted';
            $lead->save();
            
        }

As far as I can tell, it isn’t being called anywhere!..

hmm, okay, at first I thought I could add my own custom module and at first look in code it looked possible, but now I don’t understand how to do it if I can’t change / see where the function is called to update
$query = "UPDATE leads set converted='1', contact_id=$contactid, account_id=$accountid, opportunity_id=$opportunityid, people_id=$peopleid where id=$leadid and deleted=0";

Maybe I am going the wrong way and my problem can be solved differently, but now I have no idea
Maybe it’s kind of logic hook that will update smth like “UPDATE leads set people_id=$peopleid where id=$leadid and deleted=0”;`
but i don’t know how to do it now
Maybe I can get some advice here

Maybe what you’re looking for, a place to set a breakpoint, is in modules/Leads/LeadFormBase.php, function handle_save.

But yeah, doing your own stuff in a logic_hook is a good solution, it’s more upgrade-safe.

Possibly, the reason why you’re not seeing a place where the code sets converted in database to 1 is because it’s coming as a fixed value from the HTML form, and gets packaged together with all the other fields and values.

@pgr
after all, I got it worked

It needs to add one-to-many relationship between custom module (People in my case) and Leads
Also, I rebuilded this a new created relationship with this guide http://www.jsmackin.co.uk/suitecrm/suitecrm-nice-relationships/
BUT for some reasons, this guide has some Crucial bug/issue
Let’s point all cases I found and fix all of this

  1. Building SELF module one-to-many relationship:
  • For appear subpanel need to add the relationship definition to

custom/Extension/modules/SELF/Ext/Vardefs/SELF_SELF_1_SELF.php

$dictionary["SELF"]["relationships"]['SELF_SELF_1'] = array(
    'lhs_module' => 'SELF',
    'lhs_table' => 'SELF',
    'lhs_key' => 'id',
    'rhs_module' => 'SELF',
    'rhs_table' => 'SELF',
    'rhs_key' => 'id_SELF',  // -> here need place your defined column, in my case it's 'id_SELF'
    'relationship_type' => 'one-to-many',
);
  1. For one-to-many relationship between two different module

Current guide steps have not good due to broken subpanel “select button” functional To fix need a little change this code:

$dictionary["organizations"]["relationships"]['organizations_experience_1'] = array(
    'lhs_module' => 'organizations', // -> here need place Main module
    'lhs_table' => 'organizations',
    'lhs_key' => 'id',             // -> here need place 'Id'
    'rhs_module' => 'experience', // -> here need place Relate module
    'rhs_table' => 'experience',
    'rhs_key' => 'id_organization', // -> here need place your defined column, in my case it's 'id_organization'
    'relationship_type' => 'one-to-many',
);
  1. For fixing many-to-many relationship field names

Make sure you have the same file in Vardefs on both module sides, if not that just copy

custom/Extension/modules/FirstModule/Ext/Vardefs/FirstModule_SecondModule_1.php custom/Extension/modules/SecondModule/Ext/Vardefs/FirstModule_SecondModule_1.php

  1. For one-to-many SELF relationship

Create

custom/Extension/modules/organizations/Ext/Vardefs/SELF_SELF_1_SELF.php

with following code: Make sure you’re replacing SELF for your module_name

$dictionary["SELF"]["fields"]["id_SELF"] = array (
    'name' => 'id_SELF',
    'vname' => 'LBL_PARENT_SELF_ID',
    'type' => 'id',
    'required' => false,
    'reportable' => false,
    'audited' => true,
    'comment' => 'SELF ID of the parent of this SELF',
);

$dictionary["SELF"]["fields"]["parent_name"] = array (
    'name' => 'parent_name',
    'rname' => 'name',
    'id_name' => 'id_SELF',
    'vname' => 'LBL_MEMBER_OF',
    'type' => 'relate',
    'isnull' => 'true',
    'module' => 'SELF',
    'table' => 'SELF',
    'massupdate' => false,
    'source' => 'non-db',
    'len' => 36,
    'link' => 'member_of',
    'unified_search' => true,
    'importable' => 'true',
);
$dictionary["SELF"]["fields"]["members"] = array (
    'name' => 'members',
    'type' => 'link',
    'relationship' => 'member_organizations',
    'module' => 'SELF',
    'bean_name' => 'SELF',
    'source' => 'non-db',
    'vname' => 'LBL_MEMBERS',
);

$dictionary["SELF"]["fields"]["member_of"] = array (
    'name' => 'member_of',
    'type' => 'link',
    'relationship' => 'member_SELFs',
    'module' => 'SELF',
    'bean_name' => 'SELF',
    'link_type' => 'one',
    'source' => 'non-db',
    'vname' => 'LBL_MEMBER_OF',
    'side' => 'right',
);

$dictionary["SELF"]["relationships"]['member_SELFs'] = array(
        'lhs_module' => 'SELF',
        'lhs_table' => 'SELF',
        'lhs_key' => 'id',
        'rhs_module' => 'SELF',
        'rhs_table' => 'SELF',
        'rhs_key' => 'id_SELF',
        'relationship_type' => 'one-to-many'
);

Next need to define and create a file for subpanel go to custom/Extension/modules/organizations/Ext/Layoutdefs/SELF_SELF_1_SELF.php

<?php
$layout_defs["SELF"]["subpanel_setup"]['SELF_SELF_1'] = array (
  'order' => 100,
  'module' => 'SELF',
  'subpanel_name' => 'default',
  'sort_order' => 'asc',
  'sort_by' => 'id',
  'title_key' => 'LBL_SELF_SELF_1_FROM_SELF_R_TITLE',
  'get_subpanel_data' => 'members',
  'top_buttons' => 
  array (
    0 => 
    array (
      'widget_class' => 'SubPanelTopButtonQuickCreate',
    ),
    1 => 
    array (
      'widget_class' => 'SubPanelTopSelectButton',
      'mode' => 'MultiSelect',
    ),
  ),
);

Also, I have modified suitecrm/modules/Leads/vardefs.php (it’s not upgrading save)

I added this

   'people_id' =>
        array(
            'name' => 'people_id',
            'type' => 'id',
            'reportable' => false,
            'vname' => 'LBL_PEOPLE_ID',
            'comment' => 'If converted, people ID resulting from the conversion'
        ),
    'people' => array(
        'name' => 'people',
        'type' => 'link',
        'link_type' => 'one',
        'relationship' => 'people_leads_1',
        'source' => 'non-db',
        'vname' => 'LBL_PEOPLE',
        'reportable' => false,
    ),
    'people_name' =>
        array(
            'name' => 'people_name',
            'vname' => 'LBL_people_NAME',
            'type' => 'varchar',
            'len' => '255',
            'comment' => 'people name associated with lead',
        ),

Done
RIght now I have update people_id in Mysql Leads that contain a new created People record during Leads converting and it’s can be watching from Worflows (There is People ID in Leads Lable)

Nice, thanks.

Eventually this should go into the Technical blog on the docs site. Jim Mackin gave permission to post his own blog content there (all the Developer guide also comes from his book).

Once it’s in the docs site, it can be edited and corrected with contributions from the Community like this one.