+ Start a Discussion
NikiG22NikiG22 

Need a Higher Coverage %

Can anyone help me move my coverage for this trigger to a higher %? any help is appriciated.

 

Lines not Covered

:

trigger trg_new_AdProductFulfillment on Opportunity (after update)
{
   List<Opportunity> closedWonOpps=new List<Opportunity>();

   for (Opportunity opp : trigger.new)
   {
       Opportunity oldOpp=trigger.oldMap.get(opp.id);
       if ( (opp.StageName=='Closed Won') &&
            (oldOpp.StageName!='Closed Won')&&
            (opp.Message_Sent__c== False))
       {
          closedWonOpps.add(opp);
       }
   }
    
   if (!closedWonOpps.isEmpty())
   {
    List<OpportunityLineItem> olis = [SELECT ID, OpportunityId,Account_Manager_ID__c,Opportuntiy_Owner_id__c,PO_IO_Number__c, Account_ID__c,Standard_RateproductID__c,Closed_Won_Date__c,
        Quantity FROM OpportunityLineItem WHERE Ad_Product_Fulfillment__c ='True' and
        OpportunityId in :closedWonOpps];

    List<Ad_Product_Fulfillment__c> afToInsert=new List<Ad_Product_Fulfillment__c>();
   for (OpportunityLineItem oli : olis)
   {
    
    For (integer i = 0; i < oli.Quantity; i++){
            Ad_Product_Fulfillment__c newaf= new Ad_Product_Fulfillment__c();
            newaf.Opportunity__c = oli.Opportunityid;
            newaf.Placement__c = oli.Standard_RateproductID__c;
            newaf.Account__c = oli.Account_ID__c;
            newaf.IO_Start_Date__c = oli.Closed_Won_Date__c;
            newaf.PO_IO_Number__c = oli.PO_IO_Number__c;
            newaf.Quantity__c = oli.Quantity;
            newaf.Expiration_Date__c = oli.Closed_Won_Date__c +365;
            newaf.Account_Executive__c = oli.Opportuntiy_Owner_id__c;
            newaf.Account_Manager__c = oli.Account_Manager_ID__c;
            afToInsert.add(newaf);
       }
          }

    insert afToInsert;
   }
}

 

 

Here is my Test Class:

@isTest(SeeAllData=True)

Public class Testtrg_new_AdProductFulfillment{

static testmethod void Testtrg_new_AdProductFulfillment()

{


List<Opportunity> closedWonOpps=new List<Opportunity>();

Opportunity opp = new Opportunity();
opp.StageName='Closed Won';
opp.Message_Sent__c= False;
opp.Name = 'Test';
opp.Account_Manager__c = '00550000000saAA';
opp.CloseDate =Date.newInstance(2012, 01, 15);
opp.Product_Interest__c = 'Job Posting';
opp.Billing_Email__c = 'test@otj.com';
opp.New_Dollar_Amount__c=100;
insert opp;

       {
          closedWonOpps.add(opp);
       }

List<OpportunityLineItem> olis = [SELECT ID, OpportunityId,Account_Manager_ID__c,Opportuntiy_Owner_id__c,PO_IO_Number__c, Account_ID__c,Standard_RateproductID__c,Closed_Won_Date__c,
        Quantity FROM OpportunityLineItem WHERE Ad_Product_Fulfillment__c ='True'and
        OpportunityId in :closedWonOpps];
        
OpportunityLineItem oppt = new OpportunityLineItem();
oppt.OpportunityId=opp.Id;
oppt.Quantity =1;
oppt.Cost_Type__c ='weekly';
oppt.TotalPrice =100;
oppt.PricebookEntryId='01u50000003SHVz';
insert oppt;

    List<Ad_Product_Fulfillment__c> afToInsert=new List<Ad_Product_Fulfillment__c>();
   {
Ad_Product_Fulfillment__c adg= new Ad_Product_Fulfillment__c();
            adg.Opportunity__c = '006W0000002VxWF';
            adg.Placement__c = '01t50000001YRdD';
            adg.Account__c = '001W0000004yqYJ';
            adg.IO_Start_Date__c = Date.newInstance(2012, 01, 15);
            adg.PO_IO_Number__c = '123';
            adg.Quantity__c = 1;
            adg.Expiration_Date__c = Date.newInstance(2012, 01, 15);
            adg.Account_Executive__c = '00550000000saAA';
            adg.Account_Manager__c = '00550000000saAA';
insert afToInsert;
}

}}

 Please help!

 

Thank you

Niki

SFRichSFRich

