User Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
en:adminmanual:businessmappings:duplicaterecords [2017/06/16 16:03]
joebordes
en:adminmanual:businessmappings:duplicaterecords [2017/06/16 16:27] (current)
joebordes [Duplicating second level records]
Line 45: Line 45:
 Workflows are not launched on the main record being duplicated, so we avoid getting into an infinite loop at this first stage. Workflows are not launched on the main record being duplicated, so we avoid getting into an infinite loop at this first stage.
  
-Workflows are launched on all related records that are created, which opens the possibility of inifinte ​loops. For example, if the **DuplicateDirectRelations** directive is set to true on the related records they will create a new record in the main module for sure.+Workflows are launched on all related records that are created, which opens the possibility of infinite ​loops. For example, if the **DuplicateDirectRelations** directive is set to true on the related records they will create a new record in the main module for sure.
  
 So be very careful with the rules and configurations you set when duplicating. So be very careful with the rules and configurations you set when duplicating.
Line 56: Line 56:
 If you need to duplicate also the relations on those records, or even go lower in the relation tree, we need to do some additional work. If you need to duplicate also the relations on those records, or even go lower in the relation tree, we need to do some additional work.
  
 +In order to completely duplicate a record, we must know the CRMID of the record. This is the record that is launching the workflow.
  
 +If we define another workflow, with a duplicate record task, on one or more of the related modules we may think that it will launch when the parent duplicate tasks creates a record in this module. In other words, let's suppose that we have a duplicate record task on Projects and we want to duplicate all the Project Tasks with the relations it may have. So we add another duplicate record task on the Project Tasks module. What we end up with is two duplicate Project Tasks with none of the related information the original one had. This is wrong but makes sense. Let me try to explain.
 +
 +The duplicate task on Project starts creating new duplicate Project Tasks after duplicating the Project record that launched the workflow. This process creates a new Project Task and relates it to the new duplicated Project. This save process launches the duplicate record task on the newly created Project Task record which is duplicated, NOT the original Project Task which the first task duplicated but the SECOND, duplicate Project Task is the one that launches the workflow. That is why we end up with two records with no related records.
 +
 +To fix this we must add a new field on the Project Task module (and on all the related modules we want to duplicate correctly). This field must be called **//​isduplicatedfromrecordid//​**. It will hold the CRMID of the original record the duplication started from and will be used to get the relations and launch the workflow correctly.
 +
 +This new field will also work correctly in the normal edit of the application,​ getting filled in automatically when we duplicate, so it is also a helpful field to have around. You can set it to displaytype 3 or 4 if you don't want to have it on the display.
 +
 +This is the code to add the field:
 +
 +<code PHP>
 +<?php
 +
 +// Turn on debugging level
 +$Vtiger_Utils_Log = true;
 +
 +include_once('​vtlib/​Vtiger/​Module.php'​);​
 +
 +$modname = '​Potentials';​ // Or whatever module you want to have this on.
 +$module = Vtiger_Module::​getInstance($modname);​
 +
 +if($module) {
 +    $blockInstances = Vtiger_Block::​getAllForModule($module);​
 +    $blockInstance = reset($blockInstances);​ // get first block
 +
 + if($blockInstance) {
 +
 + $field = new Vtiger_Field();​
 + $field->​name = '​isduplicatedfromrecordid';​
 + $field->​label= '​isduplicatedfromrecordid';​
 + $field->​table = $module->​basetable;​
 + $field->​column = '​isduplicatedfromrecordid';​
 + $field->​columntype = '​INT(11)';​
 + $field->​uitype = 10;
 + $field->​displaytype = 1;
 + $field->​typeofdata = '​I~O';​
 + $field->​presence = 0;
 + $blockInstance->​addField($field);​
 + $field->​setRelatedModules(Array($modname));​
 +
 + echo "<​br><​b>​Added Field to $modname module.</​b><​br>";​
 +
 + } else {
 + echo "<​b>​Failed to find $modname block</​b><​br>";​
 + }
 +
 +} else {
 + echo "<​b>​Failed to find $modname module.</​b><​br>";​
 +}
 +
 +?>
 +</​code>​