You need to sign in to do that
Don't have an account?
Tatiana Cooke 9
Problem with code, need help ASAP
Team,
New to development. Please please help, need this working as soon as possible. Can somone look at the error and code below.
I created a button on the opportunity which can attach a file from the notes and attachment and send an email with a template.
Getting below error when trying to deploy to production.
System.LimitException: Too many SOQL queries: 101
Stack Trace: Class.OpenEscrowBttn.<init>: line 18, column 1 Class.TestOpenEscrowBttn.TestOpenEscrowBttn: line 57, column 1
Test Class
Class
New to development. Please please help, need this working as soon as possible. Can somone look at the error and code below.
I created a button on the opportunity which can attach a file from the notes and attachment and send an email with a template.
Getting below error when trying to deploy to production.
System.LimitException: Too many SOQL queries: 101
Stack Trace: Class.OpenEscrowBttn.<init>: line 18, column 1 Class.TestOpenEscrowBttn.TestOpenEscrowBttn: line 57, column 1
Test Class
@isTest public with sharing class TestOpenEscrowBttn { public static testMethod void TestOpenEscrowBttn(){ Schema.DescribeSObjectResult acctSchema = Schema.SObjectType.Account; Map<String,Schema.RecordTypeInfo> AccountRecordTypeInfo = acctSchema.getRecordTypeInfosByName(); Id acctRTId = AccountRecordTypeInfo.get('Ward Residential Buyers').getRecordTypeId(); Schema.DescribeSObjectResult propSchema = Schema.SObjectType.Property__c; Map<String,Schema.RecordTypeInfo> PropertyRecordTypeInfo = propSchema.getRecordTypeInfosByName(); Id propRTId = PropertyRecordTypeInfo.get('Ward Residential').getRecordTypeId(); Schema.DescribeSObjectResult spaceSchema = Schema.SObjectType.Space__c; Map<String,Schema.RecordTypeInfo> SpaceRecordTypeInfo = spaceSchema.getRecordTypeInfosByName(); Id spaceRTId = SpaceRecordTypeInfo.get('Ward Residential').getRecordTypeId(); Property__c prop = new Property__c(recordtypeid=propRTId, Name='Test Property'); insert prop; Space__c spaces = new Space__c(recordtypeid=spaceRTId, Name='Test Ward Residential', property__c = prop.id); insert spaces; Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; User thisUser = new User(Alias = 'tCook', Email='tatianaCooke@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='tCooke91@testorg.com'); //Changed System.runAs ( thisUser ) { WE_Process__c obj=new WE_Process__c(); obj.Name='test'; insert obj; Account acc = new Account(recordtypeid=acctRTId, FirstName='First Name', LastName='Last Name', Phone='111-111-1111', PersonEmail='first.last@test.com', type='Prospect'); insert acc; Opportunity opty = new Opportunity(Name='Test Opty', AccountId=acc.Id, Unit_Number__c=spaces.Id, Financing_Type__c='Cash Buyer', Purchaser_Type__c='Person(s)', Reason_for_Purchase__c='Primary Residence', Nature_of_Tenancy__c='Joint Tenants', Sale_Type__c='Direct', CloseDate=date.today()+10, StageName='S1: Ready to Contract'); insert opty; ApexPages.currentPage().getParameters().put('optyId', opty.id); ApexPages.StandardController sc = new ApexPages.StandardController(opty); OpenEscrowBttn ctrl = new OpenEscrowBttn(sc); ctrl.doEditEmail(); ctrl.doCancel(); Opportunity opty1 = new Opportunity(Name='Test Opty 1', AccountId=acc.Id, Unit_Number__c=spaces.Id, Financing_Type__c='Cash Buyer', Purchaser_Type__c='Person(s)', Reason_for_Purchase__c='Primary Residence', Nature_of_Tenancy__c='Joint Tenants', Sale_Type__c='Direct', CloseDate=date.today()+10, StageName='S1: Ready to Contract'); insert opty1; Attachment attach=new Attachment(); attach.Name='Software Eval.txt'; Blob bodyBlob=Blob.valueOf('Software Eval'); attach.body=bodyBlob; attach.parentId=opty1.id; insert attach; ApexPages.currentPage().getParameters().put('optyId', opty1.id); ApexPages.StandardController sc1 = new ApexPages.StandardController(opty1); OpenEscrowBttn ctrl1 = new OpenEscrowBttn(sc1); System.debug('***attSize: '+ctrl1.attSize); System.debug('***optyDocsSize: '+ctrl1.opptyDocs.size()); ctrl1.OpptyDocs[0].lCheck = true; ctrl1.doEditEmail(); ApexPages.currentPage().getParameters().put('optyId', null); ApexPages.currentPage().getParameters().put('acctId', null); ApexPages.StandardController sc2 = new ApexPages.StandardController(acc); OpenEscrowBttn ctrl2 = new OpenEscrowBttn(sc2); //ctrl2.doEditEmail(); ctrl2.doCancel(); ApexPages.currentPage().getParameters().put('optyId', null); ApexPages.currentPage().getParameters().put('acctId', acc.Id); ApexPages.StandardController sc3 = new ApexPages.StandardController(acc); OpenEscrowBttn ctrl3 = new OpenEscrowBttn(sc3); ctrl3.doEditEmail(); } } }
Class
public with sharing class OpenEscrowBttn { public Id targetId {get;set;} public String AdditionalEmailId { get;set;} public String[] selDocs = new String[]{}; //public Transient List<Attachment> optyDocs {get;set;} Id TempFolderId; Id TempTemplateId; String TemplateName; public Transient Attachment[] oldAttachments; public Integer attSize {get;set;} public List<DocItem> OpptyDocs {get;set;} public OpenEscrowBttn(ApexPages.StandardController controller) { Id optyId = ApexPages.currentPage().getParameters().get('optyId'); Id acctId = ApexPages.currentPage().getParameters().get('acctId'); if (optyId != Null) { targetId = optyId; List<Opportunity> oppObj = [SELECT Id, AccountId, Name FROM Opportunity where Id = :optyId ]; System.debug('******oppObj******'+oppObj); if(oppObj.size() > 0) { List<Account> accObj =[SELECT Id, AnnualRevenue, Appointment_Changes__c, AdditionalEmails__c FROM Account where Id = :oppObj[0].AccountId]; System.debug('******accObj ******'+accObj ); if(accObj.size()> 0) { if(accObj[0].AdditionalEmails__c != null) { AdditionalEmailId = accObj[0].AdditionalEmails__c; } } } } else if (acctId != Null){ targetId = acctId; List<Account> accObj =[SELECT Id, AnnualRevenue, Appointment_Changes__c, AdditionalEmails__c FROM Account where Id = :acctId ]; System.debug('******accObj ******'+accObj ); if(accObj.size()> 0) { if(accObj[0].AdditionalEmails__c != null) { AdditionalEmailId = accObj[0].AdditionalEmails__c; } } } else { targetId = Null; } system.debug('\n\n targetId = '+targetId); attSize = 0; //optyDocs = new List<Attachment>(); set<Id> RelatedContracts = new set<Id>(); if ( acctId != Null) { for (Contract ct : [select id from Contract where accountId =:acctId]) { RelatedContracts.add(ct.id); } } if (targetId != null) { OpptyDocs = new List<DocItem>(); for (Attachment at : [Select id, Name, Body, ParentId from Attachment where (ParentId = :targetId OR ParentId in :RelatedContracts)]) { //optyDocs.add(at); OpptyDocs.add(new DocItem(at.Id,false,at.Name)); attSize++; } //attSize = optyDocs.size(); if (attSize <= 0) { //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'No attachments found!')); ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'No attachments found for this opportunity!')); } } } public PageReference doEditEmail(){ Folder TFolderId = [Select id from Folder where DeveloperName = 'Ward_Residential_Workflow_Templates' limit 1]; TempFolderId = TFolderId.Id; TemplateName = 'Ke Kilohana Open Escrow Email'; EmailTemplate[] Temp = [Select id from EmailTemplate where Name = :TemplateName and FolderId = :TempFolderId limit 1]; if ( Temp.size() > 0 ) { TempTemplateId = Temp[0].id; /* } else { EmailTemplate[] Temp1 = [Select id from EmailTemplate where FolderId = :TempFolderId limit 1]; if ( Temp1.size() > 0 ) { TempTemplateId = Temp1[0].id; } */ } //Delete All current Attachments oldAttachments = [select id from Attachment where ParentId = :TempTemplateId ]; if ( oldAttachments.size() > 0 ) { delete oldAttachments; } List<EmailTemplate> t = [Select id, FolderId, TemplateType, HtmlValue, subject, body from EmailTemplate where Id = :TempTemplateid]; List<Attachment> nTlist =new list<Attachment>(); //Id attId = selDocs[0]; set<id> aList= new set<id>(); for (DocItem d: OpptyDocs) { system.debug('lCheck:'+d.lcheck); if ( d.lcheck ) { aList.add(d.DocId); //numDoc++; } } if (aList.size() == 0) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'No attachments selected!')); return null; } else { Attachment tnew; Attachment[] tList = [Select Name, Body, ParentId from Attachment where id IN :aList]; for (Attachment o: tList) { tnew = new Attachment(); //tnew.Name = '(From Attachments)-'+o.name; tnew.Name = o.name; tnew.Body = o.Body; tnew.ParentId = t[0].id ; system.debug ('\n\n parent '+t[0].id ); nTlist.add(tnew); } insert nTlist; /* set<id> sTlist = new set<id>(); for (Attachment n: nTlist) { sTlist.add(n.id); } */ //PageReference pg = new PageReference('/_ui/core/email/author/EmailAuthor?id=' + attId + '&retURL=%2F' + targetId + '&p3_lkid=' + targetId + '&p2_lkid=&template_id='+t.id+'&new_template=1'); //PageReference pg = new PageReference('/_ui/core/email/author/EmailAuthor?retURL=%2F' + targetId + '&p3_lkid=' + targetId + '&rtype=003' + '&p2_lkid=&template_id='+t.id+'&new_template=1'); PageReference pg; if(AdditionalEmailId != null) { pg = new PageReference('/_ui/core/email/author/EmailAuthor?retURL=%2F' + targetId + '&p2_lkid=003i000003hY6XKAA0' + '&p3_lkid=' + targetId +'&p24=' +AdditionalEmailId+'&rtype=003' + '&p2_lkid=&template_id='+t[0].id+ '&p4=dmoreira@tghawaii.com,jnelson@tghawaii.com,abastatas@tghawaii.com' +'&new_template=1'); } else { pg = new PageReference('/_ui/core/email/author/EmailAuthor?retURL=%2F' + targetId + '&p3_lkid=' + targetId + '&rtype=003' + '&p2_lkid=&template_id='+t[0].id+'&new_template=1'); } pg.setRedirect(true); return pg; } } public PageReference doCancel(){ PageReference pg = new PageReference('/'+targetId); pg.setRedirect(true); return pg; } public class DocItem { // Referral Request Wrapper public id DocId {get; set;} public boolean lcheck {get; set;} public string DocName {get; set;} public DocItem (id i, boolean c, string s) { DocId = i; lcheck = c; DocName =s; } } }Visualforce Page
<apex:page id="thePage" standardController="Opportunity" extensions="OpenEscrowBttn" tabStyle="Opportunity"> <apex:messages style="color:red; font-weight:bold; text-align:center;" /> <apex:form id="theForm"> <apex:pageBlock id="PB" title="Send Email"> <apex:pageBlockButtons > <apex:commandButton value="Edit Email and Send" action="{!doEditEmail}" rendered="{!IF(attSize>0, true, false)}"/> <apex:commandButton value="Cancel" action="{!doCancel}" immediate="true"/> </apex:pageBlockButtons> <apex:pageblockSection id="PBS1" title="Select Attachments" columns="1"> <apex:pageBlockTable value="{!OpptyDocs}" var="s" > <apex:column width="10%"> <apex:facet name="header">Select</apex:facet> <apex:inputcheckbox value="{!s.lcheck}"/> </apex:column> <apex:column width="30%"> <apex:facet name="header">Attachments</apex:facet> {!s.DocName} </apex:column> </apex:pageBlockTable> </apex:pageblockSection> </apex:pageBlock> </apex:form> </apex:page>
The above is a provision in salesforce to separate your test data preparation and test data execution boundaries.
Hope this helps.
I tried doing the above and my test class is up to 73% in production. So almost there.
I now have this below error:
System.ListException: List index out of bounds: 0
Stack Trace: Class.OpenEscrowBttn.doEditEmail: line 128, column 1 Class.TestOpenEscrowBttn.TestOpenEscrowBttn: line 67, column 1
Below is the code that I used:
Regards,
TC
On the surface it appears that your test class has not created data in the formate which the function doEditEmail expects.