I'm having problens with a trigger that is supposed to add a hardcoded contact ID before the task is inserted. I've arrived to the point where I'm adding the Who ID to the task, but it never works.
The code:
public static void setTaskWhoid (List<Task> tasklist){
Set<Id> idMain = new Set<Id>();
string textId;
for (task tarefa: tasklist){
if(tarefa.WhatId != NULL){
textId = tarefa.WhatId;
}
if(textId != NULL && textId.startsWith('500')){
idMain.add(tarefa.WhatId);
}
}
if(idMain.size() > 0){
List<Case> caselist = [SELECT Id, AccountId FROM Case WHERE Id IN: idMain];
Map <Id,Id> mapCase = new Map<Id,Id>();
for(Case caso: caselist){
system.debug('Account ID'+caso.AccountId);
if(caso.AccountId == '001c000000fxTF8'){
mapCase.put(caso.AccountId, '003c000000XEZfU');
}
for (task tarefa: tasklist){
system.debug('WhoID:' + mapCase.get(caso.AccountId));
tarefa.WhoId = mapCase.get(caso.AccountId);
}
}
}
}
I've add 2 debugs to check if it's getting eveything correctly, and so far so good, but my problem is that even after I add a WhoId to the task nothing appears in the actual task page.
Any help is appreciated.
EDIT: I've tried changing the trigger to 'after insert' and add a upsert at the end of the code, it returned the following error TaskWhoID: execution of AfterInsert caused by: System.FinalException: Record is read-only: Class.TaskWhoID.setTaskWhoid: line 24, column 1, line 24 is tarefa.WhoId = mapCase.get(caso.AccountId);
EDIT 2: Due to Request here is the trigger code:
trigger TaskWhoID on Task (before insert) {
TaskWhoID.setTaskWhoid(Trigger.new);
}
EDIT 3: Might've found the problem, according to this site if I have shared activities turned on i can't update task.whoid field before insert/update, and task.whoid cannot be updated after insert/update. I've created a request for them to turn shared activities off and will answer after tests.