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
satyamsatyam 

Too Many SOQL:101 problem

Hi ,

 

I am getting too many SOQL problem ,can anyone please help to resolve,its very urgent.

 

i,

 

Please find my Custom Save code from where i am calling the method 'checkDuplicatestudentdatamasschange'

 

I am not able to recognize how i can create a Map for this checking purpose and this functionality is important for me.

 

Custom Save Method:

 

public void customSave(){


double actualDiscountOne = 0.0;
double actualDiscountTwo = 0.0;
double actualDiscountOneSelling = 0.0;
double actualDiscountTwoSelling = 0.0;
List<Student_Support__c> clonedStudentSupports =null;
if(!renewFlag){
clonedStudentSupports = StudentSupportUtil.cloneStudentSupports(objectName, selectedStudentSupports,StudentSupportUtil.CHANGE,expirationDate,effectiveDate);
}else{
clonedStudentSupports = StudentSupportUtil.cloneStudentSupports(objectName, selectedStudentSupports,StudentSupportUtil.RENEW,expirationDate,null);
}
List<Student_Support__c> updatedCloneList = new List<Student_Support__c>();
for(Student_Support__c studentObj1 : clonedStudentSupports){
SuppprofileIds.add(studentObj1.Student_Profile_ID__c);
}
System.debug('-----------StudentsupportsuppprofileId-------'+SuppprofileIds);
List<Seeker_Profile__c> SuppprofileeObjList =[select Round_Final_Support_to__c from Student_Profile__c where Id in:SuppprofileIds];

for (Integer k = 0; k < SuppprofileeObjList.size(); k++) {
SuppprofileIdRoundno.put(SuppprofileeObjList[k].id,SuppprofileeObjList[k].Round_Final_Support_to__c);
}
System.debug('-----------StudentsupportsuppprofileIdMap-------'+SuppprofileIdRoundno);


for(Student_Support__c studentObj : clonedStudentSupports){

if(studentObj.Support_Type__c==StudentSupportUtil.DEBATE){

if(studentObj.Flat_or_Purchased__c==StudentSupportUtil.FLAT && studentObj.Debate_Editable__c==true){

System.debug('----------FLAT Student Support------');
studentObj.Requested_Debate_Amount__c = nullCheck(studentObj.Requested_Debate_Amount__c) + nullCheck(increaseDebateAmountBy);

}else{

System.debug('------old Purchase Cost-------'+studentObj.Purchase_Cost__c);

System.debug('------Purchase cost Type-------'+purchaseCostType);

System.debug('-----purchaseCostChange-------'+purchaseCostChange);

if(purchaseCostType.equalsIgnoreCase('$')){
studentObj.Purchase_Cost__c = nullCheck(studentObj.Purchase_Cost__c) + nullCheck(purchaseCostChange);
}else if(purchaseCostType.equalsIgnoreCase('%')){
studentObj.Purchase_Cost__c = nullCheck(studentObj.Purchase_Cost__c)+(nullCheck(studentObj.Purchase_Cost__c)*nullCheck(purchaseCostChange));
}

System.debug('------Modified Purchase Cost-------'+studentObj.Purchase_Cost__c);

System.debug('------old Seeker_Supported_Student__c-------'+studentObj.Seeker_Supported_Student__c);

System.debug('------supportedStudentType-------'+supportedStudentType);

System.debug('-----seekerSupportedStudentChange-------'+seekerSupportedStudentChange);

if(supportedStudentType.equalsIgnoreCase('$')){
studentObj.Seeker_Supported_Student__c = nullCheck(studentObj.Seeker_Supported_Student__c) + nullCheck(seekerSupportedStudentChange);
}else if(supportedStudentType.equalsIgnoreCase('%')){
studentObj.Seeker_Supported_Student__c = nullCheck(studentObj.Seeker_Supported_Student__c)+(nullCheck(studentObj.Seeker_Supported_Student__c)*nullCheck(seekerSupportedStudentChange));
}

System.debug('------Modified supportedStudent-------'+studentObj.Seeker_Supported_Student__c);

if(studentObj.X1st_Discount_Type_Buy__c!=null && studentObj.X1st_Discount_Type_Buy__c.equalsIgnoreCase('%')){
actualDiscountOne=studentObj.Purchase_Cost__c*(nullCheck(studentObj.X1st_Discount_Buy__c)/100);
}else{
actualDiscountOne = studentObj.X1st_Discount_Buy__c;
}

if(studentObj.X2nd_Discount_Type_Buy__c!=null && studentObj.X2nd_Discount_Type_Buy__c.equalsIgnoreCase('%')){
actualDiscountTwo=studentObj.Purchase_Cost__c*(nullCheck(studentObj.X2nd_Discount_Buy__c)/100);
}else{
actualDiscountTwo = studentObj.X2nd_Discount_Buy__c;
}


if(studentObj.X1st_Discount_Type_Sell__c!=null && studentObj.X1st_Discount_Type_Sell__c.equalsIgnoreCase('%')){
actualDiscountOneSelling=studentObj.Seeker_Supported_Student__c*(nullCheck(studentObj.X1st_Discount_Sell__c)/100);
}else{
actualDiscountOneSelling = studentObj.X1st_Discount_Sell__c;
}

if(studentObj.X2nd_Discount_Type_Sell__c!=null && studentObj.X2nd_Discount_Type_Sell__c.equalsIgnoreCase('%')){
actualDiscountTwoSelling=studentObj.Seeker_Supported_Student__c*(nullCheck(studentObj.X2nd_Discount_Sell__c)/100);
}else{
actualDiscountTwoSelling = studentObj.X2nd_Discount_Sell__c;
}

studentObj.Net_Purchase_Cost__c = nullCheck(studentObj.Purchase_Cost__c)-nullCheck(actualDiscountOne)-nullCheck(actualDiscountTwo)+nullCheck(studentObj.Freight_Allowance_Buying__c)+nullCheck(studentObj.Other__c);

String Roundsupport1=nullChecknew(SuppprofileIdRoundno.get(studentObj.Seeker_Profile_ID__c));
Integer Roundsupport2=Integer.valueof(Roundsupport1.trim());
system.debug('--------------Roundsupport2----------'+Roundsupport1);
studentObj.Net_Support_Cost__c = nullCheck(studentObj.Seeker_Supported_Student__c) - (nullCheck(actualDiscountOneSelling)+nullCheck(actualDiscountTwoSelling)) - nullCheck(studentObj.Packaging_Allowance__c) - nullCheck(studentObj.Freight_Allowance_Selling__c) - nullCheck(studentObj.Other_Allowances__c);

System.debug('----------Net purchase Cost------'+studentObj.Net_Purchase_Cost__c);
System.debug('----------Net support Cost------'+studentObj.Net_Support_Cost__c);

studentObj.Requested_Debate_Amount__c = (studentObj.Net_Purchase_Cost__c - studentObj.Net_Support_Cost__c).setscale(Roundsupport2);

System.debug('----------Requested_Debate_Amount__c------'+studentObj.Requested_Debate_Amount__c);
}


}/*else if(studentObj.Support_Type__c==StudentSupportUtil.TOPFRONT){

System.debug('------old Purchase Cost-------'+studentObj.Purchase_Cost__c);

System.debug('------Purchase cost Type-------'+purchaseCostType);

System.debug('-----purchaseCostChange-------'+purchaseCostChange);

if(purchaseCostType.equalsIgnoreCase('$')){
studentObj.Purchase_Cost__c = nullCheck(studentObj.Purchase_Cost__c) + nullCheck(purchaseCostChange);
}else if(purchaseCostType.equalsIgnoreCase('%')){
studentObj.Purchase_Cost__c = nullCheck(studentObj.Purchase_Cost__c)+(nullCheck(studentObj.Purchase_Cost__c)*nullCheck(purchaseCostChange));
}

System.debug('------Modified Purchase Cost-------'+studentObj.Purchase_Cost__c);


}*/
if(comments!=null && !comments.equals('')){
// studentObj.Comments__c = System.now()+' '+comments +'\n' + nullCheck(studentObj.Comments__c);
studentObj.Comments__c = comments +'\n' + nullCheck(studentObj.Comments__c);
}
//Setting the default value of review date to 9999/12/31 as per the request from Business users.
//For Mass changed and Mass renew with student changed records
//Salesforce max limit is 4000 so setting that.
//studentObj.Review_Date__c = date.newInstance(4000, 12, 31);

if(validate(studentObj)){
System.debug('------Agreement Number ---------'+studentObj.Agreement_Number__c);
updatedCloneList.add(studentObj);
}

}
System.debug('------updatedCloneList ---------'+updatedCloneList);
insert updatedCloneList;

List<Checkited_Pricing__c> clonedCheckitedPricing = StudentSupportUtil.cloneCheckitedPricing(updatedCloneList);

//Added as per Charlotte. Purchase Cost for Topfront means all the 'Student' in checkited pricing

List<Checkited_Pricing__c> updatedClonedCheckitedPricing = new List<Checkited_Pricing__c>();

for(Checkited_Pricing__c pricingObj:clonedCheckitedPricing){


if(purchaseCostType.equalsIgnoreCase('$')){
pricingObj.Student__c = nullCheck(pricingObj.Student__c) + nullCheck(purchaseCostChange);
}else if(purchaseCostType.equalsIgnoreCase('%')){
pricingObj.Student__c = nullCheck(pricingObj.Student__c) +(nullCheck(pricingObj.Student__c)*nullCheck(purchaseCostChange));
}

updatedClonedCheckitedPricing.add(pricingObj);
}

List<Checkited_Pricing__c> newUpdatedClonedCheckitedPricing = StudentSupportUtil.doTopfrontCalculation(updatedClonedCheckitedPricing);

if(newUpdatedClonedCheckitedPricing!=null && !newUpdatedClonedCheckitedPricing.isEmpty()){

insert newUpdatedClonedCheckitedPricing;

}
}

