last day (20 days later) » 

13:02
3
A: How to set Assigned To Id in task to Account.OwnerId in task after insert trigger

Adrian LarsonFixing Your Syntax You need to pull the Id from your record. To make your attempt work, you would have to do: Id accountOwner = [SELECT Account.OwnerId FROM Task WHERE Id = :myTask.Id].Account.OwnerId; Trigger Best Practice However, this approach is not bulk safe. What you should really be...

by newRecords did you mean trigger.new? newRecords gives me an error Variable does not exist.
Yep you figured it out. :) Usually, I put this code into a trigger handler so the code does not go in a trigger body. That is also best practice, but I'm trying to keep it to a few quick simple things for you to pick up on.
Ok so I changed the after to before and changed newRecords to trigger.new and it doesn't seem to be working.
Ok let me give you a little background on what I'm trying to do. salesforce.stackexchange.com/questions/270494/…
Did you actually change the trigger context from after insert to before insert?
trigger AssignTaskOwner on Task (before insert) { Set<Id> accountIds = new Set<Id>(); for (Task record : trigger.new) { accountIds.add(record.AccountId); } Map<Id, Account> accounts = new Map<Id, Account>([ SELECT OwnerId FROM Account WHERE Id IN :accountIds ]); for (Task record : trigger.new) { Account parent = accounts.get(record.AccountId); if (parent != null) record.OwnerId = parent.OwnerId; }
13:02
So are you sure Task.AccountId is populated? And are you sure your trigger is active?
Yes the trigger is active. I added System.debug('The value is: ' + accountIds); and it looks like it's null. I'm trying to see if anything else is getting populated and I added List<Account> account = new List<Account>(); account.add(record.Subject); and got Method does not exist or incorrect signature: void add(String) from the type list.
This task is being added from a case so I'm not sure if that has anything to do with the AccountId being null.
13:27
so I did a system.debug on the record and it looks like some values are populated just not AccountId
14:15
Yeah I mean...the entire feature description you've built out is based on that field being populated. Is it actually stored in WhatId?
 
1 hour later…
15:19
It seems like the case id is stored in the WhatId
 
1 hour later…
16:28
I made some changes to pull AccountId from the case instead. The cases Map returns the following {5002a000005GX1mAAG=Case:{AccountId=0012a00000Xp8XkAAJ, Id=5002a000005GX1mAAG}}. How do I use that AccountId to pull the account owner and assign the task owner to the account owner?
If you are querying the cases, just add Account.OwnerId to your query, rather than AccountId.
 
1 hour later…
17:54
Thanks how do I pull the Id and assign it. I tried Case parent = cases.get(record.Account.OwnerId); and Case parent = cases.get(record.AccountId); both returned null what am I doing wrong?
18:18
What is record there?
for (Task record : Trigger.New)
{
Case parent = cases.get(record.Account.OwnerId);
System.debug('AccountId' + parent);
System.debug('Task Owner' + record.OwnerId);
System.debug('Account Owner' + parent.Account.OwnerId);

if (parent != null) record.OwnerId = parent.Account.OwnerId;
}
18:37
if the Task points to the Case, it would be via WhatId
I'm lost WhatId will give me the case id I need the Case Account OwnerId
Set<Id> caseIds = new Set<Id>();
for (Task record : Trigger.New)
{
caseIds.add(record.WhatId);
System.debug('The value of caseIds is: ' + caseIds);
}

Map<Id, Case> cases = new Map<Id, Case>([
SELECT Account.OwnerId, AccountId FROM Case WHERE Id IN :caseIds
]);
System.debug('Cases Map: ' + cases);

for (Task record : Trigger.New)
{
Case parent = cases.get(record.Account.OwnerId);
System.debug('AccountId' + parent);
System.debug('Task Owner' + record.OwnerId);
System.debug('Account Owner' + parent.Account.OwnerId);
Yeah. So you would do:

Case parent = cases.get(record.WhatId);
Id ownerId = parent.Account.OwnerId;
Or just set record.OwnerId = parent.Account.OwnerId.
It appears to be working thank you. Now I just have to figure out how to make the Process send out emails thanks so much for being so patient

  last day (20 days later) »