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

How to create a closed task



How do I create a Closed task using Apex?


I have the following code, but it creates an Open task.

I've also tried to replace Task with ActivityHistory, but it seems to have had the effect of making all fields read only.


Task act = new Task();
act.Subject = 'SMS: '+message;
act.Description = message+'\n'+'Sent to: '+toPhone+'\n'+'Sent From: '+FromPhone;
act.whoId = recipient_id;
act.ownerId = user_id;
act.Status = 'Closed';
act.whatId = what_id;

act.IsClosed = true; //this line doesn't compile.

Database.SaveResult sr = Database.insert (act);



Thank you.


Non essential Background:

For an SMS system I'm trying to create a "Log an SMS" button. (It actually sends the text, but that's besides the point).


I'm trying to have something along the lines of "Log a Call" button in the  "Activity History" section of a contact's detail page.


You have to set the Status field to a Status picklist value that corresponds to closed. The isClosed field is read only but it is automatically updated based on the Status field. Check the object's fields or query the Task Status object to see the picklist options.



P.S. I realize this thread is old, but I just had this issue and when I googled for an answer this came up. Figured it out, so I figured it was worth posting.