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
nitinkhunal.ax1786nitinkhunal.ax1786 

Please tell me how to write Test class for this trigger

trigger Incr on Service1__c (before insert) {

Increment__c incr=new Increment__c(name='Service1__c', In_Guarantee__c=1001, Out_of_Guarantee__c=2001, etc__c=3001);

for(Increment__c inc:[select id, name, In_Guarantee__c, Out_of_Guarantee__c, etc__c from Increment__c where name='Service1__c'])

incr=inc;

for(Service1__c ser:Trigger.new)
{
if(ser.Status__c=='In Guarantee')
{
ser.S_No__c=Integer.valueOf(incr.In_Guarantee__c++);
}
if(ser.Status__c=='Out of Guarantee')
{
ser.S_No__c=Integer.valueOf(incr.Out_of_Guarantee__c++);
}
if(ser.Status__c=='etc')
{
ser.S_No__c=Integer.valueOf(incr.etc__c++);
}
}
upsert incr;

}

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi Nitin,

 

Below is the Test class For your Trigger.

 

Test Class

@istest
public class TesttrgIncr{
    Private Static testmethod void TesttrgIncr(){
        Service1__c objService1 = new Service1__c();
        objService1.Name = 'Test service';
        objService1.Status__c = 'In Guarantee';
        insert objService1;
        
        Service1__c objService2 = new Service1__c();
        objService2.Name = 'Test service';
        objService2.Status__c = 'Out of Guarantee';
        insert objService2;
        
        Service1__c objService3 = new Service1__c();
        objService3.Name = 'Test service';
        objService3.Status__c = 'etc';
        insert objService3;
    }
}

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

 

All Answers

hitesh90hitesh90

Hi Nitin,

 

Below is the Test class For your Trigger.

 

Test Class

@istest
public class TesttrgIncr{
    Private Static testmethod void TesttrgIncr(){
        Service1__c objService1 = new Service1__c();
        objService1.Name = 'Test service';
        objService1.Status__c = 'In Guarantee';
        insert objService1;
        
        Service1__c objService2 = new Service1__c();
        objService2.Name = 'Test service';
        objService2.Status__c = 'Out of Guarantee';
        insert objService2;
        
        Service1__c objService3 = new Service1__c();
        objService3.Name = 'Test service';
        objService3.Status__c = 'etc';
        insert objService3;
    }
}

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

 

This was selected as the best answer
KaityKaity

Hitesh,

Just to know, since in this trigger SOQL has been written within for loop.

Will it hit the governor limit?

 

trigger Incr on Service1__c (before insert) {

Increment__c incr=new Increment__c(name='Service1__c', In_Guarantee__c=1001, Out_of_Guarantee__c=2001, etc__c=3001);

for(Increment__c inc:[select id, name, In_Guarantee__c, Out_of_Guarantee__c, etc__c from Increment__c where name='Service1__c'])

incr=inc;

for(Service1__c ser:Trigger.new)
{
if(ser.Status__c=='In Guarantee')
{
ser.S_No__c=Integer.valueOf(incr.In_Guarantee__c++);
}
if(ser.Status__c=='Out of Guarantee')
{
ser.S_No__c=Integer.valueOf(incr.Out_of_Guarantee__c++);
}
if(ser.Status__c=='etc')
{
ser.S_No__c=Integer.valueOf(incr.etc__c++);
}
}
upsert incr;

}

 

hitesh90hitesh90

Hi Kaity,

 

The SOQL has not been Written within for Loop.

It has been used instead of List variable.

 

 

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

KaityKaity

ohkkkk...

 

Thanks Hitesh.