+ Start a Discussion
oceanofstarsoceanofstars 

help with trigger with govener limits

Hi i wrote a trigger its hiting govener limit for soql query limi101 so can any one hlp me

 

trigger ProductInsert on Product2 (after insert)
{
    //declarations 
    PricebookEntry[] newPBEntry = new PricebookEntry[0];
   
    //get the active standard pricebook
    Pricebook2 standardPB = [select id from pricebook2 where isstandard = true and isactive = true limit 1];
   
    //  create a list of the new productids
    Set<Id> pIds = new Set<Id>();
        for(Product2 pId:Trigger.new)
    {
        pIds.add(pId.Id);      
    } 
   
    //existing pb map, this is for when a record is cloned, it will also clone the pb and we dont need to create it
    Map<Id, PricebookEntry> existingPBMap = new Map<Id, PricebookEntry>();
    for(PricebookEntry ePB:[select id, Product2Id from PricebookEntry where Pricebook2Id = :standardPB.Id
            and Product2Id in :pIds])
    {
            existingPBMap.put(ePB.Product2Id, ePB);
    }
       
   
    //loop through products
    for(Product2 p:Trigger.new)
    {
        if(existingPBMap.get(p.Id) == null)
        {
            PricebookEntry pb = new PricebookEntry();
            pb.UnitPrice = 0;          
            pb.Product2Id = p.Id;
            pb.Pricebook2Id = standardPB.Id;
            pb.IsActive = true;
            newPBEntry.add(pb);
        }
    }
   
    //create the pricebookentry records
    insert newPBEntry;
}

 

in red color soql query is hiting a limit can any one plz help me

 

Thanks

VPrakashVPrakash

Try this 

 

 

trigger ProductInsert on Product2 (after insert)
{
    //declarations 
    PricebookEntry[] newPBEntry = new PricebookEntry[0];
   
    //get the active standard pricebook
    Pricebook2 standardPB = [select id from pricebook2 where isstandard = true and isactive = true limit 1];
   
    //  create a list of the new productids
    Set<Id> pIds = new Set<Id>();
        for(Product2 pId:Trigger.new)
    {
        pIds.add(pId.Id);      
    } 
   pricebookentry[] pbs= [select id, Product2Id from PricebookEntry where Pricebook2Id = :standardPB.Id and Product2Id in :pIds];
    //existing pb map, this is for when a record is cloned, it will also clone the pb and we dont need to create it
    Map<Id, PricebookEntry> existingPBMap = new Map<Id, PricebookEntry>();
    for(PricebookEntry ePB: pbs)
    {
            existingPBMap.put(ePB.Product2Id, ePB);
    }
       
   
    //loop through products
    for(Product2 p:Trigger.new)
    {
        if(existingPBMap.get(p.Id) == null)
        {
            PricebookEntry pb = new PricebookEntry();
            pb.UnitPrice = 0;          
            pb.Product2Id = p.Id;
            pb.Pricebook2Id = standardPB.Id;
            pb.IsActive = true;
            newPBEntry.add(pb);
        }
    }
   
    //create the pricebookentry records
    insert newPBEntry;
}

oceanofstarsoceanofstars

Thanks for replay but its not working  when i run a test class its still showing same error

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ProductInsert: execution of AfterInsert caused by: System.Exception: Too many SOQL queries: 101 Trigger.ProductInsert: line 29, column 27: []

In red its the error so plz help me with that

Thanks

AhmedPotAhmedPot

You can check whether there is any recursive call of any trigger which might be increasing your query count. Your debug log can tell you where the count is increasing. I suspect some repeated callout is happening in this case.

oceanofstarsoceanofstars

I think you are right anonther query increase the count query is

 

 Map<String, String> productIdByMasterProductId = new Map<String, String>();
        for(MasterProduct__c mp : [
                SELECT Id, (
                    SELECT Id, Name, Variable_Ind__c FROM Products__r
                    WHERE NPA__c = :opportunity.Exchange__r.NPA__c 
                    AND NXX__c = :opportunity.Exchange__r.NXX__c
                )
                FROM MasterProduct__c WHERE Id IN :masterProductIds
            ]) 

 

I try to change the query but its throwing erorr

i changed like this

 

MasterProduct__c[] mps : [
                SELECT Id, (
                    SELECT Id, Name, Variable_Ind__c FROM Products__r
                    WHERE NPA__c = :opportunity.Exchange__r.NPA__c 
                    AND NXX__c = :opportunity.Exchange__r.NXX__c
                )
                FROM MasterProduct__c WHERE Id IN :masterProductIds
            ];

Map<String, String> productIdByMasterProductId = new Map<String, String>();
        for(MasterProduct__c mp : mps)

 

but its give me erorr that invaild type MasterProduct__c