You need to sign in to do that
Don't have an account?
MaheemSam
Query inside method Number of SOQL queries: 100 out of 100
Hi,
In below method when i call from visualforce page i am getting Number of SOQL queries: 100 out of 100
Please suggest me how to modify the code i think query inside the code is the issue please suggest me how to modifiy
Thanks
Sudhir
In below method when i call from visualforce page i am getting Number of SOQL queries: 100 out of 100
Please suggest me how to modify the code i think query inside the code is the issue please suggest me how to modifiy
/* calculate disti discount */ public static Decimal reCalRslrDisc(Decimal listprice,String productcat, Decimal reslrdistdiscount){ Boolean isService = false; Decimal s; system.debug('listprice = ' + listprice); system.debug('productcat = ' + productcat); system.debug('reslrdistdiscount = ' + reslrdistdiscount); if ( reslrdistdiscount == null || reslrdistdiscount < 1 ){ reslrdistdiscount = 0; } if ( productcat=='C' || productcat=='E'|| productcat=='D'|| productcat=='H'|| productcat=='I'|| productcat=='J' ){ isService = true; } if ( isService == true && Limits.getQueries() < Limits.getLimitQueries()) { try { NSP_Margin_Schedule__c NMS = [ Select Distributor_Discount__c From NSP_Margin_Schedule__c where Reseller_Discount__c = :reslrdistdiscount and Service__c = :isService and createddate < 2015-01-17T00:00:00-08:00 ]; System.debug('Total Number of SOQL Queries allowed in this Apex code context: ' + Limits.getLimitQueries()); System.debug('1. Number of Queries used in this Apex code so far: ' + Limits.getQueries()); s = NMS.Distributor_Discount__c; } catch (System.NullPointerException e) { system.debug('Null Exception'); } return s; } else { return 0; } }
Thanks
Sudhir
The error occurs when your code are executing 100 queries within 1 transaction. From your code that you have shared it doesnt look like its the culprit (unless reCalRslrDisc is called within a for loop). You can turn on the debug log and trace which code is executing the most queries.
Once you have identified the culprit code consider bulkifing it to reduce the number of queries within the transaction.
Your right i am calling the reCalRslrDisc method inside a for loop as mentioned below in another method will this be causing the issue please advice me how to change the code
Thanks
Sudhir
Hi Sudhir,
Yes this can cause issues such as 101 soql error. There are multiple ways you can bulkify your code. Please refer to the below articles on how to bulkify your code
https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
https://developer.salesforce.com/page/Apex_Code_Best_Practices
I suggest to use Maps to fetch all records from your sObject like this before entering inside the Loop :
Once you have this map, you can then change thiis method reCalRslrDisc parameters to accept this NSP_Margin_Schedule__c, and then you can perform your logic...
Hope that helps !
Mark is as resolved if it does.
Please see below code does this work? Please suggest me a change with map code how to fix this issue.
Thanks
Sudhir
This code should be fine for you :
Mark as resolved if it's fine.
I am getting below error from the code I have made the code in bold which is throwing the error Please suggest me how to fix this issue.
SObject row was retrieved via SOQL without querying the requested field: NSP_Margin_Schedule__c.Reseller_Discount__c
Error is in expression '{!callDistSaleMth}' in page optylineedit: Class.OptyLineEditCnt.callDistSaleMth: line 376, column 1
Thanks
Sudhir
This is an abvious error, all fields from your sObject NSP_Margin_Schedule__c like Reseller_Discount__c and others that you're trying to fill should be part of your SOQL query as example : [Select Reseller_Discount__c.... from NSP_Margin_Schedule__c ]
There are 300 rows in visualforce page when i enter reseller discount value in one row and other is blank is this causing the error?
Please adive me how to change the code.
Thanks
Sudhir
[Select AllYourUsedFieldsfromThissObject from YourSObject]
if ( productcat=='C' || productcat=='E'|| productcat=='D'|| productcat=='H'|| productcat=='I'|| productcat=='J' ){
isService = true;
}
when product cat is of any of above type isservice becomes true else by default it is false
NSP_Margin_Schedule__c NMS = [ Select Distributor_Discount__c From NSP_Margin_Schedule__c
where Reseller_Discount__c = :reslrdistdiscount and
Service__c = :isService
and createddate < 2015-01-17T00:00:00-08:00 ];
Also how to handle the errow when query dont return any value it is throwing the error
Thanks
Sudhir
Error: Compile Error: unexpected token: '{' at line which i have highted in bold
Thanks
Sudhir