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
Vershley JoyejobVershley Joyejob 

Create a map of Task and Contact. Map <Task, Contact>, any ideas please?

Hello, 

Any ideas on how I can create a map of Task and Contact, like this Map<Task, Contact> by using the WhoId from Task. I already have the Ids of Tasks that should be processed.

Thank you for your help.

Regards,
Vershley
Best Answer chosen by Vershley Joyejob
Vershley JoyejobVershley Joyejob
Got it like this, feel free to post your answers if you have anything better :)

 
List<Task> taskList = [SELECT Id, whoId, ActivityDate, whatId, Description, Status, subject FROM Task WHERE WhoId != null];
        Set<Id> contactIdSet = new Set<Id>();
        Map<Task, Id> taskByContactIdMap = new Map<Task, Id>();
        
        for(Task tsk : taskList){
            if(tsk.WhoId != null){
                contactIdSet.add(tsk.WhoId);
                taskByContactIdMap.put(tsk, tsk.WhoId);
            }            
        }
                

        Map<Id, Contact> contactByIdMap = new Map<Id, Contact>();
        
        for(Contact cnt : [SELECT Id, Name, Phone, MobilePhone, Title, Email FROM Contact WHERE Id IN :contactIdSet]){
            contactByIdMap.put(cnt.Id, cnt);
        }
        
        Map<Task, Contact> taskByContactMap = new Map<Task, Contact>();
        for(Task tsk : taskList){
            taskByContactMap.put(tsk, contactByIdMap.get(taskByContactIdMap.get(tsk)));       
            
        }

Thanks!

All Answers

Vershley JoyejobVershley Joyejob
Got it like this, feel free to post your answers if you have anything better :)

 
List<Task> taskList = [SELECT Id, whoId, ActivityDate, whatId, Description, Status, subject FROM Task WHERE WhoId != null];
        Set<Id> contactIdSet = new Set<Id>();
        Map<Task, Id> taskByContactIdMap = new Map<Task, Id>();
        
        for(Task tsk : taskList){
            if(tsk.WhoId != null){
                contactIdSet.add(tsk.WhoId);
                taskByContactIdMap.put(tsk, tsk.WhoId);
            }            
        }
                

        Map<Id, Contact> contactByIdMap = new Map<Id, Contact>();
        
        for(Contact cnt : [SELECT Id, Name, Phone, MobilePhone, Title, Email FROM Contact WHERE Id IN :contactIdSet]){
            contactByIdMap.put(cnt.Id, cnt);
        }
        
        Map<Task, Contact> taskByContactMap = new Map<Task, Contact>();
        for(Task tsk : taskList){
            taskByContactMap.put(tsk, contactByIdMap.get(taskByContactIdMap.get(tsk)));       
            
        }

Thanks!
This was selected as the best answer
Rana CracksRana Cracks
Thank you so much for this information. Rana Cracks (https://ranacracks.com)
procracktool .comprocracktool .com
It's Really good blog i Like the way how you explain it.
I am grateful to read it. Thanks for sharing. procracktool (https://procracktool.com/)
procracktool .comprocracktool .com
It's Really good blog i Like the way how you explain it.
I am grateful to read it. Thanks for sharing.
<a href="https://procracktool.com/minecraft-crack/">minecraft crack free</a>
<a href="https://procracktool.com/winx-dvd-ripper-crack/">winx dvd ripper crack download</a>
<a href="https://procracktool.com/restoro-crack/">restoro crack patch</a>
<a href="https://procracktool.com/convert-youtube-to-mp3/">convert youtube to mp3</a>
<a href="https://procracktool.com/payroll-crack-download/">payroll crack download 2023</a>
<a href="https://procracktool.com/">procracktool.com</a>
<a href="https://procracktool.com/reason-crack/">reason crack free download</a>
<a href="https://procracktool.com/purity-vst-crack/">purity vst crack download</a>
<a href="https://procracktool.com/zmodeler-crack/">zmodeler crack free patch</a>
<a href="https://procracktool.com/pgsharp-activation-key/">pgsharp activation key full</a>
<a href="https://procracktool.com/windows-7-product-key/">Windows 7 Product Key latest version</a>