function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pluviosillapluviosilla 

Tasks Assigned Programmatically to Contact Do Not Show Up

Strangest thing. I can generate Tasks programmatically in an Apex Visualforce controller without any problem, but if I use the identical code in an Apex trigger, I get a strange behavior. The Task object is created but not assigned to the Contact until I assign a Task that same Contact from some other source, and then - poof - the previously created Task objects suddenly show up on the Contact's detail page.

 

Here's the code in the trigger (NOTE that I am using a hardcoded ID for the Contact):

 

// This method will create a task
public void createTask()
{
    Task t = new Task(WhoId = '003Z000000WzJOt',
                                  Type = 'General Task',
                                  Subject = 'Create task for contact',
                                  Description = 'Description');
    try {
        insert t;
    } catch (DmlException e) {
        ApexPages.addMessages(e);
    }
}

 

I hesitate to add the code in the Apex Visualforce controller, because it is almost identical. The only difference is the return value of the method (PageReference instead of void):

 

public PageReference createTask() {
    Task tmpTask;
    tmpTask = new Task(WhoId = '003Z000000WzJOt',
                                     Type = 'General Task',
                                     Subject = 'subject text',
                                     Description = 'Description');

    try { 

        insert tmpTask;
    } catch (DmlException e) {
        ApexPages.addMessages(e);
        System.assert(false); // assert that we should never get here
        return null;
    }
    return null;
}

Tim BarsottiTim Barsotti

Questions:

--When you say it is not displaying on the contact, do you mean in the related list section?

--Have you tried to view all and see if it is listed? 

--Are you certain the createTask is being called from your trigger? Use debugging to ensure it is. 

--Have you tried waiting a few minutes to see if it shows up?

--Have you tried clearing your browser cache and refreshing?

 

Suggestion:

I would throw a system.debug(task.Id) on this after the insert. Turn on Debugging. Open the task in the UI and see if it associated to the contact. It could be taking a moment to refresh the UI.

 

Hope this helps point you in the right direction.

 

pluviosillapluviosilla

Questions:

--When you say it is not displaying on the contact, do you mean in the related list section?

ANS. Yes

--Have you tried to view all and see if it is listed? 

ANS. Not sure I understand. In my Visualforce controller I select *all* task records and display them and, yes, it does show up. The system is definitely creating the Task records, but they are not showing up in the related list of the Contact record.

--Are you certain the createTask is being called from your trigger? Use debugging to ensure it is. 

ANS. Yes. And it is successfully creating the Task objects.

--Have you tried waiting a few minutes to see if it shows up?

ANS. No.  :-)

--Have you tried clearing your browser cache and refreshing?

ANS. Sure.

 

Suggestion:

I would throw a system.debug(task.Id) on this after the insert. Turn on Debugging. Open the task in the UI and see if it associated to the contact. It could be taking a moment to refresh the UI.

ANS. Well, I have no doubt that it is associated with the Contact, because, like I say, when I create a Task record elsewhere and assign it to the Contact, *all* *previously* *created* Task records associated with that Contact suddenly show up in the Contact record's related list - poof!!! It is as though they were cached by the system or as if the Contact record requires a Task record in its list before it will display new ones. Bizarre.

 

Hope this helps point you in the right direction.

ANS. I don't think this answers my question, but I certainly appreciate that you took time to respond. Thanks very much.