+ Start a Discussion
anandanandanandanand 

Test class for trigger

How to write test class for trigger

am new to apex

 

trigger check on Order__c (after insert) {
Order__c to=trigger.new[0];

Opportunity q=[select id from Opportunity where id=:to.Opportunity__c];
list<OpportunityLineItem> ql=[select id,ListPrice,PriceBookEntry.Name,PriceBookEntry.Product2Id ,Subtotal,TotalPrice from OpportunityLineItem where OpportunityId=:q.id];


for(OpportunityLineItem qli:ql){
Accomodation__c lm=new Accomodation__c();
lm.Price__c=qli.ListPrice;
lm.Name=qli.PriceBookEntry.Name;
lm.Order__c=to.id;
insert lm;
}
}

Best Answer chosen by Admin (Salesforce Developers) 
rmehrmeh

Hi,

 

Before helping you in the test coverage, I wanted to ask you whether your trigger is bulk enabled?

If Yes, are you aware of the "Apex Governor Limits".

Just a small tip, never insert any dml statement in a for loop

Take a List of Type Accomodation and add the object there and outside the for loop insert the list.

for example:

 

 

trigger check on Order__c (after insert) 
{
List<Accomodation__c> lstAcccomodation = new List<Accomodation__c>(); // insert this line
Order__c to=trigger.new[0];

Opportunity q=[select id from Opportunity where id=:to.Opportunity__c];
list<OpportunityLineItem> ql=[select id,ListPrice,PriceBookEntry.Name,PriceBookEntry.Product2Id ,Subtotal,TotalPrice from OpportunityLineItem where OpportunityId=:q.id];


for(OpportunityLineItem qli:ql){
Accomodation__c lm=new Accomodation__c();
lm.Price__c=qli.ListPrice;
lm.Name=qli.PriceBookEntry.Name;
lm.Order__c=to.id;
insert lm; // instead of this line write the below line
lstAcccomodation.add(lm);
}
/* insert below code outside the for loop*/
if(lstAcccomodation != null && !lstAcccomodation.isEmpty())
{
  insert lstAcccomodation;
}
}

 And here are the guidelines for your test coverage.

 

Create a class with the Template as "Test Class".

 

private class Test {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        // insert a dummy instance of "Opportunity" sobject
        Opportunity objOpp = new Opportunity();
        /* 
           put all the mandatory fields required in Opportunity object
        */
        insert objOpp;

        // insert a dummy instance of "Order sobject"
        Order__c objOrder = new Order__c();
        objOrder.Opportunity__c = objOpp.Id;
        /* 
           put all the mandatory fields required in Order object
        */
        insert objOrder;

        // insert a dummy instance of "OpportunityLineItem" object
        OpportunityLineItem objOppLineItem = new OpportunityLineItem();
        objOppLineItem.OpportunityId = objOpp.Id;
        objOppLineItem.Order__c = objOrder.Id;
        /* 
          put all mandatory fields and the ones you have queried in the list 
        */
        insert objOppLineItem;
    }
}

 

 

This should give you atleast the minimum code coverage for your trigger.

 

 

All Answers

rmehrmeh

Hi,

 

Before helping you in the test coverage, I wanted to ask you whether your trigger is bulk enabled?

If Yes, are you aware of the "Apex Governor Limits".

Just a small tip, never insert any dml statement in a for loop

Take a List of Type Accomodation and add the object there and outside the for loop insert the list.

for example:

 

 

trigger check on Order__c (after insert) 
{
List<Accomodation__c> lstAcccomodation = new List<Accomodation__c>(); // insert this line
Order__c to=trigger.new[0];

Opportunity q=[select id from Opportunity where id=:to.Opportunity__c];
list<OpportunityLineItem> ql=[select id,ListPrice,PriceBookEntry.Name,PriceBookEntry.Product2Id ,Subtotal,TotalPrice from OpportunityLineItem where OpportunityId=:q.id];


for(OpportunityLineItem qli:ql){
Accomodation__c lm=new Accomodation__c();
lm.Price__c=qli.ListPrice;
lm.Name=qli.PriceBookEntry.Name;
lm.Order__c=to.id;
insert lm; // instead of this line write the below line
lstAcccomodation.add(lm);
}
/* insert below code outside the for loop*/
if(lstAcccomodation != null && !lstAcccomodation.isEmpty())
{
  insert lstAcccomodation;
}
}

 And here are the guidelines for your test coverage.

 

Create a class with the Template as "Test Class".

 

private class Test {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        // insert a dummy instance of "Opportunity" sobject
        Opportunity objOpp = new Opportunity();
        /* 
           put all the mandatory fields required in Opportunity object
        */
        insert objOpp;

        // insert a dummy instance of "Order sobject"
        Order__c objOrder = new Order__c();
        objOrder.Opportunity__c = objOpp.Id;
        /* 
           put all the mandatory fields required in Order object
        */
        insert objOrder;

        // insert a dummy instance of "OpportunityLineItem" object
        OpportunityLineItem objOppLineItem = new OpportunityLineItem();
        objOppLineItem.OpportunityId = objOpp.Id;
        objOppLineItem.Order__c = objOrder.Id;
        /* 
          put all mandatory fields and the ones you have queried in the list 
        */
        insert objOppLineItem;
    }
}

 

 

This should give you atleast the minimum code coverage for your trigger.

 

 

This was selected as the best answer
anandanandanandanand

Thank u very much rmeh

it works fine