private double nullCheck(decimal val){
if(val==null){
return 0.0;
}else{
return val;
}
}
private String nullCheck(String val){
if(val==null){
return '';
}else{
return val;
}
}
private String nullChecknew(String val){
if(val==null){
return '0';
}else{
return val;
}
}



private boolean validate(Student_Support__c studentObj){


boolean status = true;

List <String> duplicates = StudentSupportUtil.checkDuplicateStudentSupportforMassChange(studentObj);(I am getting problem here this queery is getting run again and again)
if(duplicates!=null && !duplicates.isEmpty()){
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error, 'Support # ' '+duplicates +' record found.'));
status= false;
}

if(status && studentObj.Support_Type__c=='Debate'){
if(studentObj.Flat_or_Purchased__c==StudentSupportUtil.PURCHASED){
if(studentObj.Net_Support_Cost__c==null||studentObj.Net_Support_Cost__c<=0){
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error,’msg.'));
status= false;
}
}else{
if(studentObj.Requested_Debate_Amount__c==null || studentObj.Requested_Debate_Amount__c<=0){
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error,'Support # 'msg.'));
status= false;
}
}

}

return status;

}



}

 

 

Method in Utill Class :checkDuplicatestudentdatamasschange(Thsi method is getting called again and again can anyone correct what i can use show that SQL :101 not occur)

 

public static List<String> checkDuplicatestudentdatamasschange(Student__c studobj){

Date effectiveDate = studobj.Effective_Date__c;
Date expirationDate= studobj.Expiration_Date__c;
List<String> duplicatesS= new List<String>();
String studentrecordid= studobj.Parent_Student__c;
List<Student__c> studsupport= null;
String soql = 'select id,Parent_Student__c,name,Agreement_Number__c from Student__c where (status__c=\'Approved\' OR status__c =\'Awaiting Approval\' OR status__c =\'Awaiting Stu Approval\' OR status__c =\'Conditionally Approved\' OR status__c =\'Rejected by Stud\')';

if (studobj.Supplier_Profile_ID__c!=null)
soql += ' and Student_Profile_ID__c = \''+studobj.Student_Profile_ID__c+'\'';
if (studobj.teacher__c!=null)
soql += ' and teacher___c = \''+studobj.teacher__c+'\'';

 if (studObj.Effective_Date__c!=null)
            soql += ' and ((Expiration_Date__c = null  or Expiration_Date__c > :effectiveDate) and (Effective_Date__c < :expirationDate))';
        if (stud.Parent_stud_Support__c != null)
            soql += ' and Id != \''+studj.Parent_Price_Support__c+'\'';
System.debug('----------soql-------------------'+soql);

try {
studsupport= Database.query(soql + ' order by Effective_Date__c asc' );
} catch (Exception e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
}
if(studsupport!=null && !studsupport.isEmpty()){

for(Student__c support:studsupport){
duplicatesS.add(support.Agreement_Number__c);
}

}
System.debug('----------duplicates-------------------'+duplicatesPS);
return duplicatesS;
}

 

 

