Use the TaskRay Project Clone Apex Method in Your Own Code

This feature is available to the following product editions
  • Starter
  • Standard
  • Premium

The following article describes how Salesforce developers can take advantage of the TaskRay clone method in their own code. 

This method is focused on creating a clone of a single project (and its sub projects) in a transaction. Using this method in a loop or multiple times in a transaction would be equivalent to  SOQL queries and DML statements in a loop.

If you are cloning multiple projects at once consider: Using the TaskRay Bulk Clone Apex Method in Your Own Code

If you would like to create a project consisting of dynamically chosen tasks at the time of clone consider: Using the TaskRay Project Stitcher Apex Method in Your Own Code

One of the great things about building on the Salesforce platform is the ability to extend other features of the platform. Following that philosophy, we have made our Clone Project method Global, so other developers can use it without rebuilding the wheel. 

However, if you would like to learn more about the Project Clone and how it works, you can view the following articles:

 

TaskRay Project Clone Method

The Signature for this method is:

TASKRAY.trTaskBoardController.cloneProjectReturnID_v3(IdbaseProjectID, Map<String, Map<string, String>> projectInfo, Map<String,String> options);

 

Parameters

  1. Id baseProjectId This is simply the ID of the TaskRay Project Record you would like to clone. If you are cloning a hierarchy of projects, this is the top level project for the clone. 
    Note: For cloning templates, you will need to utilize the TaskRay ID Resolver to convert the template Addressable ID to a Salesforce ID. This will ensure your code always clones the active version of the template.
  2. Map<String, Map<String,String>> projectInfo This parameter is a map of TaskRay Project IDs relating to another map of override values for that Project. Valid override values for a project are 'newName', 'newStartDate', 'newEndDate'. The dates should be formatted in the current user's locale using something similar to:Date.today().format()
  3. Map <string,string> options The options parameter is a map of keys to values determining what features we will use in the clone operation.
 

Values for Options Map  

  • A key of 'assignInactiveToCurrent' can have a corresponding value of 'true' or 'false', this option will assign tasks which are owned by an inactive user to the current user. 
  • A key of 'cloneChildrenProjects' can have a corresponding value of 'true' or 'false', this option will determine if children of the baseProjectId parameter are cloned. 
  • A key of 'depCloneBasedOnProjectStart' can have a corresponding value of 'true' or 'false', this option will determine if the task dates will clone relative to the project start. A key of 'depCloneBasedOnProjectEnd' can have a corresponding value of 'true' or 'false', this option will determine if the task dates will clone relative to the project end.  
  • A key of 'createLookupToRecordId' can have a corresponding value of 'true' or 'false', this option will initiate the process of associating the new project to a record in Salesforce.  
  • A key of 'recordIdForLookup' takes a Salesforce record Id as a value.  If the option 'createLookupToRecordId' is 'true', the value of 'recordIdForLookup' will be associated with the new project, if the project has that record Id type as an available custom field. 
 

Example Usage 

First, if you have not already, you should create a Template in TaskRay. We will use this as the base for our project cloning. For our example, our template top level project has an ID of 'a0ja000000L7wRzAAJ'. 
Note: The ID above are the output from the TaskRay ID Resolver. You will need to utilize the template Addressable ID with the Resolver to ensure your clone operation always uses the active version of your template.
 

Apex Code  

//This is our top level project id 
Id baseProjectId = 'a0ja000000L7wRzAAJ'; 

//Project overrides
Map<String,Map<String,String>> projectInfo = new Map<String,Map<String,String>>();
Map<String,String> projectOverrides = new Map<String,String>();
projectOverrides.put('newName','Cloned Top Level Project '+Date.today().format());
projectOverrides.put('newStartDate',Date.today().addDays(-2).format());
projectInfo.put(baseProjectId,projectOverrides); 

//Now override a subproject's information
 projectOverrides = new Map<String,String>();
 projectOverrides.put('newName','Cloned Third Level Project '+Date.today().format());
 //'a0ja000000L7wS9AAJ' is the id for our "Third Level Clone Template" project
 projectInfo.put('a0ja000000L7wS9AAJ',projectOverrides); 

//Clone operation options
Map<String,String> options = new Map<String,String>();
options.put('cloneChildrenProjects','true');
options.put('assignInactiveToCurrent','true');
options.put('depCloneBasedOnProjectStart','true'); 

Id newBaseProjectId = TASKRAY.trTaskBoardController.cloneProjectReturnId_v3(baseProjectId, projectInfo, options);
//Notice that the Project Clone method returns the Id of the new baseProjectId equivalent 
//The sky is the limit on what you can do after the projects are created by inserting your own //business logic, updating custom fields, link lookup relationships 
system.debug(newBaseProjectId);

   

So, to recap, we were able to use the TaskRay global Project Clone method to clone our custom template hierarchy with minimal coding.
 
Things we clone in the Project Clone method:  
  • Project Records 
  • Task Records 
  • Checklist Item Records 
  • Team Members / Shares 
  • Chatter Topics 
  • Checklist Items for Tasks  
 
Things we do not clone in the Project Clone method:  
  • Chatter

Was this article helpful?

1 out of 2 found this helpful

Have more questions? Submit a request