function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Melissa ParkerMelissa Parker 

Task record type same as parent record type

Can anyone assist me on how to create a trigger to upate the Task record type to reflect the same as the Parent Record type? For instance, if the Lead or Opporunity record type is "TEST RECORD TYPE" then Task record type is updated as the same "TEST RECORD TYPE". Thanks.
Best Answer chosen by Melissa Parker
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello Melissa,

My bad! Lead and Opportunity has different fields in Task so I had to add some extra validations. Please, try the code below:
 
trigger TaskRecordType on Task  (after delete, after insert, after update, before delete, before insert, before update) {

	if(Trigger.isBefore && !Trigger.isDelete) {
	
		Set<Id> leadIds = new Set<Id>();
		Set<Id> opportunityIds = new Set<Id>();
		Map<String, Schema.RecordTypeInfo > taskRecordTypes = Task.sObjectType.getDescribe().getRecordTypeInfosByName();
			
		for ( Task tsk : Trigger.new) {
            if(tsk.WhoId != null && tsk.WhoId.getSObjectType() == Lead.sObjectType)
                    leadIds.add(tsk.WhoId);
			else if (tsk.WhatId != null && tsk.WhatId.getSObjectType() == Opportunity.sObjectType)
                    opportunityIds.add(tsk.WhatId);                
		}
		
		Map<Id, String> recordTypeNames = new Map<Id, String>();
		for (Lead ld : [SELECT Id, RecordType.Name FROM Lead WHERE Id =: leadIds]) {
			recordTypeNames.put(ld.Id, ld.RecordType.Name);
		}
		
		for (Opportunity opp : [SELECT Id, RecordType.Name FROM Opportunity WHERE Id =: opportunityIds]) {
			recordTypeNames.put(opp.Id, opp.RecordType.Name);
		}
		
        if(recordTypeNames.isEmpty()) return;
        
		String recordTypeName;
        Id recordTypeId;
		for ( Task tsk : Trigger.new) {
            
            if(tsk.WhoId != null && tsk.WhoId.getSObjectType() == Lead.sObjectType)
                    recordTypeName = recordTypeNames.get(tsk.WhoId);
			else if (tsk.WhatId != null && tsk.WhatId.getSObjectType() == Opportunity.sObjectType)
                    recordTypeName = recordTypeNames.get(tsk.WhatId);
            
			if(String.isNotBlank(recordTypeName)) {
                recordTypeId = taskRecordTypes.get(recordTypeName).getRecordTypeId();
                if(recordTypeId != null)
					tsk.RecordTypeId = recordTypeId;
            }
		}	
	}
}

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,

Do you have these record types with the same Labels in all objects? I asking because RecordTypes are exclusive from one object and cannot be share. One solution would be having your RecordTypes with the same Labels. Are you looking for a solution only for Lead and Opportunity or a more generic one?

Regards
Melissa ParkerMelissa Parker
Just Leads and Opportunities for now. We have two specific record types that we have on Leads and Opportunities:
SolarPerks
Standard Products

Both have the same lables. Thanks, Zuinglio!
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Keep in mind that the code below can be changed as needed and that you must ensure that you have in Task, record types with the same Opp and Lead record types labels.
 
trigger TaskRecordType on Task  (after delete, after insert, after update, before delete, before insert, before update) {

	if(Trigger.isBefore && !Trigger.isDelete) {
	
		Set<Id> leadIds = new Set<Id>();
		Set<Id> opportunityIds = new Set<Id>();
		Map<String, Schema.RecordTypeInfo > taskRecordTypes = Task.sObjectType.getDescribe().getRecordTypeInfosByName();
			
		for ( Task tsk : Trigger.new) {
			if(tsk.WhatId.getSObjectType() == Lead.sObjectType)
				leadIds.add(tsk.WhatId);
			else if (tsk.WhatId.getSObjectType() == Opportunity.sObjectType)
				opportunityIds.add(tsk.WhatId);
		}
		
		Map<Id, String> recordTypeNames = new Map<Id, String>();
		for (Lead ld : [SELECT Id, RecordType.Name FROM Lead WHERE Id =: leadIds]) {
			recordTypeNames.put(ld.Id, ld.RecordType.Name);
		}
		
		for (Opportunity opp : [SELECT Id, RecordType.Name FROM Opportunity WHERE Id =: opportunityIds]) {
			recordTypeNames.put(opp.Id, opp.RecordType.Name);
		}
		
		String recordTypeName;
        Id recordTypeId;
		for ( Task tsk : Trigger.new) {
			
			if(recordTypeNames.containsKey(tsk.WhatId)) {
				recordTypeName = recordTypeNames.get(tsk.WhatId);
                recordTypeId = taskRecordTypes.get(recordTypeName).getRecordTypeId();
                if(recordTypeId != null)
					tsk.RecordTypeId = recordTypeId;
			}
		}	
	}
}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Melissa ParkerMelissa Parker
Great! Question.... where do I insert the recordtypeID or recordTYPEname? Bare with me as I am very new to code! But your super awesome for spelling it out for me here!!! :)
Melissa ParkerMelissa Parker
on and one more thing, does this trigger need to be created on the Parent object (Leads and Opps) or does it get created under Tasks?
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Do you mean in the code? If so, there is no need to do that, the code itself will handle to get the record type names of Lead and Opportunity and based on that will search for a record type in Task with the same Label. 

i.E: If you insert or update a Task related to a Lead, the trigger, based on Lead Id's will search for the record type name of Lead's record. Once found, it will get the name and search for a record type in Task with the same name and then it will get the recordtype's Id and set Task's record with the proper RecordTypeId. That is why you have to ensure that Task also will have two Record Types with the labels SolarPerks and Standard Products as Lead and Opportunity does. 

