I have a trigger that inserts a new child record (Training Session) below Training Module using a third object (Scheduler). Training Module contains an external ID called Training_Module_ID, but when I enter that into Scheduler and save it gives me the following error:
Error: Invalid Data. Review all error messages below to correct your data. Apex trigger createTrainingSession caused an unexpected exception, contact your administrator: createTrainingSession: execution of AfterInsert caused by: System.StringException: Invalid id: Test: Trigger.createTrainingSession: line 7, column 1
However, if I type in the 15 character Record ID, then the Training Session is created as intended. How can I make this work using the external ID?
trigger createTrainingSession on Scheduler__C(after insert) {
List < Training_Session__c > TStoinsert = new List < Training_Session__c > ();
for (Scheduler__C o: Trigger.new) {
if (o.Type__c == 'Schedule Training') {
Training_Session__c v = new Training_Session__c();
v.Training_Module_ID__c = o.Training_Module_ID__c;
TStoinsert.add(v);
}
}
try {
insert TStoinsert;
} catch (system.Dmlexception e) {
system.debug(e);
}
}