Is it somehow possible to implement a process based on TASK creation, that accesses information on a related opportunity (if present)?
More precisely, I want to update a field on the task, if the account behind the related opportunity has a related_company field.
Meaning: Task.related_company__c = Task.Opportunity.Account.related_company
However, since Opportunity is not required on the task accessing it is not trivial. Any help appreciated!
Update:
For anybody wondering, how to access leads information. I created an invokable method and will trigger it on Task creation if WhoId starts with '00Q'. Find the method below:
public with sharing class ActivityCompanyFromLead {
@InvocableMethod(label='ForActivityGetCompanyFromLead'
description='If invoked, will check the lead for a related company and update the task')
public static void getLeadInformation(List<Task> tasksToUpdate) {
List<Task> tasks = [SELECT Id,Related_Company__c,WhoId
FROM Task
WHERE Id IN: new Map<Id, Task>(tasksToUpdate).keySet()];
if(tasks.size() > 0) {
List<String> whoIds = new List<String>();
for (Task t : tasks) {
whoIds.add(t.WhoId);
}
if (whoIds.size() > 0) {
List<Lead> leads = [SELECT Id,Related_Company__c
FROM Lead
WHERE Id IN: whoIds];
//create mapping from leadId to related company
Map<String,String> whoIdMap = new Map<String,String>();
for(Lead l : leads){whoIdMap.put(l.Id,l.Related_Company__c);}
for (Task t : tasks){
if(String.isEmpty(t.Related_Company__c) == true) {
ID related_company = whoIdMap.get(t.WhoId);
t.Related_Company__c = related_company;
}
}
update tasks;
}
}
} }
Feedback is obviously welcome. Thanks a bunch to David!