+ Start a Discussion
Angie YzaguirreAngie Yzaguirre 

trigger to change task record type created by workflow rule

I have created a WFR that creates a task every time the Account has a certain product listed in a custom field.  The task is created just fine, but I need the task to be a certain task record type, rather than the default type.

I have created another rule to update the task record type field when the subject correlates to the first task created.  I realize that a workflow cannot be triggered off of another workflow.

I need to write the trigger to change the task record type, can someone please help with this??

Thanks!!
Best Answer chosen by Angie Yzaguirre
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Well, if the names that you provided are correct this code should do the work. Just create a Apex Trigger and paste the code below:
 
trigger TaskRecordType on Task  (after delete, after insert, after update, before delete, before insert, before update) {

	if(Trigger.isBefore && Trigger.isInsert) {
	
		Map<String, Schema.RecordTypeInfo > taskRecordTypes = Task.sObjectType.getDescribe().getRecordTypeInfosByName();
		Id recordTypeId = taskRecordTypes.get('CA - NWC Roadmap').getRecordTypeId();
		
		for ( Task tsk : Trigger.new) {
			if(tsk.Subject.equalsIgnoreCase('CA - NWC Roadmap'))
				tsk.RecordTypeId = recordTypeId;
		}
	}
}

If you do not name your Apex Trigger as TaskRecordType you have to change in the code with the name that you gave it. Let me know if it worked.

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 

All Answers

Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

What will be the criteria to distinguish this Task that want to change the RecordType from another one?

Regards.
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Try the code below changing the bolded parts:
 
trigger TaskRecordType on Task  (after delete, after insert, after update, before delete, before insert, before update) {

	if(Trigger.isBefore && Trigger.isInsert) {
	
		Map<String, Schema.RecordTypeInfo > taskRecordTypes = Task.sObjectType.getDescribe().getRecordTypeInfosByName();
		Id recordTypeId = taskRecordTypes.get('My Task Name (NOT API)').getRecordTypeId();
		
		for ( Task tsk : Trigger.new) {
			if(tsk.Subject.equals('Condition'))
				tsk.RecordTypeId = recordTypeId;
		}
	}
}


taskRecordTypes.get('My Task Name (NOT API)').getRecordTypeId();
In the line above you have to change 'My Task Name (NOT API)' to your RecordType's name (the label) not the API.


if(tsk.Subject.equals('Condition'))
In the line above you should add a condition to prevent your trigger from changing the RecordType of your Tasks even if its not related to that specific case.

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Angie YzaguirreAngie Yzaguirre
Ah, thank you for replying!!

The default task that is created from the first workflow has a subject of CA - NWC Roadmap, I'm assigning it to a member of the Account Team with a Status of Not Started and Priority High (these are the only fields I can automate when creating the task from the workflow).  The task record type is default 'Sales - Tasks'.

For the second workflow, the criteria that I want to change is the task record type, from 'Sales - Tasks' to 'CA - NWC Roadmap'.

Please let me know if you need a snapshot or more info on what I'm trying to accomplish.  I'm stuck and very new to Apex so it's a bit over my head.  Thanks!
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Well, if the names that you provided are correct this code should do the work. Just create a Apex Trigger and paste the code below:
 
trigger TaskRecordType on Task  (after delete, after insert, after update, before delete, before insert, before update) {

	if(Trigger.isBefore && Trigger.isInsert) {
	
		Map<String, Schema.RecordTypeInfo > taskRecordTypes = Task.sObjectType.getDescribe().getRecordTypeInfosByName();
		Id recordTypeId = taskRecordTypes.get('CA - NWC Roadmap').getRecordTypeId();
		
		for ( Task tsk : Trigger.new) {
			if(tsk.Subject.equalsIgnoreCase('CA - NWC Roadmap'))
				tsk.RecordTypeId = recordTypeId;
		}
	}
}

If you do not name your Apex Trigger as TaskRecordType you have to change in the code with the name that you gave it. Let me know if it worked.

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 
This was selected as the best answer
Angie YzaguirreAngie Yzaguirre
It worked!!!! Thank you!!
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Glad to have helped :)
Angie YzaguirreAngie Yzaguirre
Hello again - I went to deploy the code into production and ran into the issue that the code coverage is at 0%.  Any ideas on how to fix this??
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Just create a class as the code below and deploy it with your trigger:
 
@isTest
private class TaskRecordTypeTest {

   static testMethod void testInsert() {
    
		Map<String, Schema.RecordTypeInfo > taskRecordTypes = Task.sObjectType.getDescribe().getRecordTypeInfosByName();
		Id recordTypeCAId = taskRecordTypes.get('CA - NWC Roadmap').getRecordTypeId();
			
		Task tsk = new 	Task(	Subject = 'CA - NWC Roadmap',
								Status = 'Not Started',
								ActivityDate = system.now().date(),
								Priority = 'High', 
								Description = 'Test description',
								OwnerId = UserInfo.getUserId());
			
		Database.insert(tsk);
		
		tsk = [SELECT RecordTypeId FROM TASK WHERE Id =: tsk.Id];
		
		System.assert(tsk.RecordTypeId == recordTypeCAId);
			
    }
}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Gabriel Meneses 10Gabriel Meneses 10
@Zuinglio Lopes

I have a question for you, I see the trigger and test class above. Is there any way you can help me with building an Apex class that would be called by the trigger? I want the apex class to run the recordtype change instead of the trigger. I am in the early stages of learning to write these and would love some assistance.