You need to put test data in your test code that makes this condition true:

    opp.StageName=='Closed Won') &&  (oldOpp.StageName!='Closed Won')&&  (opp.Message_Sent__c== False

 

and you need do the the same to make this condition true:

(!closedWonOpps.isEmpty())

 

Then those red lines will be executed and covered.

PhoenixedPhoenixed
Hi Niki, 
I dont know the schema for your application modify the test class data field value as per your schema. I just added couple of data in your test class.
Try out this :
@isTest(SeeAllData=True)
Public class Testtrg_new_AdProductFulfillment{
static testmethod void Testtrg_new_AdProductFulfillment()
{
List<Opportunity> closedWonOpps=new List<Opportunity>();
Account Acc = new Account( Name = 'Testing');
insert Acc;
Placement__c Pal = new Placement__c(Name = 'Test Data');
insert pal;
Opportunity opp = new Opportunity();
opp.StageName='Closed Won';
opp.AccountID = Acc.id;
opp.Placement__c = pal.id;
opp.Account_Executive__c = userinfo.getUserId();
opp.Account_Manager__c = userinfo.getUserId();
opp.Message_Sent__c= False;
opp.Name = 'Test';
opp.Account_Manager__c = userinfo.getUserId();
opp.CloseDate =Date.newInstance(2012, 01, 15);
opp.Product_Interest__c = 'Job Posting';
opp.Billing_Email__c = 'test@otj.com';
opp.New_Dollar_Amount__c=100;
Ad_Product_Fulfillment__c ='True';
insert opp;
PricebookEntry PB= new PricebookEntryId(Name = 'Testing');
insert PB;
OpportunityLineItem oppt = new OpportunityLineItem();
oppt.OpportunityId=opp.Id;
oppt.Quantity =1;
oppt.Cost_Type__c ='weekly';
oppt.TotalPrice =100;
oppt.PricebookEntryId=PB.Id;
insert oppt;
opp.StageName='Closed Won';
update opp;
}
}
Hope it works :)
Thanks,
Phoenixed.
NikiG22NikiG22

Thank you for all your help!

 

its almost all covered. this is the only part that is not.

 

 

 

trigger trg_new_AdProductFulfillment on Opportunity (after update)
{
   List<Opportunity> closedWonOpps=new List<Opportunity>();

   for (Opportunity opp : trigger.new)
   {
       Opportunity oldOpp=trigger.oldMap.get(opp.id);
       if ( (opp.StageName=='Closed Won') &&
            (oldOpp.StageName!='Closed Won')&&
            (opp.Message_Sent__c== False))
       {
          closedWonOpps.add(opp);
       }
   }
    
   if (!closedWonOpps.isEmpty())
   {
    List<OpportunityLineItem> olis = [SELECT ID, OpportunityId,Account_Manager_ID__c,Opportuntiy_Owner_id__c,PO_IO_Number__c, Account_ID__c,Standard_RateproductID__c,Closed_Won_Date__c,
        Quantity FROM OpportunityLineItem WHERE Ad_Product_Fulfillment__c ='True' and
        OpportunityId in :closedWonOpps];

    List<Ad_Product_Fulfillment__c> afToInsert=new List<Ad_Product_Fulfillment__c>();
   for (OpportunityLineItem oli : olis)
   {
    
    For (integer i = 0; i < oli.Quantity; i++){
            Ad_Product_Fulfillment__c newaf= new Ad_Product_Fulfillment__c();
            newaf.Opportunity__c = oli.Opportunityid;
            newaf.Placement__c = oli.Standard_RateproductID__c;
            newaf.Account__c = oli.Account_ID__c;
            newaf.IO_Start_Date__c = oli.Closed_Won_Date__c;
            newaf.PO_IO_Number__c = oli.PO_IO_Number__c;
            newaf.Quantity__c = oli.Quantity;
            newaf.Expiration_Date__c = oli.Closed_Won_Date__c +365;
            newaf.Account_Executive__c = oli.Opportuntiy_Owner_id__c;
            newaf.Account_Manager__c = oli.Account_Manager_ID__c;
            afToInsert.add(newaf);
       }
          }

    insert afToInsert;
   }
}

 

Here is my code:

 

