You can, and because of special functionality on Task.AccountId
you don't have to use the generalized solution, which requires either Apex or Process Builder + Flow to handle the polymorphic Related To field (WhatId
). Instead, for this use case, you can get it done with Process Builder alone.
Process Builder Solution
Task
has a lookup to Account
as well (Account
/AccountId
). When the Task
is related to an Opportunity, the Account
is populated automatically with the Opportunity's Account. That's not the only source of population of the AccountId
field:
If the value of
WhatId
is any of the following objects, then Salesforce uses that object’sAccountId
.
- Account
- Opportunity
- Contract
- Custom object that is a [master-detail] child of Account
If the value of the
WhatId
field is any other object, and the value of theWhoId
field is a Contact object, then Salesforce uses that contact’sAccountId
.
This fact allows you to do a Process Builder-only solution when you want to do some work that involves a related Account.
Set up your Process on Task, when a record is created or edited. Set the criteria note to "Related To Id starts with "006", which will select Tasks related to Opportunities.
Then, add a Record Update action. Choose Related_Company__c
on Task, and set its value to a Field Reference. Index through the Account Id node to get the Related Company field there, and you're all set.
Generalized Solution
When you don't get to take advantage of the builtin AccountId
lookup, you have a somewhat more complex solution pattern that requires either Apex or Process Builder + Flow.
The basic logic is to inspect the WhatId
to determine its type ('006' is the key prefix for an Opportunity, so a Process can have a criterion "Related To Id starts with "006", or you can look at What.Type
in Apex) and then perform a query (Apex) or lookup (Flow) to find the relevant Opportunity and field, based on an Id that turns out to be an Opportunity.
I'd personally prefer to do this in an Apex trigger, because to me the Flow approach here is a little clumsy, but it's perfectly doable in Flow. You'd start a Process on Task update and have the Process inspect its WhatId
to see if it's related to an Opportunity, and if so pass the Task's Id to an invoked Flow, which would implement the logic as above.