2

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.

  • What error do you see? – Keith C Dec 2 '14 at 18:23
  • The name of the contact in the task is not beeing filled with the specified contact, I've add another debug line at the end to check 'tarefa.WhoId' and it's returning the expected hardcoded value. but when i check the actual task page, there is nothing there. – Sephaq Dec 2 '14 at 18:55
  • Weird; you'd get an INVALID_CROSS_REFERENCE_KEY error if the ID was bad. And its definitely "before insert"? – Keith C Dec 2 '14 at 19:24
  • Yes before insert – Sephaq Dec 3 '14 at 11:26
  • You're showing this as a class, not as a trigger. I'm guessing you must call this from a trigger? If so, would you show us your code for the trigger that contains how you create your list and pass it to your class? Perhaps the problem lies there. For some reason, this seems to resemble more of a controller without a getter and setter method than it does a trigger. Also, recognize that these values won't display in the UI until the save occurs unless they're passed to trigger.new. – crmprogdev Dec 3 '14 at 14:54
2

If you are always using the same Contact, there is no need for a map.

Aside from the best practices listed below, your aims are unclear. Do you mean to replace every WhoId or only the Case WhoIds? Right now it looks like you are taking all Tasks that look up to an Case and if that Case looks up to a particular Account, replace the WhoId with a particular Contact. In all other situations, it looks like you will be inserting null into the WhoId. You will even insert null sometimes if you insert multiple tasks related to Cases, some of which are not to the Account you are looking for.

RECOMMENDED PRACTICE: Take advantage of static types. Compare SObjectType directly:

if (tarefa.WhatId != null && tarefa.WhatId.getSObjectType() == Case.SObjectType)
{
    caseIds.add(tarefa.WhatId);
}

BEST PRACTICE: Never use hard coded Ids. Query (and lazy load) your data like so.

static Account importantAccount
{
    get
    {
        if (importantAccount == null)
        {
            List<Account> potentialAccounts = [SELECT Id FROM Account];
            // WHERE someUniqueField = someUniqueValue
            if (!potentialAccounts.isEmpty()) importantAccount = potentialAccounts[0];
        }
        return importantAccount;
    }
    private set;
}

static Contact importantContact
{
    get
    {
        if (importantContact == null)
        {
            List<Contact> potentialContacts = [SELECT Id FROM Contact];
            // WHERE someUniqueField = someUniqueValue
            if (!potentialContacts.isEmpty()) importantContact = potentialContacts [0];
        }
        return importantContact;
    }
    private set;
}

BEST PRACTICE: Don't nest for loops. You are not using any Case information in your Task loop. Separate them.

for (Case caso : caseList)
{
    //do stuff
}
for (Task tarefa : taskList)
{
    //do stuff
}
| improve this answer | |
  • That is a code for test in the sandbox mode. The Code is ment to add a replace the WhoId every Task related to Case has with a specific Contact of that Accounts Case. I'm fairly new to all this, so I haven't read any of the ebst pratices, since I'm adapting a code that was developed by a Salesforce partner company, when it's deployed there will be multiple accounts with an specific contact for each account. It's indeed a rushed development. Thank you for your patience, and i will surely look at the best practices. – Sephaq Dec 2 '14 at 18:46
1

So regarding my 3rd edit, It was correct, the code was working correctly.

The problem lies in that I can't update the Task.WhoId before the insert if the feature Shared Activities is enabled.

Fields not updated.

Thanks for the help.

| improve this answer | |
0
string textId;

It appears to me that you're using 15 character strings for your ID's. Try converting them to Id's by using ID.valueOf(String) and see if that doesn't solve your problem.

| improve this answer | |
  • Didn't seen to solve the problem. – Sephaq Dec 2 '14 at 18:58
  • Did you try it here: tarefa.WhoId =ID.valueOf( mapCase.get(caso.AccountId)); ? – crmprogdev Dec 2 '14 at 19:11
  • Problem still persists, the code executes with no problem but the Name of the contact in the task is still blank – Sephaq Dec 3 '14 at 11:32
  • I just noticed that my WhoID is coming with 18 characters, instead of "003c000000XEZfU" it adds AAX in the end like : '003c000000XEZfUAAX" When i see the debug message in the log 09:28:31:052 USER_DEBUG [25]|DEBUG|tarefa WhoId: 003c000000XEZfUAAX – Sephaq Dec 3 '14 at 11:38
  • An 18 digit ID isn't case sensitive like a 15 digit ID. That's the difference and not an issue. – crmprogdev Dec 3 '14 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.