+ Start a Discussion
DoondiDoondi 

Is this Bulk Trigger, if so how do I write test classes for this trigger?

Hello, Champ's,
Below is my trigger, works fine if I am inserting one record or updating one record.
I would like to know if it qualifies for a BULK trigger.
 
Intro: Two custom objects, School__c and Admission__c,
whenever a new school__c record is inserted or updated with the TYPE and STATUS conditions, 
Then
New admission__c record to be created with the mentioned values.
 
trigger CreateAdmissionRecord on School__c (after insert, after update) 
{
        Student_Trigger__c TT = Student_Trigger__c.getvalues(UserInfo.getProfileId());
        set<id> triggerIds = trigger.newMap.keyset();
        List <Admission__c> Project = [select id, Name,Deal__r.Student__r.Name from Admission__c where id in :triggerIds];
        List <Admission__c> newadmissions = new List<Admission__c>() ;
    	
        for(School__c scool : [Select id,Name,Type__c,Status__c,Manager__c,  Class__r.Name, Class__c,Unit__c,New_Flag__c from School__c where id in :triggerIds])
        {
		// the below line is to active or de active the trigger in production from CUSTOM SETTINGS
                if((TT.Active__c == TRUE) 
                &&
				// While INSERTING or UPDATE the school record, a New Admission record to be created based 
				// on the below condition[if Type__c = New and Status__c = changed]
               (scool.Type__c == 'New') 
                && 
                (scool.Status__c == 'Changed')
            	{
				// the below string tries to check for duplicate names and avoid creating duplicate records. 
                string Name = scool.Name + ' - ' + scool.Class__r.Name;
                    List <Admission__c> ProjectList = [select id, Name,Deal__r.Class__r.Name from Admission__c where Name =: Name];
                    if (ProjectList.size() > 0)
                    {
                      if(!ProjectList.isEmpty())
						insert newadmissions;
                    }
                    else{
                    newadmissions.add(new Admission__c
                                  (
                                      Name = scool.Name + ' - ' + scool.Class__r.Name, 
                                      OwnerId = scool.Manager__c,
                                      Status__c = 'Project Not Started',
                                      Unit__c = scool.Unit__c,
                                      New_Unit__c = scool.New_Flag__c  
                                  )
                                 );
            }
        }
    
        insert newadmissions;
    }
}

It will be great if anyone can help me with test classes. 
 
KrishnaAvvaKrishnaAvva
Hi Doondi,

This below line qualifies it for a bulk trigger as it can handle more than one record from Trigger.new. (set<id> triggerIds = trigger.newMap.keyset();)
for(School__c scool : [Select id,Name,Type__c,Status__c,Manager__c,  Class__r.Name, Class__c,Unit__c,New_Flag__c from School__c where id in :triggerIds]).

For writing test classes, please refer to the following to begin with:
https://trailhead.salesforce.com/content/learn/modules/apex_testing/apex_testing_triggers
https://blog.jeffdouglas.com/2009/04/20/writing-bulk-triggers-for-salesforce/
https://blog.mkorman.uk/testing-apex-triggers/

Regards,
Krishna Avva
DoondiDoondi
Between line number 4 & 5 I have the below code as per your suggestion

Trigger.new (set<id> triggerIds = trigger.newMap.keyset();)

it's throwing an error (Unexpected token '('.)
KrishnaAvvaKrishnaAvva
Hi Doondi,
Please make sure you close the if bracket at line number 11.
if((TT.Active__c == TRUE) && (scool.Type__c == 'New') && (scool.Status__c == 'Changed')) {
	//Your logic here
}
Please mark this as SOLVED if it had helped you. Thanks!

Regards,
Krishna Avva