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
JamsieJamsie 

Create new RecordType and assign to custom object

Hi there,

 

I have built a trigger (on insert) for a custom object (Mail Item) that sets the owner to be a particular queue depending on the recordtype selected during creation.

 

I am now trying to write the test class.  My problem is that I cannot work out how to create a new record type and assign it to my test created Mail Item object.

 

I can't rely upon a particular record type being in the system so cannot run a query on the record  types.

 

Any help is much appreciated.

Thanks,

James.

incuGuSincuGuS

Hello Jamsie,

 

If i understood correctly you have a Custom Object "Mail Item" and another custom object "type" , is that correct?

if so , in your test method create a new TYPE record (it will only exist within the test method) and create a mail of that type.

 

Something along the lines of :

 

 

MailItem__c mail = new MailItem__c();
MailItemType__c newType = new MailItemType__c();

newType.Name = "New type"; // set whatever is needed.
insert newType; // insert a new type

mail.type__c = newType.Id; // assign the new type to the mail 
mail.body__c = "Hello!"; // set whatever else is needed.

Test.StartTest();
insert mail; // here your trigger will execute.
Test.StopTest();

 

 

Im not sure i understand the problem completely , could you explain a bit more if this was not what you needed?

 

Let me know if this helped you,

Gaston.