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
akhil Vakhil V 

Test Class

please help out to coverage of trigger
List<Contract_Product__c> contractProducts = new List<Contract_Product__c>();
                
                for(OpportunityLineItem oli : Trigger.new)
                {
                    opportunityIds.add(oli.OpportunityId);
                    productIds.add(oli.Product2Id);
                }
                
                List<Product2> products = [select id,
                                                ProductCode from Product2 where id in: productIds];
            
                for(Product2 p : products)
                {
                    productMap.put(p.id, p.ProductCode);
                    
                }
            
                List<Contract> contractList = [select id,
                                                      Opportunity_Name__c 
                                                      from Contract 
                                                      where Opportunity_Name__c in: opportunityIds];
                for(String s : opportunityIds)
                {
                    List<String> cList = new List<String>();
                    for(Contract c : contractList)
                    {
                        if(c.Opportunity_Name__c == s)
                        {
                            //contractMap.put(c.Opportunity_Name__c, c.id);
                            cList.add(c.id);
                        }
                    }
                    contractListMap.put(s, cList);
                }
                
                
                System.debug('************ '+contractListMap);
            // Block for Insert Opp Line Items
            
            if(Trigger.isInsert)
            {
                
                for(OpportunityLineItem oli : Trigger.new)
                {
                    
                    if(contractListMap.get(oli.OpportunityId) != null)
                    {
                        System.debug('% '+contractListMap.get(oli.OpportunityId));
                        for(String s : contractListMap.get(oli.OpportunityId))
                        {
                            Contract_Product__c c = new Contract_Product__c();
                            System.debug('333333333333'+s);
                            c.Contract__c = s;
                            c.ServiceDate__c = oli.ServiceDate;
                            c.Discount__c = oli.Discount;
                            c.Description__c = oli.Description;
                            c.ListPrice__c = oli.ListPrice;
                            c.Opp_Line_Item_Id__c = oli.id;
                            c.Product__c = oli.Product2Id;
                            c.ProductCode__c = productMap.get(oli.Product2Id);
                            c.Quantity__c = oli.Quantity;
                            c.UnitPrice__c = oli.UnitPrice;
                            
                            contractProducts.add(c);
                        }
                    }
                }
                System.debug('++++++++++++'+contractProducts);
                if(contractProducts.size()>0)
                {
                    insert contractProducts;
                }
                System.debug('-------------- '+contractProducts);
            }
            
            // Block for update line items
            
            if(Trigger.isUpdate)
            {
                List<String> oppLineItemIds = new List<String>();
                List<String> unmatchedOppLineItemIds = new List<String>();
                for(OpportunityLineItem oli : Trigger.new)
                {
                    oppLineItemIds.add(oli.id);
                }
                
                List<Contract_Product__c> contractProdList = [select id, 
                                                                        Contract__c,
                                                                        ServiceDate__c,
                                                                        Discount__c,
                                                                        Description__c,
                                                                        ListPrice__c,
                                                                        Opp_Line_Item_Id__c,
                                                                        Product__c,
                                                                        ProductCode__c,
                                                                        Quantity__c,
                                                                        UnitPrice__c from Contract_Product__c 
                                                                        where Opp_Line_Item_Id__c in : oppLineItemIds ];
                
                
                delete contractProdList;
                
                for(OpportunityLineItem oli : Trigger.new)
                {
                    
                    if(contractListMap.get(oli.OpportunityId) != null)
                    {
                        System.debug('% '+contractListMap.get(oli.OpportunityId));
                        for(String s : contractListMap.get(oli.OpportunityId))
                        {
                            Contract_Product__c c = new Contract_Product__c();
                            System.debug('333333333333 update'+s);
                            c.Contract__c = s;
                            c.ServiceDate__c = oli.ServiceDate;
                            c.Discount__c = oli.Discount;
                            c.Description__c = oli.Description;
                            c.ListPrice__c = oli.ListPrice;
                            c.Opp_Line_Item_Id__c = oli.id;
                            c.Product__c = oli.Product2Id;
                            c.ProductCode__c = productMap.get(oli.Product2Id);
                            c.Quantity__c = oli.Quantity;
                            c.UnitPrice__c = oli.UnitPrice;
                            
                            contractProducts.add(c);
                        }
                    } 
ManojjenaManojjena
Hi Akil,

Have you tried with any test class .What is the error you are getting .Please shere I will definitly help you .
akhil Vakhil V
Hi Manoj,
Thanks for reply this trigger is on opportunitylineitem i got coverage with 67 passed succesfully but it didnt covered if(trigger.isupdate) part  
Note:inserted account,contact,opportunity,opportunitylineitem and contract,contract_product__C need to cover this part. help me for this 
if(Trigger.isUpdate)
            {
                List<String> oppLineItemIds = new List<String>();
                List<String> unmatchedOppLineItemIds = new List<String>();
                for(OpportunityLineItem oli : Trigger.new)
                {
                    oppLineItemIds.add(oli.id);
                }
                
                List<Contract_Product__c> contractProdList = [select id, 
                                                                        Contract__c,
                                                                        ServiceDate__c,
                                                                        Discount__c,
                                                                        Description__c,
                                                                        ListPrice__c,
                                                                        Opp_Line_Item_Id__c,
                                                                        Product__c,
                                                                        ProductCode__c,
                                                                        Quantity__c,
                                                                        UnitPrice__c from Contract_Product__c 
                                                                        where Opp_Line_Item_Id__c in : oppLineItemIds ];
                
                
                delete contractProdList;
                
                for(OpportunityLineItem oli : Trigger.new)
                {
                    
                    if(contractListMap.get(oli.OpportunityId) != null)
                    {
                        System.debug('% '+contractListMap.get(oli.OpportunityId));
                        for(String s : contractListMap.get(oli.OpportunityId))
                        {
                            Contract_Product__c c = new Contract_Product__c();
                            System.debug('333333333333 update'+s);
                            c.Contract__c = s;
                            c.ServiceDate__c = oli.ServiceDate;
                            c.Discount__c = oli.Discount;
                            c.Description__c = oli.Description;
                            c.ListPrice__c = oli.ListPrice;
                            c.Opp_Line_Item_Id__c = oli.id;
                            c.Product__c = oli.Product2Id;
                            c.ProductCode__c = productMap.get(oli.Product2Id);
                            c.Quantity__c = oli.Quantity;
                            c.UnitPrice__c = oli.UnitPrice;
                            
                            contractProducts.add(c);
                        }
                    }