You need to sign in to do that
Don't have an account?
wedaft
Trigger to create new records. No errors received, but no new records either!
I've created a trigger to create new child records (COTM_Protocol__c) when certain conditions are met on the teacher implementation record (Teacher_Implementation__c). I am not getting any errors when I save the trigger, and I'm not getting any errors when I save the parent record with the conditions that should set off the trigger. No error messages, it's just that no new records are being created. Any idea of where I am going wrong, based on the code below?
trigger createCOTMOnTeacherImplementationX on Teacher_Implementation__c (after insert, after update) { List <COTM_Protocol__c> COTMToInsert = new List <COTM_Protocol__c> (); for (Teacher_Implementation__c TI : Trigger.new) { if(TI.trigger_lock__c == true) if(TI.of_Observations_Required__c == 1) { //NEW COTM// COTM_Protocol__c COTM1 = new COTM_Protocol__c (); COTM1.Teacher_Implementation__c = TI.ID; COTM1.CO_due_date__c = TI.CO1_due__c; COTM1.Observer_User__c = TI.School_Implementation__r.OwnerId; COTMToInsert.add(COTM1); } else if(TI.trigger_lock__c == true) if(TI.of_Observations_Required__c == 2) { //NEW COTM// COTM_Protocol__c COTM2 = new COTM_Protocol__c (); COTM2.Teacher_Implementation__c = TI.ID; COTM2.CO_due_date__c = TI.CO1_due__c; COTM2.Observer_User__c = TI.School_Implementation__r.OwnerId; //NEW COTM// COTM_Protocol__c COTM3 = new COTM_Protocol__c (); COTM3.Teacher_Implementation__c = TI.ID; COTM3.CO_due_date__c = TI.CO2_due__c; COTM3.Observer_User__c = TI.School_Implementation__r.OwnerId; COTMToInsert.add(COTM2); COTMToInsert.add(COTM3); }//end if }//end for try { insert COTMToInsert; } catch (system.Dmlexception e) { system.debug (e); } }
Thanks!
Ah. There were required fields on the COTM Protocol that weren't being populated by the trigger. Fixed! Thank you for your help
All Answers
The else part will never executed in your case. Why you are checking same condition in else part?
try below code :
Try this:
trigger createCOTMOnTeacherImplementationX on Teacher_Implementation__c (after insert, after update) {
List <COTM_Protocol__c> COTMToInsert = new List <COTM_Protocol__c> ();
for (Teacher_Implementation__c TI : Trigger.new) {
if(TI.trigger_lock__c == true) {
if(TI.of_Observations_Required__c == 1) {
//NEW COTM//
COTM_Protocol__c COTM1 = new COTM_Protocol__c ();
COTM1.Teacher_Implementation__c = TI.ID;
COTM1.CO_due_date__c = TI.CO1_due__c;
COTM1.Observer_User__c = TI.School_Implementation__r.OwnerId;
COTMToInsert.add(COTM1);
} else if(TI.of_Observations_Required__c == 2) {
//NEW COTM//
COTM_Protocol__c COTM2 = new COTM_Protocol__c ();
COTM2.Teacher_Implementation__c = TI.ID;
COTM2.CO_due_date__c = TI.CO1_due__c;
COTM2.Observer_User__c = TI.School_Implementation__r.OwnerId;
//NEW COTM//
COTM_Protocol__c COTM3 = new COTM_Protocol__c ();
COTM3.Teacher_Implementation__c = TI.ID;
COTM3.CO_due_date__c = TI.CO2_due__c;
COTM3.Observer_User__c = TI.School_Implementation__r.OwnerId;
COTMToInsert.add(COTM2);
COTMToInsert.add(COTM3);
}//end if
}
}//end for
try {
if (COTMToInsert != null && COTMToInsert.size() > 0) {
insert COTMToInsert;
}
} catch (system.Dmlexception e) {
system.debug (e);
}
}
Thanks for the suggestions, but the same thing happened with both of those. The code saved fine with no errors, but no new child records (COTM Protocols) were created by the trigger.
Please post the code which you are using to insert the record. If you are doing through UI then please ensure that :
I am testing it through the user inteface. Yes, the trigger is active, and both of those fields required to set off the trigger have the appropriate values. Is there nothing else in the code that explains the new records not being generated.
How you are verifing that record is created or not?
I'm checking the related list on the record, and then I'm going to the COTM Protocol object itself to look for any newly created records.
Have you tried debug log?
Put System.debug() statement, every where in check that log is printing or not in debug logs.
Run this query and see the first record better :
select id from COTM_Protocol__c order by createddate desc limit 5
I know I probably sound completely ignorant, but how exactly do I run a query?
Hi,
Please use data loader or download force.com beta http://wiki.developerforce.com/page/ForceExplorer
login withusername , password and security token
then run the query...
Ah. There were required fields on the COTM Protocol that weren't being populated by the trigger. Fixed! Thank you for your help