Adding mass email functionality for campaigns to custom module

Recently I had problem with adding mass email for campaigns in custom modules but managed to figure it out in couple of simple steps and decided to share my solution with community.

Let’s get to it.

  1. Create 1 to many relationship between custom module and activity

  2. In modules/{custom_module_name}/{custom_module_name}.php add implements EmailInterface to class declaration like in account module:
    class Account extends Company implements EmailInterface

  3. Create many to many relationship between target lists and custom module

Now time for coding.
4. In custom/modules/ProspectLists edit/create file logic_hooks.php and add after relationship add logic hook and after relationship delete. Here is documentation for logic hooks if you’re unsure of how to do it.
example of mine:

    <?php
    $hook_version = 1;
    $hook_array = Array();

     $hook_array['after_relationship_add'] = Array();
     $hook_array['after_relationship_add'][] = Array(
                                   100,
                                   'addRelationship',
                                   'custom/modules/ProspectLists/toMail.php',
                                   'toMail',
                                   'onAdd');

      $hook_array['after_relationship_delete'] = Array();
     $hook_array['after_relationship_delete'][] = Array(
                                   101,
                                   'deleteRelationship',
                                   'custom/modules/ProspectLists/toMail.php',
                                   'toMail',
                                   'onDelete');
  1. Then on a path declared in logic hook create your file. In it you want to insert data of your custom module into table prospect_lists_prospects. Below is my example if you want to get it working for yourself you need to change part_partnerzy_p to your module name.
    <?php
    class toMail
    {
    	function onAdd($bean, $event, $arguments)
    	{
    		if (!($arguments['related_module'] == "part_partnerzy_p") || $this->linkExists($bean->id, $arguments['related_bean']->id)) return;

    		$id = create_guid();
    		$prospect_list_id = $bean->id;
    		$related_id = $arguments['related_bean']->id;
    		$related_type = $arguments['related_module'];
    		$date_modified = 'CURDATE()';
    		$deleted = '0';

    		$query = "INSERT INTO prospect_lists_prospects (`id`, `prospect_list_id`, `related_id`, `related_type`, `date_modified`, `deleted`) VALUES ('?', '?', '?', '?', ?, '?')";
    		$arr = array($id, $prospect_list_id, $related_id, $related_type, $date_modified, $deleted);

    		$db = DBManagerFactory::getInstance();
    		$db->pQuery($query, $arr);
    	}

    	function linkExists($prospect_list_id, $related_id)
    	{
    		$query = "SELECT id FROM prospect_lists_prospects WHERE prospect_list_id = '?' AND related_id = '?' AND deleted = 0";

    		$db = DBManagerFactory::getInstance();
    		$ret = $db->pQuery($query, array($prospect_list_id, $related_id));

    		if($ret-> num_rows > 0) return true;
    		return false;
    	}


    	function onDelete($bean, $event, $arguments)
    	{
    		$prospect_list_id = $bean->id;
    		$related_id = $arguments['related_bean']->id;

    		$query = "UPDATE prospect_lists_prospects SET deleted = 1 WHERE prospect_list_id = '?' AND related_id = '?'";

    		$db = DBManagerFactory::getInstance();
    		$ret = $db->pQuery($query, array($prospect_list_id, $related_id));
    	}
    }

And that’s it. after rebuild and repair you should be able to create target lists that work in campaigns.

1 Like