You need to sign in to do that
Don't have an account?

Help to solve Trigger error
Hi Experts,
Help needed to solve the below error in trigger.
execution of AfterInsert caused by: System.QueryException: Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact salesforce.com about custom indexing. Even if a field is indexed a filter might still not be selective when: 1. The filter value includes null (for instance binding with a list that contains null) 2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times): Trigger.insertOppsLineitem: line 31, column 1
Thanks in Advance
Help needed to solve the below error in trigger.
execution of AfterInsert caused by: System.QueryException: Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact salesforce.com about custom indexing. Even if a field is indexed a filter might still not be selective when: 1. The filter value includes null (for instance binding with a list that contains null) 2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times): Trigger.insertOppsLineitem: line 31, column 1
trigger insertOppsLineitem on OrderItems__c (after insert, after update) { System.debug('--- Inside orderItems Trigger ---'); Set<String> orderIds = new Set<String>(); List<Opportunitylineitem> oliinsertList = new List<Opportunitylineitem>(); List<Opportunitylineitem> oliupdateList = new List<Opportunitylineitem>(); for(OrderItems__c oi : Trigger.new) { orderIds.add(oi.Name); orderIds.add(oi.Product_Id__c); System.debug('--- OrderItems Id: ---' + oi.Name); System.debug('--- Product Name: ---' + oi.Product_Id__c); } Map<String, List<Opportunity>> oppMap = new Map<String, List<Opportunity>>(); List<Opportunitylineitem> oppliList = new List<Opportunitylineitem>(); oppliList = [SELECT Id, OpportunityId, Name__c, Name, product_id__c FROM Opportunitylineitem WHERE product_id__c IN: orderIds]; Map<String, List<pricebookentry>> pricebookMap = new Map<String, List<pricebookentry>>(); //Error below query::: List<Pricebookentry> pbeList = new List<Pricebookentry>(); pbeList = [SELECT Id, Name, Pricebook2Id, Pricebook2.Name, Product2Id, IsActive, Product2.entity_id__c FROM Pricebookentry WHERE IsActive = true AND Pricebook2.Name = 'Standard Price Book' AND Product2.entity_id__c IN: orderIds]; if(!orderIds.isEmpty()) { for(Opportunity opps : [SELECT Id, Name FROM Opportunity WHERE Name IN: orderIds]){ if(!oppMap.containsKey(opps.Name)){ oppMap.put(opps.Name, new List<Opportunity> {opps}); System.debug('--- Inside OppMap ---' + oppMap); } else{ List<Opportunity> oppList = oppMap.get(opps.Name); oppList.add(opps); oppMap.put(opps.Name, oppList); System.debug('--- Else oppMap ---' + oppList); } } for(Pricebookentry pbe : pbeList) { if(!pricebookMap.containsKey(pbe.Product2.entity_id__c)) { pricebookMap.put(pbe.Product2.entity_id__c, new List<Pricebookentry> {pbe}); } else { List<Pricebookentry> pbeList = pricebookMap.get(pbe.Product2.entity_id__c); pbeList.add(pbe); pricebookMap.put(pbe.Product2.entity_id__c, pbeList); } } } Pricebookentry pbe = [SELECT Id, Name, isActive FROM Pricebookentry WHERE isActive = true AND Name = 'Zookal Product' Limit 1]; // Insert Opportunity Products. if(Trigger.isInsert && Trigger.isAfter) { for(OrderItems__c oi : Trigger.New) { System.debug('--- Name of Order Items: ---' + oppMap); if(oppMap.containsKey(oi.Name)) { //Inserting into Opportunity with Opportunity Name. for(Opportunity opp : oppMap.get(oi.Name)) { System.debug('--- Name of Order Items: ---' + oppMap.get(oi.Name)); if(pricebookMap.containsKey(oi.Product_Id__c)) { //Getting the Pricebookentry Name and Inserting Opportunity Lineitem. for(Pricebookentry pe : pricebookMap.get(oi.Product_Id__c)) { System.debug('--- Name of Order Items: ---' + pricebookMap.get(oi.Product_Id__c)); Opportunitylineitem oli = new Opportunitylineitem (); oli.OpportunityId = opp.Id; oli.PricebookEntryId = pe.Id; oli.Quantity = 1; oli.TotalPrice = oi.Price__c; oli.item_id__c = oi.item_id__c; oli.Name__c = oi.Name__c; oli.product_id__c = oi.product_id__c; oli.Due_Date__c = oi.Due_Date__c; oli.product_type__c = oi.product_type__c; oli.qty_backordered__c = oi.qty_backordered__c; oli.qty_canceled__c = oi.qty_canceled__c; oli.qty_invoiced__c = oi.qty_invoiced__c; oli.qty_ordered__c = oi.qty_ordered__c; oli.qty_refunded__c = oi.qty_refunded__c; oli.qty_returned__c = oi.qty_returned__c; oli.qty_shipped__c = oi.qty_shipped__c; oli.Discount_Amount__c = oi.Discount_Amount__c; oli.Sku__c = oi.Sku__c; oliinsertList.add(oli); } } else { Opportunitylineitem oli = new Opportunitylineitem (); oli.OpportunityId = opp.Id; oli.PricebookEntryId = pbe.Id; oli.Quantity = 1; oli.TotalPrice = oi.Price__c; oli.item_id__c = oi.item_id__c; oli.Name__c = oi.Name__c; oli.product_id__c = oi.product_id__c; oli.Due_Date__c = oi.Due_Date__c; oli.product_type__c = oi.product_type__c; oli.qty_backordered__c = oi.qty_backordered__c; oli.qty_canceled__c = oi.qty_canceled__c; oli.qty_invoiced__c = oi.qty_invoiced__c; oli.qty_ordered__c = oi.qty_ordered__c; oli.qty_refunded__c = oi.qty_refunded__c; oli.qty_returned__c = oi.qty_returned__c; oli.qty_shipped__c = oi.qty_shipped__c; oli.Discount_Amount__c = oi.Discount_Amount__c; oli.Sku__c = oi.Sku__c; oliinsertList.add(oli); } } } } } // Update Opportunity Lineitem Method. if(Trigger.isUpdate) { List<Opportunitylineitem> oliLists = new List<Opportunitylineitem>(); Set<String> oliIds = new Set<String>(); Map<String, List<Opportunitylineitem>> oppliMap = new Map<String, List<Opportunitylineitem>>(); for(OrderItems__c oi : Trigger.New) { oliIds.add(oi.Name__c); System.debug('--- Order Ids ---' + oi.Name__c); } oliLists = [SELECT Id, Name, Name__c FROM Opportunitylineitem WHERE Name__c IN: oliIds]; System.debug('--- oliLists ---' + oliLists.size()); for(OrderItems__c oi : Trigger.New) { for(Opportunitylineitem olits : oppliList) { olits.Quantity = 1; olits.TotalPrice = oi.Price__c; olits.item_id__c = oi.item_id__c; olits.Name__c = oi.Name__c; olits.product_id__c = oi.product_id__c; olits.Due_Date__c = oi.Due_Date__c; olits.product_type__c = oi.product_type__c; olits.qty_backordered__c = oi.qty_backordered__c; olits.qty_canceled__c = oi.qty_canceled__c; olits.qty_invoiced__c = oi.qty_invoiced__c; olits.qty_ordered__c = oi.qty_ordered__c; olits.qty_refunded__c = oi.qty_refunded__c; olits.qty_returned__c = oi.qty_returned__c; olits.qty_shipped__c = oi.qty_shipped__c; olits.Discount_Amount__c = oi.Discount_Amount__c; olits.Sku__c = oi.Sku__c; oliupdateList.add(olits); } } } //Insert and Update Methods. try { if(oliinsertList.size() > 0) { insert oliinsertList; } if(oliupdateList.size() > 0) { update oliupdateList; } System.debug('--- Inserted Opportunitylineitem List: ---' + oliinsertList.size()); System.debug('--- Updated Opportunitylineitem List: ---' + oliupdateList.size()); } catch (Exception e) { System.debug('The Following Exception has occurred: '+ e.getLinenumber() + ' : ' + e.getMessage()); } }
Thanks in Advance
If you're running a trigger on objects that have more than 200,000 records, it's possible that you'll receive the error, "System.QueryException: Non-selective query against large object type." We'll go over the a few possible fixes.
Refer this links
https://help.salesforce.com/articleView?id=000002493&type=1