Skip to content

Instantly share code, notes, and snippets.

@mtetlow
Created April 19, 2013 18:58
Show Gist options
  • Save mtetlow/5422421 to your computer and use it in GitHub Desktop.
Save mtetlow/5422421 to your computer and use it in GitHub Desktop.
TaskRay add Project Name as a Task Tag
@isTest
private class customTriggerTests{
@isTest
static void tagAdditionOnTaskAdd_test(){
//Insert a project
TASKRAY__Project__c project = new TASKRAY__Project__c(Name='Test Project', TASKRAY__Status__c = false);
insert project;
//get the id of the newly inserted project
Id projectId = project.Id;
//Add a task to the new project
TASKRAY__Project_Task__c task = new TASKRAY__Project_Task__c(Name='Test Task', TASKRAY__Project__c= projectId);
insert task;
//now check to see if there was a tag added
sObject taskTag = [SELECT ItemId,Name FROM TASKRAY__Project_Task__tag WHERE ItemId=:task.Id LIMIT 1];
system.assert(project.Name==String.valueOf(taskTag.get('Name')));
//Now check to see if when the project is changed, the tag is updated correctly
//Insert a project
TASKRAY__Project__c newProject = new TASKRAY__Project__c(Name='Test Project2', TASKRAY__Status__c = false);
insert newProject;
//Update the task
task.TASKRAY__Project__c=newProject.Id;
upsert task;
//now check to see if the tag was updated
sObject newTaskTag = [SELECT ItemId,Name FROM TASKRAY__Project_Task__tag WHERE ItemId=:task.Id LIMIT 1];
system.assert(newProject.Name==String.valueOf(newTaskTag.get('Name')));
}
}
trigger trTaskAddProjectNameTag on TASKRAY__Project_Task__c (after update, after insert){
if(trigger.isInsert){
//First we need to find out info about the new task, like if it has a project assigned to it. Then persist it for later.
Map<Id,Id> taskProjectIdMap=new Map<Id,Id>();
Set<Id> projectIds=new Set<Id>();
for(TASKRAY__Project_Task__c newTask : trigger.new){
if(newTask.TASKRAY__Project__c!=null){
taskProjectIdMap.put(newTask.Id,newTask.TASKRAY__Project__c);
projectIds.add(newTask.TASKRAY__Project__c);
}
}
//If we have tags to add, let's add them.
if(projectIds.size()>0)
{
//Find the text we want to put into the tag (the Project's name)
Map<Id,String> projectIdNameMap=new Map<Id,String>();
List<TASKRAY__Project__c> projects = [SELECT Id, Name FROM TASKRAY__Project__c WHERE Id IN :projectIds];
for(TASKRAY__Project__c project : projects){
projectIdNameMap.put(project.Id,project.Name);
}
//Construct a map for a relationship between the task id and the project name string
Map<Id,String> taskProjectNameMap= new Map<Id,String>();
for(Id taskId : taskProjectIdMap.keySet()){
Id projectId=taskProjectIdMap.get(taskId);
String projectName=projectIdNameMap.get(projectId);
taskProjectNameMap.put(taskId,projectName);
}
//Now insert the tags by looping through the map
List<sObject> tagList = new List<sObject>();
for(Id taskId : taskProjectNameMap.keySet()){
String tagStr=taskProjectNameMap.get(taskId);
sObject newTag = Schema.getGlobalDescribe().get('TASKRAY__Project_Task__tag').newSObject();
newTag.put('Name',tagStr);
newTag.put('ItemId',taskId);
newTag.put('Type','Public');
tagList.add(newTag);
}
//We try and catch to ensure we don't interrupt the task addition with an exception on tag add
//Namely tag's are very picky about what characters are used in them
try{
insert tagList;
}
catch(Exception e){
system.debug('error inserting the tag');
system.debug(e.getMessage());
}
}
}
if(trigger.isUpdate){
//First we need to look at the contents of the old tasks
Map<Id,Id> oldTaskProjectIdMap=new Map<Id,Id>();
Set<Id> oldProjectIds=new Set<Id>();
for(TASKRAY__Project_Task__c oldTask : trigger.old){
oldTaskProjectIdMap.put(oldTask.Id,oldTask.TASKRAY__Project__c);
oldProjectIds.add(oldTask.TASKRAY__Project__c);
}
//Now let's look at the contents of the new tasks
Map<Id,Id> newTaskProjectIdMap=new Map<Id,Id>();
Set<Id> newProjectIds=new Set<Id>();
Set<Id> newTaskIds=new Set<Id>();
for(TASKRAY__Project_Task__c newTask : trigger.new){
if(newTask.TASKRAY__Project__c != oldTaskProjectIdMap.get(newTask.TASKRAY__Project__c)){
newTaskProjectIdMap.put(newTask.Id,newTask.TASKRAY__Project__c);
newProjectIds.add(newTask.TASKRAY__Project__c);
newTaskIds.add(newTask.Id);
}
}
//Now we need to delete the old tag, then add the new one.
if(newProjectIds.size()>0)
{
Map<Id,String> projectIdNameMap=new Map<Id,String>();
List<TASKRAY__Project__c> projects = [SELECT Id, Name FROM TASKRAY__Project__c WHERE Id IN :newProjectIds OR Id IN :oldProjectIds];
for(TASKRAY__Project__c project : projects){
projectIdNameMap.put(project.Id,project.Name);
}
//now we need to delete the old tags for a project
List<sObject> existingTags = [SELECT ItemId,Name FROM TASKRAY__Project_Task__tag WHERE ItemId IN :newTaskIds];
List<sObject> tagsToDel= new List<sObject>();
for(sObject existingTag : existingTags){
Id tagTask=String.valueOf(existingTag.get('ItemId'));
String tagStr=String.valueOf(existingTag.get('Name'));
String oldProjectName=projectIdNameMap.get(oldTaskProjectIdMap.get(tagTask));
if(oldProjectName==tagStr){
tagsToDel.add(existingTag);
}
}
//Perform the delete operation
delete tagsToDel;
//Now we have to find the new replacement tags
Map<Id,String> taskProjectNameMap= new Map<Id,String>();
for(Id taskId : newTaskProjectIdMap.keySet()){
Id projectId=newTaskProjectIdMap.get(taskId);
String projectName=projectIdNameMap.get(projectId);
taskProjectNameMap.put(taskId,projectName);
}
//Now insert the tags
List<sObject> tagList = new List<sObject>();
for(Id taskId : taskProjectNameMap.keySet()){
String tagStr=taskProjectNameMap.get(taskId);
sObject newTag = Schema.getGlobalDescribe().get('TASKRAY__Project_Task__tag').newSObject();
newTag.put('Name',tagStr);
newTag.put('ItemId',taskId);
newTag.put('Type','Public');
tagList.add(newTag);
}
//We try and catch to ensure we don't interrupt the task addition with an exception on tag add
//Namely tag's are very picky about what characters are used in them
try{
insert tagList;
}
catch(Exception e){
system.debug('error inserting the tag');
system.debug(e.getMessage());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment