0

Can anyone please help to solve this ?

I am not using any DML inside the For loop but still I am getting the following error:

Too many SOQL queries: 101 Error is in expression '{!Save}' in component in page addgroupforquote: Class.AddGroupForQuoteController.Save: line 69, column 1

APEX CLASS :

public class AddGroupForQuoteController{

public Quote quote{get;set;}
public boolean lstSections{get;set;}
public boolean lstSections1{get;set;}
public boolean lstSections2{get;set;}
public boolean editSection1{get;set;}
public Group__c grp{get;set;}
public String partNumber{get;set;}
public String Description{get;set;}
public List<Bundle__c> bundlewithProduct{get;set;}
Public Product_Quantity__c PrdQuantity{get;set;}
public List<Product_Quantity__c> productWithValues{get;set;}
public Quote_Bundle_Product__c QBP{get;set;}
public List<Quote_Bundle_Product__c> QBPList{get;set;}
Public Map<Id, Product2> mapProductIdToProduct{get;set;}

public AddGroupForQuoteController(ApexPages.StandardController controller) {
    lstSections = false;
    lstSections1 = false;
    lstSections2 = false;
    editSection1 = false;
    grp = new Group__c();
    QBP = new Quote_Bundle_Product__c();
    QBPList = new List<Quote_Bundle_Product__c>();
    this.quote = (Quote)controller.getRecord();
    PrdQuantity = new Product_Quantity__c();
    productWithValues = new List<Product_Quantity__c>();
    mapProductIdToProduct = new Map<Id, Product2>();
}

public void retrieveproductInfo() {
    for(Product2 product : [SELECT Id, ProductCode, Description FROM Product2]) {
            mapProductIdToProduct.put(product.Id, product);
    }
    if(PrdQuantity.Product__c != Null){
        this.partNumber = mapProductIdToProduct.get(PrdQuantity.Product__c).ProductCode;
        this.Description = mapProductIdToProduct.get(PrdQuantity.Product__c).Description;
    }
}

public void retrieveBundleInfo() {
    bundlewithProduct = [SELECT Id, (select Id, Name, Product__c, Quantity__c from Product_Quantities__r) FROM Bundle__c WHERE Id =: QBPList.get(0).Bundle__c];
}

public PageReference addSection() {
    lstSections = true;
    return null;
}

public void addSection1() {
    lstSections1 = true;
    PrdQuantity = new Product_Quantity__c();
    if(PrdQuantity.Product__c != Null){
        this.partNumber = mapProductIdToProduct.get(PrdQuantity.Product__c).ProductCode;
        this.Description = mapProductIdToProduct.get(PrdQuantity.Product__c).Description;
    }
    productWithValues.add(PrdQuantity);
}

public PageReference addSection2() {
    lstSections2 = true;
    return null;
}

public PageReference editSection1(){
    //editSection1 = true;
    lstSections1 = true;
    return null;
}

public Pagereference Save(){
    if(grp.Name != Null){
        upsert grp;
        QBP.Group__c = grp.Id;
        insert QBP;
        for(integer i=0; i<productWithValues.size(); i++){
            this.partNumber = mapProductIdToProduct.get(PrdQuantity.Product__c).ProductCode;
            this.Description = mapProductIdToProduct.get(PrdQuantity.Product__c).Description;
            PrdQuantity.Quote_Bundle_Product__c = QBP.Id;
            productWithValues.add(PrdQuantity);
        }
        upsert productWithValues;
    }else{
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Group Name'));
    }
    return null;
}

public Pagereference Cancel(){
    PageReference pg = new PageReference('/apex/AddGroupForQuote?id='+quote.Id);
    pg.setRedirect(True);
    return pg;
}

}

VISUALFORCE :

<apex:pageBlockButtons location="Bottom">
<apex:commandButton value="Add Product" action="{!addSection1}"/>
<apex:commandButton value="Save" action="{!Save}"/>
<apex:commandButton value="Cancel" action="{!Cancel}"/>
</apex:pageBlockButtons>
<apex:outputPanel >
<div>
<ul id="sortable">
 <apex:variable value="" var="foo" rendered="{!IF(lstSections1 = True, true, false)}">
        <apex:pageBlockSection title="Select Product">
            <apex:actionRegion >
                <apex:pageBlockTable value="{!QBP}" var="QBP">
                <apex:column >
                    <apex:outputLabel >Select Product :</apex:outputLabel>
                </apex:column>
                <apex:column >
                <apex:inputField value="{!QBP.Product__c}" >
                    <apex:actionSupport event="onchange" rerender="relatedInfo" action="{!retrieveproductInfo}" immediate="false"/>
                </apex:inputField>
                </apex:column>
                <apex:column >
                    <apex:pageBlockSection id="relatedInfo">
                        <apex:outputText value="{!partNumber}" label="Part Number :"/>
                        <apex:outputText value="{!Description}" label="Description :"/>
                    </apex:pageBlockSection>
                </apex:column>
                </apex:pageBlockTable>
            </apex:actionRegion>
        </apex:pageBlockSection>
</apex:variable>
</ul>
</div>        
</apex:outputPanel>

Apex CPU time limit exceeded

2
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Matt Lacey
    Jan 30, 2017 at 0:32
  • @MattLacey thanks for the reply...by the way i got the solution and going to answer :) Jan 30, 2017 at 6:34

1 Answer 1

1

I was just doing a silly mistake to add multiple record into related list of parent and the changed code as following :

Just made the changes in SAVE method and it worked for me:

public Pagereference Save(){
    if(grp.Name != Null){
        upsert grp;
        QBP.Group__c = grp.Id;
        insert QBP;
        for(integer i=0; i<productWithValues.size(); i++){
            productWithValues.get(i).Quote_Bundle_Product__c = QBP.Id;
            upsert productWithValues;
        }
    }else{
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Group Name'));
    }
    return null;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .