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
Mehul pMehul p 

How to get Name from Task Instead of WhoId?

Hi guys,
       I need a help here.. I am trying to extract Name from Tasks but I got the ID instead. Is there any way we could get the name from Task.
User-added image
Map<Id,Contact> ContactMap =  new Map<Id,Contact>([select id, Last_Activity_Subject__c,	Last_Activity_Date__c, Last_Contact_First_Name__c, Last_Contact_Last_Name__c from Contact where id in:ContactIds]);
        
        for(Task t :Trigger.new)
    
            for(Contact l : ContactMap.Values())
            {  
               If(t.subject does not contain "words" ) // how to filter some subject from here?
                l.Last_Activity_Subject__c = t.subject;
                l.Last_Activity_Date__c = t.ActivityDate;
                l.Last_Contact_First_Name__c = t.WhoId;  //<= I need a Name here Not an ID.
                
                
                ContactList.add(l);
            }
Thanks in advance!
Khan AnasKhan Anas (Salesforce Developers) 
Hi Mehul,

Greetings to you!

For if condition, you can use contains method. The contains method returns a boolean, so you can use boolean operators on the result. 
 
if(!t.subject.contains('string')){
    // Logic
}

If you want to get the name from a task you can use Who.Name
 
l.Last_Contact_First_Name__c = t.Who.Name;

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Mehul pMehul p
Hey Khan,

            I tried that solution but it still saves it even though I put "2018" in the subject.  After changing it to t.Who.Name, field became empty.
 
Map<Id,Contact> ContactMap =  new Map<Id,Contact>([select id, Last_Activity_Subject__c,	Last_Activity_Date__c, Last_Contact_First_Name__c, Last_Contact_Last_Name__c from Contact where id in:ContactIds]);
        
        for(Task t :Trigger.new)
        {
         If((!t.Subject.contains('2018')) ){   
            for(Contact l : ContactMap.Values())
            {  
                
                    l.Last_Activity_Subject__c = t.subject;
                    l.Last_Activity_Date__c = t.ActivityDate;
                    l.Last_Contact_First_Name__c = t.Who.Name;
                    
                    
                    ContactList.add(l);
             } 
            }
       }
User-added image