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
Devendra Hirulkar 3Devendra Hirulkar 3 

how to write test class of this trigger

hi all
this is my trigger i dont no how to write test class for this trigger
please help me to know how to do this
trigger CopyRolltoStudent1 on class__c (before insert,before update) //You want it on update too, right?
{
  Map<ID, Student__c> parentroll = new Map<ID, Student__c>(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();

  for (class__c childObj : Trigger.new)
  {
    listIds.add(childObj.student__c);
  }

  //Populate the map. Also make sure you select the field you want to update, Rollno
  //The child relationship is more likely called Classes__r (not Class__r) but check
  //You only need to select the child classes a if you are going to do something for example checking whether the Classes in the trigger is the latest
  parentroll = new Map<Id, Student__c>([SELECT id, Roll_No__c,(SELECT ID, Roll_No__c FROM Class__r) FROM Student__c WHERE ID IN :listIds]);

  for (class__c cl : Trigger.new)
  {
     Student__c myParentroll = parentroll.get(cl.student__c);
     myParentroll.Roll_No__c = Decimal.valueOf(cl.Roll_No__c);
  }
    
  update parentroll.values();
}
Sforce.NinjaSforce.Ninja
Here are sample steps for the test class

1. Create the 'Student__c' dummy record
2. Create the 'class__c' dummy record in test class and assign the Student__c from above, set the Roll_No__c on class__c to '1234'
3. Insert the Class__c record
4. Check if the Student.Roll_No__c is '1234' using assert statement
5. Change class__c to 4321 and update.
6. Check if the Student.Roll_No__c is '4321' using assert statement

This is a pseudo code for writing test class for the trigger. If you succeed, step 7. Mark this as the answer

This link (https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods) will help you with the syntax

Ninja Out,
http://sforce.Ninja