You need to sign in to do that
Don't have an account?
![Rahul Rahul](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Hello friends, I have deployed this code in Production, Iam getting this Error while saving a contract record "SBQQ.ContractAfter: System.LimitException: Too many SOQL queries: 101"
public class QuoteHandler_PayTerms_GenerateDoc {
Public void Documenttemplate(list<SBQQ__Quote__c> newlist){
list<SBQQ__QuoteTemplate__c> qt1 = [select id,Name,SBQQ__Default__c from SBQQ__QuoteTemplate__c WHERE name =:'Auto Renew' ];
list<SBQQ__QuoteTemplate__c> qt2 = [select id,Name,SBQQ__Default__c from SBQQ__QuoteTemplate__c WHERE name =:'Auto Renew with Auto pay' ];
list<SBQQ__QuoteTemplate__c> qt3 = [select id,Name,SBQQ__Default__c from SBQQ__QuoteTemplate__c WHERE name =:'Basic Template'];
list<SBQQ__QuoteTemplate__c> qt4 = [select id,Name,SBQQ__Default__c from SBQQ__QuoteTemplate__c WHERE name =:'Basic Template with Auto Pay'];
list<SBQQ__QuoteTemplate__c> qt5 = [select id,Name,SBQQ__Default__c from SBQQ__QuoteTemplate__c WHERE name =:'Auto Renew with 3% Annual Increment with Autopay'];
list<SBQQ__QuoteTemplate__c> qt6 = [select id,Name,SBQQ__Default__c from SBQQ__QuoteTemplate__c WHERE name =:'Auto Renew with 3% Annual Increment'];
list<SBQQ__QuoteTemplate__c> list1 = new list<SBQQ__QuoteTemplate__c>();
for(SBQQ__Quote__c ss : newlist)
{
if(ss.Document_Template__c=='Auto Renew' && ( ss.SBQQ__PaymentTerms__c=='Monthly' || ss.SBQQ__PaymentTerms__c=='Quarterly') )
{
if(Test.isRunningTest()) {
qt2.add(new SBQQ__QuoteTemplate__c());
qt1.add(new SBQQ__QuoteTemplate__c());
qt3.add(new SBQQ__QuoteTemplate__c());
qt4.add(new SBQQ__QuoteTemplate__c());
qt5.add(new SBQQ__QuoteTemplate__c());
qt6.add(new SBQQ__QuoteTemplate__c());
}
if(qt2.size() >0) {
qt2[0].SBQQ__Default__c = true;
list1.add(qt2[0]);
}
if(qt1.size() >0) {
qt1[0].SBQQ__Default__c = false;
list1.add(qt1[0]);
}
if(qt3.size() >0) {
qt3[0].SBQQ__Default__c = false;
list1.add(qt3[0]);
}
if(qt4.size() >0) {
qt4[0].SBQQ__Default__c = false;
list1.add(qt4[0]);
}
if(qt5.size() >0) {
qt5[0].SBQQ__Default__c = false;
list1.add(qt5[0]);
}
if(qt6.size() >0) {
qt6[0].SBQQ__Default__c = false;
list1.add(qt6[0]);
}
}
if(ss.Document_Template__c=='Auto Renew' && ( ss.SBQQ__PaymentTerms__c=='Annual' || ss.SBQQ__PaymentTerms__c=='Semi-annual' || ss.SBQQ__PaymentTerms__c=='Upfront') )
{
if(Test.isRunningTest()) {
qt2.add(new SBQQ__QuoteTemplate__c());
qt1.add(new SBQQ__QuoteTemplate__c());
qt3.add(new SBQQ__QuoteTemplate__c());
qt4.add(new SBQQ__QuoteTemplate__c());
qt5.add(new SBQQ__QuoteTemplate__c());
qt6.add(new SBQQ__QuoteTemplate__c());
}
if(qt2.size() >0) {
qt2[0].SBQQ__Default__c = False;
list1.add(qt2[0]);
}
if(qt1.size() >0) {
qt1[0].SBQQ__Default__c = True;
list1.add(qt1[0]);
}
if(qt3.size() >0) {
qt3[0].SBQQ__Default__c = false;
list1.add(qt3[0]);
}
if(qt4.size() >0) {
qt4[0].SBQQ__Default__c = false;
list1.add(qt4[0]);
}
if(qt5.size() >0) {
qt5[0].SBQQ__Default__c = false;
list1.add(qt5[0]);
}
if(qt6.size() >0) {
qt6[0].SBQQ__Default__c = false;
list1.add(qt6[0]);
}
}
if(ss.Document_Template__c=='Auto Renew 3' && ( ss.SBQQ__PaymentTerms__c=='Quarterly' || ss.SBQQ__PaymentTerms__c=='Monthly') )
{
if(Test.isRunningTest()) {
qt2.add(new SBQQ__QuoteTemplate__c());
qt1.add(new SBQQ__QuoteTemplate__c());
qt3.add(new SBQQ__QuoteTemplate__c());
qt4.add(new SBQQ__QuoteTemplate__c());
qt5.add(new SBQQ__QuoteTemplate__c());
qt6.add(new SBQQ__QuoteTemplate__c());
}
if(qt2.size() >0) {
qt2[0].SBQQ__Default__c = false;
list1.add(qt2[0]);
}
if(qt1.size() >0) {
qt1[0].SBQQ__Default__c = false;
list1.add(qt1[0]);
}
if(qt3.size() >0) {
qt3[0].SBQQ__Default__c = false;
list1.add(qt3[0]);
}
if(qt4.size() >0) {
qt4[0].SBQQ__Default__c = false;
list1.add(qt4[0]);
}
if(qt5.size() >0) {
qt5[0].SBQQ__Default__c = True;
list1.add(qt5[0]);
}
if(qt6.size() >0) {
qt6[0].SBQQ__Default__c = false;
list1.add(qt6[0]);
}
}
if(ss.Document_Template__c=='Auto Renew 3' && ( ss.SBQQ__PaymentTerms__c=='Annual' || ss.SBQQ__PaymentTerms__c=='Semi-annual' || ss.SBQQ__PaymentTerms__c=='Upfront') )
{
if(Test.isRunningTest()) {
qt2.add(new SBQQ__QuoteTemplate__c());
qt1.add(new SBQQ__QuoteTemplate__c());
qt3.add(new SBQQ__QuoteTemplate__c());
qt4.add(new SBQQ__QuoteTemplate__c());
qt5.add(new SBQQ__QuoteTemplate__c());
qt6.add(new SBQQ__QuoteTemplate__c());
}
if(qt2.size() >0) {
qt2[0].SBQQ__Default__c = false;
list1.add(qt2[0]);
}
if(qt1.size() >0) {
qt1[0].SBQQ__Default__c = false;
list1.add(qt1[0]);
}
if(qt3.size() >0) {
qt3[0].SBQQ__Default__c = false;
list1.add(qt3[0]);
}
if(qt4.size() >0) {
qt4[0].SBQQ__Default__c = false;
list1.add(qt4[0]);
}
if(qt5.size() >0) {
qt5[0].SBQQ__Default__c = false;
list1.add(qt5[0]);
}
if(qt6.size() >0) {
qt6[0].SBQQ__Default__c = True;
list1.add(qt6[0]);
}
}
try {
update list1;
}
catch(exception e) {
system.debug('error:'+e);
}
}
}
}
Public void Documenttemplate(list<SBQQ__Quote__c> newlist){
list<SBQQ__QuoteTemplate__c> qt1 = [select id,Name,SBQQ__Default__c from SBQQ__QuoteTemplate__c WHERE name =:'Auto Renew' ];
list<SBQQ__QuoteTemplate__c> qt2 = [select id,Name,SBQQ__Default__c from SBQQ__QuoteTemplate__c WHERE name =:'Auto Renew with Auto pay' ];
list<SBQQ__QuoteTemplate__c> qt3 = [select id,Name,SBQQ__Default__c from SBQQ__QuoteTemplate__c WHERE name =:'Basic Template'];
list<SBQQ__QuoteTemplate__c> qt4 = [select id,Name,SBQQ__Default__c from SBQQ__QuoteTemplate__c WHERE name =:'Basic Template with Auto Pay'];
list<SBQQ__QuoteTemplate__c> qt5 = [select id,Name,SBQQ__Default__c from SBQQ__QuoteTemplate__c WHERE name =:'Auto Renew with 3% Annual Increment with Autopay'];
list<SBQQ__QuoteTemplate__c> qt6 = [select id,Name,SBQQ__Default__c from SBQQ__QuoteTemplate__c WHERE name =:'Auto Renew with 3% Annual Increment'];
list<SBQQ__QuoteTemplate__c> list1 = new list<SBQQ__QuoteTemplate__c>();
for(SBQQ__Quote__c ss : newlist)
{
if(ss.Document_Template__c=='Auto Renew' && ( ss.SBQQ__PaymentTerms__c=='Monthly' || ss.SBQQ__PaymentTerms__c=='Quarterly') )
{
if(Test.isRunningTest()) {
qt2.add(new SBQQ__QuoteTemplate__c());
qt1.add(new SBQQ__QuoteTemplate__c());
qt3.add(new SBQQ__QuoteTemplate__c());
qt4.add(new SBQQ__QuoteTemplate__c());
qt5.add(new SBQQ__QuoteTemplate__c());
qt6.add(new SBQQ__QuoteTemplate__c());
}
if(qt2.size() >0) {
qt2[0].SBQQ__Default__c = true;
list1.add(qt2[0]);
}
if(qt1.size() >0) {
qt1[0].SBQQ__Default__c = false;
list1.add(qt1[0]);
}
if(qt3.size() >0) {
qt3[0].SBQQ__Default__c = false;
list1.add(qt3[0]);
}
if(qt4.size() >0) {
qt4[0].SBQQ__Default__c = false;
list1.add(qt4[0]);
}
if(qt5.size() >0) {
qt5[0].SBQQ__Default__c = false;
list1.add(qt5[0]);
}
if(qt6.size() >0) {
qt6[0].SBQQ__Default__c = false;
list1.add(qt6[0]);
}
}
if(ss.Document_Template__c=='Auto Renew' && ( ss.SBQQ__PaymentTerms__c=='Annual' || ss.SBQQ__PaymentTerms__c=='Semi-annual' || ss.SBQQ__PaymentTerms__c=='Upfront') )
{
if(Test.isRunningTest()) {
qt2.add(new SBQQ__QuoteTemplate__c());
qt1.add(new SBQQ__QuoteTemplate__c());
qt3.add(new SBQQ__QuoteTemplate__c());
qt4.add(new SBQQ__QuoteTemplate__c());
qt5.add(new SBQQ__QuoteTemplate__c());
qt6.add(new SBQQ__QuoteTemplate__c());
}
if(qt2.size() >0) {
qt2[0].SBQQ__Default__c = False;
list1.add(qt2[0]);
}
if(qt1.size() >0) {
qt1[0].SBQQ__Default__c = True;
list1.add(qt1[0]);
}
if(qt3.size() >0) {
qt3[0].SBQQ__Default__c = false;
list1.add(qt3[0]);
}
if(qt4.size() >0) {
qt4[0].SBQQ__Default__c = false;
list1.add(qt4[0]);
}
if(qt5.size() >0) {
qt5[0].SBQQ__Default__c = false;
list1.add(qt5[0]);
}
if(qt6.size() >0) {
qt6[0].SBQQ__Default__c = false;
list1.add(qt6[0]);
}
}
if(ss.Document_Template__c=='Auto Renew 3' && ( ss.SBQQ__PaymentTerms__c=='Quarterly' || ss.SBQQ__PaymentTerms__c=='Monthly') )
{
if(Test.isRunningTest()) {
qt2.add(new SBQQ__QuoteTemplate__c());
qt1.add(new SBQQ__QuoteTemplate__c());
qt3.add(new SBQQ__QuoteTemplate__c());
qt4.add(new SBQQ__QuoteTemplate__c());
qt5.add(new SBQQ__QuoteTemplate__c());
qt6.add(new SBQQ__QuoteTemplate__c());
}
if(qt2.size() >0) {
qt2[0].SBQQ__Default__c = false;
list1.add(qt2[0]);
}
if(qt1.size() >0) {
qt1[0].SBQQ__Default__c = false;
list1.add(qt1[0]);
}
if(qt3.size() >0) {
qt3[0].SBQQ__Default__c = false;
list1.add(qt3[0]);
}
if(qt4.size() >0) {
qt4[0].SBQQ__Default__c = false;
list1.add(qt4[0]);
}
if(qt5.size() >0) {
qt5[0].SBQQ__Default__c = True;
list1.add(qt5[0]);
}
if(qt6.size() >0) {
qt6[0].SBQQ__Default__c = false;
list1.add(qt6[0]);
}
}
if(ss.Document_Template__c=='Auto Renew 3' && ( ss.SBQQ__PaymentTerms__c=='Annual' || ss.SBQQ__PaymentTerms__c=='Semi-annual' || ss.SBQQ__PaymentTerms__c=='Upfront') )
{
if(Test.isRunningTest()) {
qt2.add(new SBQQ__QuoteTemplate__c());
qt1.add(new SBQQ__QuoteTemplate__c());
qt3.add(new SBQQ__QuoteTemplate__c());
qt4.add(new SBQQ__QuoteTemplate__c());
qt5.add(new SBQQ__QuoteTemplate__c());
qt6.add(new SBQQ__QuoteTemplate__c());
}
if(qt2.size() >0) {
qt2[0].SBQQ__Default__c = false;
list1.add(qt2[0]);
}
if(qt1.size() >0) {
qt1[0].SBQQ__Default__c = false;
list1.add(qt1[0]);
}
if(qt3.size() >0) {
qt3[0].SBQQ__Default__c = false;
list1.add(qt3[0]);
}
if(qt4.size() >0) {
qt4[0].SBQQ__Default__c = false;
list1.add(qt4[0]);
}
if(qt5.size() >0) {
qt5[0].SBQQ__Default__c = false;
list1.add(qt5[0]);
}
if(qt6.size() >0) {
qt6[0].SBQQ__Default__c = True;
list1.add(qt6[0]);
}
}
try {
update list1;
}
catch(exception e) {
system.debug('error:'+e);
}
}
}
}
First make a set of all name of QuoteTemplate__c that you are using in all the queries.
Let nameSet is the set of string and contains value as:
'Auto Renew', 'Auto Renew with Auto pay', 'Basic Template', 'Basic Template with Auto Pay', 'Auto Renew with 3% Annual Increment'.
Then use this query for fetch record:
[SELECT Id, Name, SBQQ__Default__c FROM SBQQ__QuoteTemplate__c WHERE Name IN : nameSet LIMIT 10000];
After doing this separate the record according to your requirement by iterating list of queried record and check the name of particular record.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
public class QuoteHandler_PayTerms_GenerateDoc {
Public void Documenttemplate(list<SBQQ__Quote__c> newlist) {
list<SBQQ__QuoteTemplate__c> qt = [select id,Name,SBQQ__Default__c from SBQQ__QuoteTemplate__c];
Map<String, SBQQ__QuoteTemplate__c> quoteMap1 = new Map<String, SBQQ__QuoteTemplate__c>();
for(SBQQ__QuoteTemplate__c q :qt) {
quoteMap1.put(q.Name, q);
}
system.debug('quoteMap1::'+quoteMap1);
SBQQ__QuoteTemplate__c qt1 = quoteMap1.get('Auto Renew');
SBQQ__QuoteTemplate__c qt2 = quoteMap1.get('Auto Renew with Auto pay');
SBQQ__QuoteTemplate__c qt3 = quoteMap1.get('Basic Template');
SBQQ__QuoteTemplate__c qt4 = quoteMap1.get('Basic Template with Auto Pay');
SBQQ__QuoteTemplate__c qt5 = quoteMap1.get('Auto Renew with 3% Annual Increment with Autopay');
SBQQ__QuoteTemplate__c qt6 = quoteMap1.get('Auto Renew with 3% Annual Increment');
Map<string, SBQQ__QuoteTemplate__c> quoteMap = new Map<String, SBQQ__QuoteTemplate__c>();
for(SBQQ__Quote__c ss : newlist) {
if(qt1 != null) {
qt1.SBQQ__Default__c = false;
quoteMap.put('qt1',qt1);
}
if(qt2 != null) {
qt2.SBQQ__Default__c = false;
quoteMap.put('qt2',qt2);
}
if(qt3 != null) {
qt3.SBQQ__Default__c = false;
quoteMap.put('qt3',qt3);
}
if(qt4 != null){
qt4.SBQQ__Default__c = false;
quoteMap.put('qt4',qt4);
}
if(qt5 != null){
qt5.SBQQ__Default__c = false;
quoteMap.put('qt5',qt5);
}
if(qt6 != null) {
qt6.SBQQ__Default__c = false;
quoteMap.put('qt6',qt6);
}
if(ss.Document_Template__c=='Auto Renew' && ( ss.SBQQ__PaymentTerms__c=='Monthly' || ss.SBQQ__PaymentTerms__c=='Quarterly')) {
if(qt2 != null) qt2.SBQQ__Default__c = true;
quoteMap.put('qt2',qt2);
}
if(ss.Document_Template__c=='Auto Renew' && ( ss.SBQQ__PaymentTerms__c=='Annual' || ss.SBQQ__PaymentTerms__c=='Semi-annual' || ss.SBQQ__PaymentTerms__c=='Upfront')) {
system.debug('qt1:::'+qt1);
if(qt1 != null) qt1.SBQQ__Default__c = true;
quoteMap.put('qt1',qt1);
}
if(ss.Document_Template__c=='Auto Renew 3' && ( ss.SBQQ__PaymentTerms__c=='Quarterly' || ss.SBQQ__PaymentTerms__c=='Monthly')){
if(qt5 != null) qt5.SBQQ__Default__c = true;
quoteMap.put('qt5',qt5);
}
if(ss.Document_Template__c=='Auto Renew 3' && ( ss.SBQQ__PaymentTerms__c=='Annual' || ss.SBQQ__PaymentTerms__c=='Semi-annual' || ss.SBQQ__PaymentTerms__c=='Upfront')) {
if(qt6 != null) qt6.SBQQ__Default__c = true;
quoteMap.put('qt6',qt6);
}
if(ss.Document_Template__c=='Basic Template' && ( ss.SBQQ__PaymentTerms__c=='Quarterly' || ss.SBQQ__PaymentTerms__c=='Monthly')) {
if(qt4 != null) qt4.SBQQ__Default__c = true;
quoteMap.put('qt4',qt4);
}
if(ss.Document_Template__c=='Basic Template' && ( ss.SBQQ__PaymentTerms__c=='Annual' || ss.SBQQ__PaymentTerms__c=='Semi-annual' || ss.SBQQ__PaymentTerms__c=='Upfront')) {
if(qt3 != null) qt3.SBQQ__Default__c = true;
quoteMap.put('qt3',qt3);
}
}
try{
update quoteMap.values();
}
catch(exception e) {
system.debug('error:'+e);
}
}
}