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 

Please help get the code bulkyfied not getting any idea.

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

ibtesamibtesam

What exactly is your requirement?

you are using a button to call this class and display duplicates on page?

 

That way is should be fine, what error is it throwing?

and did you looked into the possibility of sending List of student object instead of one obj.

satyamsatyam

Hi,

 

Yes, i am doing mass operation on multiple record for this i am clicking a button filling a form and save the record.So while mass operation i am calling this method again and again.

 

Yes i can send it using list but dont know how i will proceeed from there ,how  i can use map there.Can you please help on this.Please find below whole code:

 

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