+ Start a Discussion
satyamsatyam 

Please help to Resolve Too man SOQL :101 problem

Hi,

 

I am doing mass update in a class and while doing mass operation i am checking duplicate record based on some condition for this i am calling a method again and again and getting this error can anyone please help how i can bulkyfy my code.code is below,your help will be really appreciated:

 

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+'\'';
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;
}

 

 

Thanks:))))

V SubhashV Subhash

Hi Satyam,

 

I guess, you might call "checkDuplicatestudentdatamasschange" method from a for loop.

 

Please send complete code. Or if you can stop calling this method from for loop then your problem willl solve.

 

Regards,

Subhash

satyamsatyam

Hi,

 

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);
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

 

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:))