Find your content:

Search form

You are here

Creating a Task in Apex

 
Share

I'm trying to create a Task in Apex, but it doesn't seem to be working. Here's my actual code:

RecordType rt = [SELECT Id FROM RecordType WHERE Name = 'Some Record Type' AND SobjectType = 'Task'];
Task logCall = new Task(WhatId = friend.Id, RecordTypeId = rt.Id, Type = 'General Task', Subject = 'Here\'s my subject', Description = 'Description');

System.debug('@@@@@@@@@@@');

try {
    insert logCall;
} catch (DmlException e) {
    ApexPages.addMessages(e);
    return appendId(Page.SomeView);
}

When I execute that, there are no error returned to my view. When I check at the developer console, I get this error message:

15:35:28:208 VARIABLE_ASSIGNMENT [3]|this.message|"You do not have priv (40 more) ..."|0x67e122
15:35:28:208 VARIABLE_ASSIGNMENT [4]|this.message2|"You can not modify t (61 more) ..."|0x67e122

Any idea how to prevent that from happening?

Thanks.


Attribution to: jpmonette

Possible Suggestion/Solution #1

It seems like the code is working. The reason I couldn't see it from my object Activity History is because I wasn't filling the DueDate and the Status.

So, a quick ActivityDate = Date.today() and Status = 'Completed' did the job.


Attribution to: jpmonette

Possible Suggestion/Solution #2

  1. Check the SysAdmin profile's allowed Task Record Types (this is enforced even if you're SysAdmin, on any object).
  2. Check if your "friend" object whatever it is that you're passing as WhatId) has "Allow Activities" ticked.
  3. Consider using normal Debug Logs or just open raw log so you can see the full message.

Attribution to: eyescream
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/5517

My Block Status

My Block Content