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
Jyoti SinghJyoti Singh 

How can I create Tasks with a specific record type by trigger?

Hi


I am creating tasks by trigger using code: Can anyone suggest ,How can I  create Tasks with a specific record type by trigger? using this code?

Task tsk = new Task(whatID = ID, Ownerid =user.id, Subject = 'XYZ', ActivityDate = DueDate.date() , IsReminderSet = true);
tasks.add(tsk);
Best Answer chosen by Admin (Salesforce Developers) 
swatKatswatKat

You can try this :

 

RecordType r =[Select Id from RecordType where DeveloperName='Name of the record type'];
Task tsk = new Task(whatID = ID, Ownerid =user.id, Subject = 'XYZ', ActivityDate = DueDate.date() , IsReminderSet = true,recordtypeId=r.Id);
tasks.add(tsk);

 

 

 

All Answers

swatKatswatKat

You can try this :

 

RecordType r =[Select Id from RecordType where DeveloperName='Name of the record type'];
Task tsk = new Task(whatID = ID, Ownerid =user.id, Subject = 'XYZ', ActivityDate = DueDate.date() , IsReminderSet = true,recordtypeId=r.Id);
tasks.add(tsk);

 

 

 

This was selected as the best answer
Jyoti SinghJyoti Singh

Thanks it solved my problem. :-)

Puja_mfsiPuja_mfsi

HI,

Use the below code to create task with specific record type.You can get the id of the particular record type withour perform query in recordType object:

 

final string taskRecordType = 'Name of record type';
Map<String, Schema.RecordTypeInfo> taskRecordTypes = new Map<String, Schema.RecordTypeInfo>();
Id taskRecordTypeId;
taskRecordTypes = Schema.SObjectType.Task.getRecordTypeInfosByName();
taskRecordTypeId = taskRecordTypes.get(taskRecordType).getRecordTypeId();

 

Task tsk = new Task(

           whatID = ID,

           Ownerid =user.id,

           Subject = 'XYZ',  

           ActivityDate = DueDate.date() ,

           IsReminderSet = true,

           RecordTypeId = taskRecordTypeId /* Assign record type Id.*/

);
tasks.add(tsk);

Jyoti SinghJyoti Singh

Thanks puja..

Actually I am getting recordTypeID by custom label.