+ Start a Discussion
Ken sfdc1Ken sfdc1 

Case record type name on Task object

HI,

  Can anyone help me how to acheive this I tried a formula field --
If the related to is a case record then it needs to populate case record type on task record.

Let me know if you understood it.
Veenesh VikramVeenesh Vikram
You can try below code:
trigger prepopulateCaseId on Task(before Insert, before Update){
	List<Id> relatedCaseIds = new List<Id>();
	for (Task tsk : trigger.new){
		if(String.valueOf(tsk.whatId).subString(0,3) == '005'){
			relatedCaseIds.add(tsk.whatId);
		}
	}
	Map<Id,Case> caseRecords = new Map<Id,Case>();
	caseRecords.putAll([SELECT id, Name, RecordType.DeveloperName, RecordTypeId FROM Case WHERE Id in :relatedCaseIds ]);
	
	for (Task tsk : trigger.new){
		if(String.valueOf(tsk.whatId).subString(0,3) == '005'){
			tsk.YOUR_FIELD__c = caseRecords.get(tsk.whatId).RecordType.DeveloperName;
		}
	}
}
Kindly Mark as Solved if this resolves your Issue.

Best Regards
Veenesh