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
Traveller MadeTraveller Made 

Code coverage Apex trigger and Apex Class

I tried to import an Apex trigger and Class I created so that when an opportunity is created a quote is created, with the opportunity's product if it has one. We said me that we have to have 75%code coverage to import it. So I have to create an Apex test. But above my trigger and my class, it is already written 100%, so I don't understand.

Also, my Apex test is only 62%... And I don't know how to increase it. 

/// TRIGGER

trigger Facturation on Opportunity (after insert) {

        List<Id> oppIds = new List<Id>();

        for (Opportunity o : Trigger.new)
            oppIds.add(o.id);

        CreateQuote.createQuote(oppIds);   

}

/// CLASS

public class CreateQuote {

    @future

    public static void createQuote(List<Id> oppIds) {
        List<Quote> lstQ = new List<Quote>();

        //Fetching all the OLIs belonging to the Opportunities of Trigger.new
        List<OpportunityLineItem> olis =[select id, OpportunityId, quantity, PriceBookEntry.Product2Id, UnitPrice, PricebookentryId from OpportunityLineItem where OpportunityId in :oppIds];
        List<Opportunity> opps =[select id, name, CloseDate, Type, Pricebook2Id from Opportunity where Id in :oppIds];

        //Preparing a Map of each Opportunity and their corrresponding LineItems  ( Id of the opportunity => List of the OLIs that belong to the opportunity)
        Map<Id,List<OpportunityLineItem>> mapOppIdOli = new Map<Id,List<OpportunityLineItem>>();
        for (OpportunityLineItem oli : olis) {
            if (mapOppIdOli.containsKey(oli.OpportunityId)) {
                mapOppIdOli.get(oli.OpportunityId).add(oli);
            } else {
                List<OpportunityLineItem> lstOlis = new List<OpportunityLineItem>();
                lstOlis.add(oli);
                mapOppIdOli.put(oli.OpportunityId, lstOlis);
            }
        }

        //Preparing the new quotes - one for each Opportunity
        //List<Opportunity> opps =[select id, name, CloseDate, Pricebook2Id from Opportunity where Id in :oppIds];
        for (Opportunity o : opps) {
            if (o.Type == 'Event') {
                Quote q = new Quote();
                //q.name = 'Credit Note ' + o.name;
                q.name = o.name;
                //q.Montant_1_re_ch_ance__c = 1000;
                q.Date_de_Facture__c = o.CloseDate;
                q.Date_1_re_ch_ance__c = o.CloseDate;
                q.opportunityId = o.id;
                q.Pricebook2Id= o.Pricebook2Id;
                lstQ.add(q);
            }  
        }

        //Inserting the new quotes
        insert lstQ;

        //Preparing the new QLIs - one for each OLI
        List<QuoteLineItem> lstQLI = new List<QuoteLineItem>();
        for (Quote q : lstQ) {
            List<OpportunityLineItem> lstOlis = mapOppIdOli.get(q.OpportunityId);
            if (lstOlis != null) {
                for (OpportunityLineItem oli : lstOlis) {
                    QuoteLineItem qli = new QuoteLineItem();
                    qli.quoteId = q.Id;
                    qli.UnitPrice = oli.UnitPrice;
                    qli.Product2Id = oli.PriceBookEntry.Product2Id;
                    qli.Quantity = oli.Quantity;
                    qli.PriceBookentryid = oli.PriceBookentryId;
                    lstQLI.add(qli);
                }
            }
        }
        //Inserting the new QLIs
        insert lstQLI;
    }
}

/// TEST


@isTest

public class CreateQuoteTest {
    static testMethod void insertNewOpportunity() {
        Test.startTest();

        Product2 testProduct = new Product2(
            Name = 'Happy Funtime Ball'

        );

        insert testProduct;

        PricebookEntry pbEntry = new PricebookEntry(
             Pricebook2Id = Test.getStandardPricebookId(),
             Product2Id = testProduct.Id,
             UnitPrice = 100.00,
             IsActive = true

        );

        insert pbEntry;

        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity Triggers',
            CloseDate = date.today().addDays(5),        
            StageName = 'Sourcing Demand',
            Type = 'Event',           
            Account_manager_gescom__c = 'Farida Raji',
            Pricebook2Id = pbEntry.Pricebook2Id                      

        );

        insert testOpportunity;

        OpportunityLineItem testOLI = new OpportunityLineItem(
            OpportunityId = testOpportunity.Id,
            Product2Id = testProduct.Id,
            PricebookEntryId = pbEntry.Id,
            Quantity = 5,
            TotalPrice = 5 * pbEntry.UnitPrice        
            //UnitPrice = 1100.00

        );

        insert testOLI;     
        
        OpportunityLineItem testOLI2 = new OpportunityLineItem(
            OpportunityId = testOpportunity.Id,
            Product2Id = testProduct.Id,
            PricebookEntryId = pbEntry.Id,
            Quantity = 5,
            TotalPrice = 5 * pbEntry.UnitPrice         
            //UnitPrice = 1100.00

        );

        insert testOLI2;  
        
        
        Test.stopTest();

    }

}
 
Best Answer chosen by Traveller Made
v varaprasadv varaprasad
Hi,

please Follow below steps and let me know in case of any issues. 
//Create opportunities records and pass to below method
//Change your metohd name also.class name and method should be differnt always.

test.startTest();
opportunity op1 = new opportunity();
//Add fields
Insert op1;

opportunity op2 = new opportunity();
//Add fields
Insert op2;

set<id> opIds = new set<id>();
opIds.add(op1.id);
opIds.add(op2.id);


CreateQuote.createQuoteMethod(oppids);


Test.stopTest();


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/


Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1


 

All Answers

Traveller MadeTraveller Made
User-added image

The code coverage for the trigger and the apex class is 100% so why doesn't it work ? 
v varaprasadv varaprasad
Hi,

please Follow below steps and let me know in case of any issues. 
//Create opportunities records and pass to below method
//Change your metohd name also.class name and method should be differnt always.

test.startTest();
opportunity op1 = new opportunity();
//Add fields
Insert op1;

opportunity op2 = new opportunity();
//Add fields
Insert op2;

set<id> opIds = new set<id>();
opIds.add(op1.id);
opIds.add(op2.id);


CreateQuote.createQuoteMethod(oppids);


Test.stopTest();


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/


Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1


 
This was selected as the best answer