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
Radhika pawar 5Radhika pawar 5 

How to write Test Class for Before insert/update Trigger

Hey Friends,
I am new in salesforce,
I am Stuck on writting test class for Trigger .
Below my  Code:
trigger add_questions_balwadi_batch on UP_Batch__c (before insert, before update)
{
List<UP_Question__c> piq = [Select id,Name from UP_Question__c];
for(UP_Batch__c upb : trigger.new)
{
    if(upb.Program__c == 'Balwadi')
    {
        upb.Q_1__c = piq[0].id;
        upb.Q_2__c = piq[1].id;
        upb.Q_3__c = piq[19].id;
        upb.Q_4__c = piq[2].id;
        upb.Q_5__c = piq[5].id;
        upb.Q_6__c = piq[12].id;
        upb.Q_7__c = piq[6].id;
        upb.Q_8__c = piq[15].id;
        upb.Q_9__c = piq[3].id;
        upb.Q_10__c = piq[14].id;
        upb.Q_11__c = piq[11].id;
        upb.Q_12__c = piq[10].id;
        upb.Q_13__c = piq[9].id;
        upb.Q_14__c = piq[13].id;
        upb.Q_15__c = piq[17].id;
        upb.Q_16__c = piq[18].id;
        upb.Q_17__c = piq[8].id;
        upb.Q_18__c = piq[16].id;
        upb.Q_19__c = piq[7].id;
        upb.Q_20__c = piq[4].id;
                                                       
    }
}

}
 
Bhanu MaheshBhanu Mahesh
Hi Radhika,

@isTest
public class TriggerTestClass{
    static testMethod void testInsert(){
        List<UP_Question__c> listUPQuestionToInsert = new List<UP_Question__c>();
        UP_Batch__c upBatch = new UP_Batch__c();
        for(Integer i=0;i<20;i++){
            UP_Question__c upQuestn = new UP_Question__c();
            upQuestn.Name = 'test';
            //Populate all the required fields
            listUPQuestionToInsert.add(upQuestn);
        }
        if(listUPQuestionToInsert.size() > 0){
            insert listUPQuestionToInsert;
        }
        upBatch.Name = 'test';
        upBatch.Program__c = 'Balwadi';
        test.startTest();
            insert upBatch;
        test.stopTest();

    }




Regards,
Bhanu Mahesh
Shaijan ThomasShaijan Thomas
@isTest
public class SampleTest {
    public static testmethod void MyMethod(){
        Test.startTest();
		list<UP_Question__c> UPQ = new list<UP_Question__c>();
		for (integer i=1;i<=20;i++){
			UP_Question__c obj = new UP_Question__c(Name='Test');
			UPA.add(obj);
		}
		insert UPA;
		UP_Batch__c UPBatch = new UP_Batch__c ();
		UPBatch.Name='Test';
		UPBatch.Program__c ='Balwadi';
		insert UPBatch;
        Test.stopTest();
    }
 }

try this
silpa garikapatisilpa garikapati
Hi
 I am unable to cover the code coverage for   user  Trigger .  Please help me out.

My Trigger 

trigger UserTrigger on User ( before update) {

    if(Trigger.isBefore && Trigger.isUpdate){
        UserTriggerHandler.afterUpdateEvent(Trigger.newMap, Trigger.oldMap); 
    }
    
}