I have put some extra validation in the code to avoid errors:
 
trigger TaskRecordType on Task  (after delete, after insert, after update, before delete, before insert, before update) {

	if(Trigger.isBefore && !Trigger.isDelete) {
	
		Set<Id> leadIds = new Set<Id>();
		Set<Id> opportunityIds = new Set<Id>();
		Map<String, Schema.RecordTypeInfo > taskRecordTypes = Task.sObjectType.getDescribe().getRecordTypeInfosByName();
			
		for ( Task tsk : Trigger.new) {
            if(tsk.WhatId != null) {
                if(tsk.WhatId.getSObjectType() == Lead.sObjectType)
                    leadIds.add(tsk.WhatId);
                else if (tsk.WhatId.getSObjectType() == Opportunity.sObjectType)
                    opportunityIds.add(tsk.WhatId);
            }
		}
		
		Map<Id, String> recordTypeNames = new Map<Id, String>();
		for (Lead ld : [SELECT Id, RecordType.Name FROM Lead WHERE Id =: leadIds]) {
			recordTypeNames.put(ld.Id, ld.RecordType.Name);
		}
		
		for (Opportunity opp : [SELECT Id, RecordType.Name FROM Opportunity WHERE Id =: opportunityIds]) {
			recordTypeNames.put(opp.Id, opp.RecordType.Name);
		}
		
        if(recordTypeNames.isEmpty()) return;
        
		String recordTypeName;
        Id recordTypeId;
		for ( Task tsk : Trigger.new) {
			
			if(recordTypeNames.containsKey(tsk.WhatId)) {
				recordTypeName = recordTypeNames.get(tsk.WhatId);
                recordTypeId = taskRecordTypes.get(recordTypeName).getRecordTypeId();
                if(recordTypeId != null)
					tsk.RecordTypeId = recordTypeId;
			}
		}	
	}
}

If you need any further assistance, please let me know. You have to creat the trigger under Task, in the code I named it (first line) TaskRecordType. If you want to give another name just remember to change in the code too.


Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello Melissa,

It worked?

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Melissa ParkerMelissa Parker
I entered the code (Task Trigger) in my sandbox. I tested by creating a new task under a Lead record which had "SolarPerks" record type. To see if it would change the task record type automatically, I purposly chose "standard product" task record type on creation. When I saved the record, I noticed it did not trigger the automation to change the Task record type to "SolarPerks". I'm not sure if I should do something different or if the code is not working properly. Let me know what you need from me to get this to work. 

FYI>Thanks again, Zuinglio, for your time and assistance with this! :)
Melissa ParkerMelissa Parker
Here is the screenshot of the Task Trigger. Basically, I just copied/paste the recent code you provided. Is there anything im missing here?

User-added image
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello Melissa,

My bad! Lead and Opportunity has different fields in Task so I had to add some extra validations. Please, try the code below:
 
trigger TaskRecordType on Task  (after delete, after insert, after update, before delete, before insert, before update) {

	if(Trigger.isBefore && !Trigger.isDelete) {
	
		Set<Id> leadIds = new Set<Id>();
		Set<Id> opportunityIds = new Set<Id>();
		Map<String, Schema.RecordTypeInfo > taskRecordTypes = Task.sObjectType.getDescribe().getRecordTypeInfosByName();
			
		for ( Task tsk : Trigger.new) {
            if(tsk.WhoId != null && tsk.WhoId.getSObjectType() == Lead.sObjectType)
                    leadIds.add(tsk.WhoId);
			else if (tsk.WhatId != null && tsk.WhatId.getSObjectType() == Opportunity.sObjectType)
                    opportunityIds.add(tsk.WhatId);                
		}
		
		Map<Id, String> recordTypeNames = new Map<Id, String>();
		for (Lead ld : [SELECT Id, RecordType.Name FROM Lead WHERE Id =: leadIds]) {
			recordTypeNames.put(ld.Id, ld.RecordType.Name);
		}
		
		for (Opportunity opp : [SELECT Id, RecordType.Name FROM Opportunity WHERE Id =: opportunityIds]) {
			recordTypeNames.put(opp.Id, opp.RecordType.Name);
		}
		
        if(recordTypeNames.isEmpty()) return;
        
		String recordTypeName;
        Id recordTypeId;
		for ( Task tsk : Trigger.new) {
            
            if(tsk.WhoId != null && tsk.WhoId.getSObjectType() == Lead.sObjectType)
                    recordTypeName = recordTypeNames.get(tsk.WhoId);
			else if (tsk.WhatId != null && tsk.WhatId.getSObjectType() == Opportunity.sObjectType)
                    recordTypeName = recordTypeNames.get(tsk.WhatId);
            
			if(String.isNotBlank(recordTypeName)) {
                recordTypeId = taskRecordTypes.get(recordTypeName).getRecordTypeId();
                if(recordTypeId != null)
					tsk.RecordTypeId = recordTypeId;
            }
		}	
	}
}

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
This was selected as the best answer
Melissa ParkerMelissa Parker
WOO HOO! IT WORKS! Im so excited right now! Thank you, Thank you, Thank you!

​**TO QUOTE ICE CUBE... "TODAY WAS A GOOD DAY"!!**
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Glad to have helped! :)
Vidhyasagar RanganathanVidhyasagar Ranganathan
I am currently working on the same. The record type names on objects and tasks are different. How do I map them in the trigger.
Thanks!