Please help.

 

 

Thanks:))

Vinit_KumarVinit_Kumar

Can you check debug logs.I am pretty much sure some other transactions also running in the same context.

satyamsatyam
Hi vinit,

No any other transaction is running i am doing mass operation for multiple records and when i am calling this method its getting called again and again.

what i should so it should call only once.
satyamsatyam
Hi,

i am doing mass operation ,and while doing mass operation i am calling above method for checking duplicates if present but this i am calling again and again in for loop .

Can you please to reduce the call.

How i can do this.

Thanks:))
Vinit_KumarVinit_Kumar

Satyam,

 

Create a helper class in your org like below :-

 

public class HelperClass {   

    private static boolean flagvalue = false;


    public static boolean hasAlreadyfired() {
        return flagvalue;
    }
    
    public static void setAlreadyfired() {
        flagvalue = true;
    }


}

And use it in your code like below :-

 

public void customSave(){

if(!HelperClass.hasAlreadyfired()){

 

// do your code here

 

}

HelperClass.setAlreadyfired();

}

 

satyamsatyam

Hi,

 

Thanks fo your reply!!!

 

Can you please provide any idea if i am passing a list in this method and then from there how i can use Map.

 

And if possible can you please elaborate more your code you pasted and how i can use it in my code.And what this helper class is doing here and how i can use it in my code for the method which is causing issue.

 

Thanks:)

 

Vinit_KumarVinit_Kumar

In my helper class,I am creating a boolean property and setting its value to false.In hasAlreadyfired(),I am returning that value and in setAlreadyfired(),I am setting ot to true.

 

Now,the issue is with checkDuplicatestudentdatamasschange() method so you should be using it in that metho s not in customsave() one like below :-

 

public void public static List<String> checkDuplicatestudentdatamasschange(Student__c studobj)(){

if(!HelperClass.hasAlreadyfired()){

 

// do your code here

 

}

HelperClass.setAlreadyfired(); // This would prevent method being called in a loop

}

 

 

 

satyamsatyam
Hi,

Thank You!!!

But one question here if first time means for first record this method will be fired and Boolean property will set to false then for second record this method will not work ?????

Can you please,clarify my doubt or it will work????

Thanks
:))))
Vinit_KumarVinit_Kumar

As you said earlier that this method is being called in a loop,this helper class would prevent it to do so.I am not sure how you are calling thie method ,I will have to see about your 2nd part as in whether it would work for 2nd record or not.