HOWTO: Convert a record of one module type to a record in another

bsoremsugar —  January 13, 2011 — 15 Comments

One common request I’ve seen is the ability to convert records of one module to another. It just came up again this week in this forums post, using the example of how Sugar does Lead Conversion. While this is a more advanced example where multiple records are created as a part of the conversion and specific business logic code will be needed, a more simple example is where we would want do have a simple single record in one module to a single record in another module conversion.

For a good example of how to do this, let’s look at the Target to Lead conversion as it exists in the product. To do this, there are two important code snippets to look at; first, we have a button on the Targets DetailView which will trigger the conversion.

$viewdefs['Prospects']['DetailView'] = array(
    'templateMeta' => array(
        'form' => array(
            'buttons' => array(
                 ...snipped code...
                 array('customCode' => ''),
                 ...snipped code...
                 ),

We’ve added a custom button which will call the Leads EditView, passing with it the return_action of Prospects ( this being the bean name of the Targets module ) and the return_id being the current Target record’s id. Both of these fields are important, since we’ll be using them in the next code snippet to know which Target record we are converting from.

Next is to handle the incoming request to the Leads EditView correctly. I’ve grabbed this snippet of code out of the Leads module controller.php code that illustrates how this is done.

class LeadsController extends SugarController
{
	public function pre_editview()
        {
		//IF we have a prospect id leads convert it to a lead
		if (empty($this->bean->id) && !empty($_REQUEST['return_module']) &&$_REQUEST['return_module'] == 'Prospects' ) {

			$prospect=new Prospect();
			$prospect->retrieve($_REQUEST['return_id']);
			foreach($prospect->field_defs as $key=>$value)
			{
				if ($key == 'id' or $key=='deleted' )continue;
				if (isset($this->bean->field_defs[$key])) {
					$this->bean->$key = $prospect->$key;
				}
			}
			$_POST['is_converted']=true;
		}
		return true;
	}

This code is designed to convert a Target record ( the bean name is Prospect ) into a Lead record thru the Leads EditView page. To do this, we first check to see if the referring page was a view of a Target record and we aren’t opening an existing Lead record. If that is the case, we retrieve the Target record, and then iterate thru the field defs and copy the value of each field in the Target record into a Lead record. After this the user will end up on the Leads EditView with all of the applicable data from the originating Target record in the form, and the user can click ‘Save’ to save the new record or ‘Cancel’ to abort the conversion.

Using this example, you do simple record conversion between any modules. It can also be easily extended to handle field mapping between fields of different names or defaulting certain target record fields to a particular value, depending upon your requirements.

15 responses to HOWTO: Convert a record of one module type to a record in another

  1. 

    Hello, thank you for the examples. i’m trying to do exactly that, but i can’t seem to find where do i put those snippets. there are several folders for my custom module and all of them contain that detailed view php file… and nomatter which one i edit i don’t see ANY change in the sugarCRM… maybe if you could guide me a bit …
    tnx

    • 

      hello, you have to insert the first part of code in the detailviewdefs.php that you find in customYOUR MODULE NAMEmetadata adding this code you’ll be able to create a new custom button in detailview of your module.For the second one, you have to create a new file in customYOUR MODULE NAME folder the rename it to controller.php. With this one you’ll be able start the action called by your button.

    • 

      oops!i almost forgot once you have done your customizations you need to run a “quick repair and rebuild” from admin panel.

  2. 

    This code didn’t work for me. I had to add

    $this->view = ‘edit';

    to the pre_editview() function, otherwise I’ll end up on a blank page.

  3. 

    One thing that I see left out here is

    1)  You must edit your language files to include the custom button names and key values

    2)  If you are extending a module such as Contracts the only way I could get it to work was to use a filename of Controller.php, whereas the file that Prospects uses is controller.php

  4. 
    Mayank Sharma July 21, 2011 at 1:03 pm

    Very nice article, but I have one concern here. When we click on Convert Lead button it takes to Lead page, there it doesn’t populate fields with the custom module. Any reason/solution? 

  5. 

    Good Ones…………….

  6. 

    Good One I like it.

  7. 

    can u tell me how to use another module data in my custom module.

  8. 

    i want more details regarding this one…………….

  9. 

    It would be very nice if somebody could support with a few more detailed instructions (such as screenshots, paths to file directories etc.) on how to do this. Thanks.

  10. 

    halfway guide ?

Trackbacks and Pingbacks:

  1. convert customer in to contact. - SugarCRM Forums - October 10, 2011

    […] […]

  2. Duplicating the lead module or adding the convert layout to custom modules - January 21, 2013

    […] […]

  3. Custom module with "convert Lead to Contact" functionality - January 23, 2013

    […] […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s