I am trying to update Task Records and set the WhoId (a look up field) on the Task equal to the value of a lookup field on the associated (Related to) Opportunity.
I have a specific list of Opportunities that I query and store into a map, I then want to loop over the Task records in Trigger.new and based on the subject line, I want to set the "WhoId" field on the Task equal to the value of the "Main_Contact_ID__c" field in the Opportunity where the Opportunity ID is Equal to the value of the "WhatId" field on the task.
I have this code which doesn't quite work as I need it to:
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([SELECT Id, Main_Contact_ID__c FROM Opportunity WHERE Id = :oppIds]);
for(Task t: Trigger.new) {
// if the task subject contains Business-Deal
if( t.Subject.contains('Business-Deal') ) {
t.WhoId = oppMap.get('Main_Contact_ID__c');
}
}