@isTest(SeeAllData=True)
Public class Testtrg_new_AdProductFulfillment{

static testmethod void Testtrg_new_AdProductFulfillment()


{
List<Opportunity> closedWonOpps=new List<Opportunity>();

Account Acc = new Account( Name = 'Testing');
Acc.BillingState ='CO';
Acc.BillingCountry = 'United States';
insert Acc;

Opportunity opp = new Opportunity();
opp.StageName='Closed Won';
opp.AccountID = Acc.id;
opp.Ownerid = userinfo.getUserId();
opp.Account_Manager__c = userinfo.getUserId();
opp.Message_Sent__c= False;
opp.Name = 'Test';
opp.Account_Manager__c = userinfo.getUserId();
opp.CloseDate =Date.newInstance(2012, 01, 15);
opp.Product_Interest__c = 'Job Posting';
opp.Billing_Email__c = 'test@otj.com';
opp.New_Dollar_Amount__c=100;
insert opp;


OpportunityLineItem oppt = new OpportunityLineItem();
oppt.OpportunityId=opp.Id;
oppt.Ad_Product_Fulfillment__c ='True';
oppt.Quantity =1;
oppt.PricebookEntryId='01u50000003SHVz';
oppt.Cost_Type__c ='weekly';
oppt.TotalPrice =100;

insert oppt;
opp.StageName='Closed Won';
update opp;


Ad_Product_Fulfillment__c adg= new Ad_Product_Fulfillment__c();
            adg.Opportunity__c=opp.Id;
            adg.Placement__c = '01t50000001YRdD';
            adg.Account__c = Acc.id;
            adg.IO_Start_Date__c = Date.newInstance(2012, 01, 15);
            adg.PO_IO_Number__c = '123';
            adg.Quantity__c = 1;
            adg.Expiration_Date__c = Date.newInstance(2012, 01, 15);
            adg.Account_Executive__c = '00550000000saAA';
            adg.Account_Manager__c = '00550000000saAA';
insert adg;
}
}

 

Rajesh SriramuluRajesh Sriramulu

Hi

 

 

Here is 100% code coverage.

Try this

 

@isTest(SeeAllData=True)

Private class opp_Test{

static testmethod void AdProductFulfillment_Test()
{
List<Opportunity> closedWonOpps=new List<Opportunity>();
Opportunity opp = new Opportunity();
opp.StageName='Closed Won';
opp.Message_Sent__c= False;
opp.Name = 'Test';
Opp.StageName='Qualification';
opp.Message_Sent__c=false;
//opp.Account_Manager_ID__c = '005saAA';
opp.CloseDate =Date.newInstance(2012, 01, 15);
//opp.Product_Interest__c = 'Job Posting';
//opp.Billing_Email__c = 'test@otj.com';
//opp.New_Dollar_Amount__c=100;
 closedWonOpps.add(opp);
insert closedWonOpps;
Account acc = new Account();
acc.Description='test';
acc.name='test';
insert acc;

 
 
Contact con = new contact();
con.lastname='test';
insert con;

sObject s = [select ID from Pricebook2 where IsStandard = TRUE];

//for (Product2 newProduct: Trigger.new) {
product2 pro = new product2();
pro.Name='test';
//pro.Product_Id__c
insert pro;

PricebookEntry z = new PricebookEntry(Pricebook2Id=s.ID,Product2Id=pro.Id, UnitPrice=0.00, IsActive=TRUE, UseStandardPrice=FALSE);
insert z;


OpportunityLineItem oppp = new OpportunityLineItem();
oppp.OpportunityId=opp.Id;
oppp.Account_Manager_ID__c='tetere';
oppp.Opportunity_Owner_id__c=con.Id;
oppp.PO_IO_Number__c=123464;
oppp.Account_ID__c=acc.Id;
oppp.Quantity=23.56;
oppp.Standard_RateproductID__c='test';
oppp.Closed_Won_Date__c=system.today();
oppp.Ad_Product_Fulfillment__c='True';
oppp.TotalPrice=12356.20;
oppp.PricebookEntryId=z.Id;
insert oppp;

           
    List<Ad_Product_Fulfillment__c> afToInsert=new List<Ad_Product_Fulfillment__c>();
 
Ad_Product_Fulfillment__c adg= new Ad_Product_Fulfillment__c();
            adg.Opportunity__c = opp.Id;
            adg.Placement__c = oppp.Standard_RateproductID__c;
            adg.Account__c = oppp.Account_ID__c;
            adg.IO_Start_Date__c =oppp.Closed_Won_Date__c;
            adg.PO_IO_Number__c = oppp.PO_IO_Number__c;
            adg.Quantity__c = oppp.Quantity;
            adg.Closed_Won_Date__c=oppp.Closed_Won_Date__c;
           // adg.Standard_RateproductID__c='sgetre4534';
            //adg.Account_ID__c='43dfdferw';
           // adg.Opportunity_Owner_id__c='3423dfd';
            adg.Expiration_Date__c = oppp.Closed_Won_Date__c +365;
            adg.Account_Executive__c = oppp.Opportunity_Owner_id__c;
            adg.Account_Manager__c = oppp.Account_Manager_ID__c;
            afToInsert.add(adg);
insert afToInsert;
opp.StageName='Closed Won';
 update opp;
    
    }

}

 

Regards,

Rajesh.