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
wedaftwedaft 

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!

 

Best Answer chosen by Admin (Salesforce Developers) 
wedaftwedaft

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

JitendraJitendra

The else part will never executed in your case. Why you are checking same condition in else part?

try below code :

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 && 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);
        
        }  
        if(TI.trigger_lock__c == true && 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);
    }   
}

 

vriavmvriavm

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);
    }    
}

wedaftwedaft

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.

JitendraJitendra

Please post the code which you are using to insert the record. If you are doing through UI then please ensure that :

  • Trigger is Active
  • field "trigger_lock__c " is checked.
  • Value of "of_Observations_Required__c" is either 1 or 2
wedaftwedaft

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.

JitendraJitendra

How you are verifing that record is created or not? 

wedaftwedaft

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.

JitendraJitendra

Have you tried debug log?

 

Put System.debug() statement, every where in check that log is printing or not in debug logs.

vriavmvriavm

Run this query and see the first record better :

select id from COTM_Protocol__c order by createddate desc limit 5

wedaftwedaft

I know I probably sound completely ignorant, but how exactly do I run a query? 

vriavmvriavm

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...

 

wedaftwedaft

Ah. There were required fields on the COTM Protocol that weren't being populated by the trigger. Fixed! Thank you for your help

This was selected as the best answer