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
Filippo SalviFilippo Salvi 

Add case field in the task view

Hi.

I would like to create a new field in the task that contain an information already present in the case.
For example: I would lile to add in the task view the case record type.

I can creat a customized field in the task but I am not able to relate this field with the Case record type.

I think is not so hard but I am a principiant :(

Thanks!!
Best Answer chosen by Filippo Salvi
Bhanu MaheshBhanu Mahesh
Hi salvi,

We can do this bu using trigger on Task

Lets suppose our field to track case record type on task is Case_Record_Type__c

Try below is the code for trigger on task object 
trigger UpdateCaseRecordTypeTrigger on Task (before insert,before update) {
    List<Id> caseIdSet = new List<Id>();
    Map<Id,Case> mapIdWIthCase = new Map<Id,Case>();
    
    if(trigger.isInsert){
        for (Task tsk: Trigger.new) {
            if (tsk.WhatId != null && String.valueOf(tsk.WhatId).startsWith('500')) {
                caseIdSet.add(tsk.WhatId);
            }
        }
    }
    else if(trigger.isUpdate){
        for (Task tsk: Trigger.new) {
            if (tsk.WhatId != null && tsk.WhatId != trigger.oldMap.get(tsk.Id).WhatId && String.valueOf(tsk.WhatId).startsWith('500')) {
                caseIdSet.add(tsk.WhatId);
            }
        }
    }
    
    if(!caseIdSet.isEmpty()){
        for(Case cas : [SELECT Id,RecordType.Name FROM Case WHERE Id IN :caseIdSet]){
            mapIdWIthCase.put(cas.Id,cas);
        }
    }
    
    for (Task tsk: Trigger.new) {
        if (tsk.WhatId != null && String.valueOf(tsk.WhatId).startsWith('500') && mapIdWIthCase.get(tsk.WhatId) != null) {
            tsk.Case_Record_Type__c = mapIdWIthCase.get(tsk.WhatId).RecordType.Name;
        }
    }
    
 }

Mark this as "SOLVED" if your query is answered.

Thanks & Regards,
Bhanu Mahesh