8

I'm trying to create a unit test where a I insert a record with a specific record type

    Student_Records__c endterm = new Student_Records__c();
    endterm.School__c = schl.id;
    endterm.Student__c = ct1.id;
    endterm.Position_in_class__c = 1;
    endterm.Type_Bursary__c = 'Full Bursary';
    endterm.Type_of_Exam__c = 'Test';
    endterm.Date__c = date.today();
    endterm.Number_of_students_in_class__c = 10;

The above code works fine when I run the unit test but when I try add:

   endterm.Recordtype.Name = 'Student Exam Records';

I get an error when I run the test. How can I set the record type of a test record being inserted?

9

You need to set the Id not the name. First fetch the Id

Id stExam = Schema.SObjectType.Student_Records__c.getRecordTypeInfosByName().get('Student Exam Records').getRecordTypeId();

and then assign it

endterm.RecordtypeId= stExam;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.