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
scottrmscottrm 

set record type on task created in apex

I want to set the record type on a task I have created in apex with code similar to below but I don't see any property on the task object where I can do this, any ideas?

 

 Task myTask = new Task();
 myTask.Subject = 'Follow up task for opportunity';

 

The bigger problem I am trying to solve is create tasks in a trigger on opportunity creation. I can do this ok but I want to make sure they can not be deleted without affecting the delete permissions on any other kind of task. I thought about doing this by creating a task with a different record type so I could assign a page layout to it where the delete button is not available. I don't know if there is another way.

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

you can set record type Id in task object , query record type against Name and Sobject Type

and set into new record

 

Task myTask = new Task();
myTask.Subject = 'Follow up task for opportunity';
myTask.RecordTypeId =[Select Id,SobjectType,Name From RecordType Name ='YOUR_RECORD_TYPE_NAME' and SobjectType ='Task'  limit 1].Id;
.
.
//setting other fields

insert myTask; 

 

 

Hope this will help you out

Thanks,

Bala

All Answers

b-Forceb-Force

you can set record type Id in task object , query record type against Name and Sobject Type

and set into new record

 

Task myTask = new Task();
myTask.Subject = 'Follow up task for opportunity';
myTask.RecordTypeId =[Select Id,SobjectType,Name From RecordType Name ='YOUR_RECORD_TYPE_NAME' and SobjectType ='Task'  limit 1].Id;
.
.
//setting other fields

insert myTask; 

 

 

Hope this will help you out

Thanks,

Bala

This was selected as the best answer
b-Forceb-Force

It really good to see that solution works for you

 

Thanks,

Bala

Lee HildebrandLee Hildebrand

Thanks for the help. One thing: the query was missing WHERE. I've added it below:

 

myTask.RecordTypeId = [Select Id,SobjectType,Name From RecordType WHERE Name ='YOUR_TASK_RECORD_TYPE_NAME' and SobjectType ='Task'  limit 1].Id;