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
iyappan kandasamy 4iyappan kandasamy 4 

Triggers test classes

This is the original code for trigger
---------------------------------------------
updateQuoteLineItem = new List<QuoteLineItem>();
        for (QuoteLineItem qli : MapQLI.values()) {
            if(mapQuoteOppty.get(qli.Id) != null) {
                String OppID = mapQuoteOppty.get(qli.Id);
                OpportunityLineItem OLI = MapOLI.get(OppID);

                qli.Event__c = OLI.Event__c;
                qli.Opp_Line_Item_Id__c = OLI.Id;
                qli.Opportunity_Line_Item_Name__c = OLI.Name;
                qli.ServiceDate = OLI.ServiceDate;
                qli.Onsite_Additional_Price__c = OLI.Onsite_Additional_Price__c;
                qli.Tax__c = OLI.Tax__c;
                qli.Tax_Rate__c = OLI.Tax_Rate__c;
                qli.Promo_Code__c = OLI.Promo_Code__c;
                updateQuoteLineItem.add(qli);
            }

Need test class for the above code.
It is very urgent...Please do the needful....Thanks in advance
Naren9Naren9
Hi iyappan,
Please can you provide me the complete trigger, so that we can understand the functionality and can write a test class.
In the given trigger, what is MapQLI.values means. MapQLI is variable defined some where in the Trigger.

Thanks,
Naren
 
iyappan kandasamy 4iyappan kandasamy 4
Thanks Naren...
Below is the entire code....

trigger QuoteLineItemTrigger on QuoteLineItem (after insert, after update){

    List<QuoteLineItem> newQlis = Trigger.new;
    Map<Id, QuoteLineItem> oldQlis = Trigger.oldMap;

    // Collecting reference Ids.
    Set<Id> quoteIds = new Set<Id>();
    Set<Id> oppIds = new Set<Id>();
    Map<Id, Quote> parentQuotes;

    if (Trigger.isInsert || Trigger.isUpdate) {

        for (QuoteLineItem qli : newQlis) {
            quoteIds.add(qli.QuoteId);
        }
        parentQuotes = new Map<Id, Quote>([SELECT Id FROM Quote WHERE Id IN: quoteIds]);

        for (Quote quote : [SELECT Id, Name, OpportunityId, IsSyncing FROM Quote WHERE Id IN: quoteIds]) {
            oppIds.add(quote.OpportunityId);
        }
    }


    List<QuoteLineItem> updateQuoteLineItem;
    if (Trigger.isInsert) {
        String JSONContent = Json.Serialize(newQlis);
        JSONParser parser = JSON.createParser(JSONContent);
        System.debug('parser-------->'+parser );

        List<String> OpptyLineId = new List<String>();
        List<String> QuoteLineId = new List<String>();

        while (parser.nextToken() != null) {
            if (parser.getCurrentToken() == JSONToken.VALUE_STRING && parser.getCurrentName() == 'OpportunityLineItemId') {
                OpptyLineId.add(parser.getText());
            }

            if(parser.getCurrentToken() == JSONToken.VALUE_STRING && parser.getCurrentName() == 'Id') {
                QuoteLineId.add(parser.getText());
            }

            parser.nextToken();

            if(parser.getCurrentToken() == JSONToken.VALUE_STRING && parser.getCurrentName() == 'OpportunityLineItemId') {
                OpptyLineId.add(parser.getText());
            }

            if(parser.getCurrentToken() == JSONToken.VALUE_STRING && parser.getCurrentName() == 'Id') {
                QuoteLineId.add(parser.getText());
            }
        }
        System.debug('OpptyLineId-------->'+OpptyLineId);
        System.debug('QuoteLineId-------->'+QuoteLineId);


        Map<String, String> mapQuoteOppty = new Map<String, String>();
        Integer iCount = 0;
        for (String strOppLineId : OpptyLineId) {
            String iQuoteLineId = QuoteLineId[iCount];
            mapQuoteOppty.put(iQuoteLineId, strOppLineId);
            iCount++;
        }

        Map<Id, OpportunityLineItem> MapOLI = new Map<Id, OpportunityLineItem>([
            SELECT Id, Name, ServiceDate, Tax__c, Tax_Rate__c, Promo_Code__c, Onsite_Additional_Price__c, OpportunityId, Event__c
            FROM OpportunityLineItem WHERE OpportunityId IN: oppIds
        ]);

        Map<Id, QuoteLineItem> MapQLI = new Map<Id, QuoteLineItem>([
            SELECT Id, ServiceDate, Event__c, Opp_Line_Item_Id__c, Opportunity_Line_Item_Name__c, Tax__c, Tax_Rate__c, Promo_Code__c, Onsite_Additional_Price__c
            FROM QuoteLineItem WHERE QuoteId IN: quoteIds
        ]);
        
        
        updateQuoteLineItem = new List<QuoteLineItem>();
        for (QuoteLineItem qli : MapQLI.values()) {
            if(mapQuoteOppty.get(qli.Id) != null) {
                String OppID = mapQuoteOppty.get(qli.Id);
                OpportunityLineItem OLI = MapOLI.get(OppID);

                qli.Event__c = OLI.Event__c;
                qli.Opp_Line_Item_Id__c = OLI.Id;
                qli.Opportunity_Line_Item_Name__c = OLI.Name;
                qli.ServiceDate = OLI.ServiceDate;
                qli.Onsite_Additional_Price__c = OLI.Onsite_Additional_Price__c;
                qli.Tax__c = OLI.Tax__c;
                qli.Tax_Rate__c = OLI.Tax_Rate__c;
                qli.Promo_Code__c = OLI.Promo_Code__c;
                updateQuoteLineItem.add(qli);
            }
        }

        try {
            if (updateQuoteLineItem != null && !updateQuoteLineItem.isEmpty()) {
                update updateQuoteLineItem;
            }
        } catch (Exception e) {
            System.debug(e.getMessage());
        }
    }

Kindly do the needfull.
Thanks in advance....


 
Naren9Naren9
Hi Iyappan,
use the below code. Below code covers 73%, you need to edit this to cover the lines which are not covered in red color.

@isTest(SeeAllData=true)
public class QuoteLineItemTriggerTest {

    @isTest Static void docTest() 
{
    //get standard pricebook
    Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];

    Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
    insert pbk1;

    Product2 prod = new Product2(Name='test1',Family='Liner', IsActive=true);
    insert prod;

    PricebookEntry pbe = new PricebookEntry (Product2Id=prod.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
    insert pbe;
     Opportunity opp = new Opportunity(Pricebook2Id = standardPb.id,Name='test Oppty',CloseDate=system.today(),CurrencyIsoCode='USD', StageName='Prospecting');
    insert opp;
    
    OpportunityLineItem OpptyLineItem = new OpportunityLineItem(Product2Id=prod.id,OpportunityId=opp.id,PriceBookEntryID=pbe.id,Quantity=4, UnitPrice =pbe.UnitPrice);
        insert OpptyLineItem;
    List<OpportunityLineItem> OpptyLineItem2 = [select id from OpportunityLineItem limit 1];
    Id OpportunityLineItemId=OpptyLineItem2.get(0).Id;

    Quote qt = new Quote(Name='test quote',OpportunityId=opp.id,Pricebook2Id = standardPb.id, ExpirationDate=system.today());
    insert qt;
        QuoteLineItem qliliner = new QuoteLineItem(Product2Id=prod.id,QuoteId=qt.id,PriceBookEntryID=pbe.id,Quantity=4, UnitPrice =pbe.UnitPrice);
        insert qliliner;
        qliliner.Quantity=10;
    update qliliner;   
      
    System.debug('LineItemName'+qliliner.Product2Id);
}

}
User-added image
Thanks,
Narendar
Naren9Naren9
It is not enetring to those loops due to code on these lines.
I am not sure, why you have this code.
at any time, parser.getCurrentName() is always gives the null values. Due to that, OpptyLineId is always null.
as OpptyLineId is null, it is not entering to other codes.

 while (parser.nextToken() != null) {
                    if (parser.getCurrentToken() == JSONToken.VALUE_STRING && parser.getCurrentName() == 'OpportunityLineItemId') {
                        OpptyLineId.add(parser.getText());
                    }
        
                    if(parser.getCurrentToken() == JSONToken.VALUE_STRING && parser.getCurrentName() == 'Id') {
                        QuoteLineId.add(parser.getText());
                    }
        
                    parser.nextToken();
        
                    if(parser.getCurrentToken() == JSONToken.VALUE_STRING && parser.getCurrentName() == 'OpportunityLineItemId') {
                        OpptyLineId.add(parser.getText());
                    }
        
                    if(parser.getCurrentToken() == JSONToken.VALUE_STRING && parser.getCurrentName() == 'Id') {
                        QuoteLineId.add(parser.getText());
                    }
Naren9Naren9
Comment out this code in your trigger and test whether it is covering 75% or not.

// while (parser.nextToken() != null) {
               //     if (parser.getCurrentToken() == JSONToken.VALUE_STRING && parser.getCurrentName() == 'OpportunityLineItemId') {
                //        OpptyLineId.add(parser.getText());
                //   }
        
                 //   if(parser.getCurrentToken() == JSONToken.VALUE_STRING && parser.getCurrentName() == 'Id') {
                    //    QuoteLineId.add(parser.getText());
                  //  }
        
                    //parser.nextToken();
        
                    //if(parser.getCurrentToken() == JSONToken.VALUE_STRING && parser.getCurrentName() == 'OpportunityLineItemId') {
                    //    OpptyLineId.add(parser.getText());
                    //}
        
                   // if(parser.getCurrentToken() == JSONToken.VALUE_STRING && parser.getCurrentName() == 'Id') {
                   //     QuoteLineId.add(parser.getText());
                   // }
               // }


One more thing:
I didn't understand why you are using this code.

 Map<String, String> mapQuoteOppty = new Map<String, String>();
                Integer iCount = 0;
                for (String strOppLineId : OpptyLineId) {
                    String iQuoteLineId = QuoteLineId[iCount];
                    mapQuoteOppty.put(iQuoteLineId, strOppLineId);
                    iCount++;
                }
iyappan kandasamy 4iyappan kandasamy 4
After commenting we are getting only 60% test coverage....
So please enhance the test class and can you provide us...
Its urgent please....Thanks in advance....
Naren9Naren9
What lines of Apex class is not covering. Provide me the screen shot of class which is not covering.

Thanks,
Naren
Naren9Naren9
In the above trigger, you have 3 custom fields - Opportunity_Product_1__c,Opportunity_Product_2__c,Opportunity_Product_3__c
These are text fields or lookup fields or any other type?.

Thanks,
Naren
Robin ShiraRobin Shira
Hi,
Any update please.
Thanks
Naren9Naren9
Hi Robin,
I didn't have a time to look into this. Please check with others.