You need to sign in to do that
Don't have an account?
Tatiana Cooke 9
Apex Error on custom visual force button
Team,
I have created a button that will allow you to add attachments from the notes and attachments section before sending out an email. Code coverage is 95% in sandbox but am getting the below errors in production.
Getting the below erros when running my test class in production:
Can someone please take a look and see what I am doing wrong?
Apex Class:
Test Class:
I have created a button that will allow you to add attachments from the notes and attachments section before sending out an email. Code coverage is 95% in sandbox but am getting the below errors in production.
Getting the below erros when running my test class in production:
System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.OpenEscrowBttn.doEditEmail: line 100, column 1 Class.TestOpenEscrowBttn.TestOpenEscrowBttn: line 58, column 1
Stack Trace: Class.OpenEscrowBttn.doEditEmail: line 100, column 1 Class.TestOpenEscrowBttn.TestOpenEscrowBttn: line 58, column 1
Can someone please take a look and see what I am doing wrong?
Apex 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; } 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.id; system.debug ('\n\n parent '+t.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 + '&p3_lkid=' + targetId +'&p24=' +AdditionalEmailId+'&rtype=003' + '&p2_lkid=003i000003hY6XKAA0' + '&p2_lkid=&template_id='+t.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.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; } } }VF 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>
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 RecordType personAccountRecordType = [SELECT Id FROM RecordType WHERE Name = 'Ward Residential Buyers' and SObjectType = 'Account']; System.runAs ( thisUser ) { WE_Process__c obj=new WE_Process__c(); obj.Name='test'; insert obj; Account acc = new Account(); acc.FirstName='John'; acc.LastName='DoeTest1'; acc.PersonEmail = 'doetest1@test.com'; acc.RecordTypeId = personAccountRecordType.Id; //Changed insert acc; Opportunity optyId = new Opportunity(); optyId.Name = 'optyId'; optyId.AccountId = acc.Id; optyId.Unit_Number__c = spaces.id; optyId.Financing_Type__c = 'Cash Buyer'; optyId.Purchaser_Type__c = 'Person(s)'; optyId.CloseDate = System.today(); optyId.StageName ='S1: Ready to Contract'; optyId.Reason_for_Purchase__c ='Primary Residence'; optyId.Nature_of_Tenancy__c ='Severalty'; optyId.Sale_Type__c ='Founder'; INSERT optyId; ApexPages.currentPage().getParameters().put('optyId', optyid.id); ApexPages.StandardController sc = new ApexPages.StandardController(optyId); OpenEscrowBttn ctrl = new OpenEscrowBttn(sc); ctrl.doEditEmail(); ctrl.doCancel(); Opportunity optyId1 = new Opportunity(Name='optyId', 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 optyId1; Attachment attach=new Attachment(); attach.Name='Software Eval.txt'; Blob bodyBlob=Blob.valueOf('Software Eval'); attach.body=bodyBlob; attach.parentId=optyId.id; insert attach; ApexPages.currentPage().getParameters().put('optyId', optyId.id); ApexPages.StandardController sc1 = new ApexPages.StandardController(optyId); 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(); } } }
Try to always use Collections to perform SOQL queries:
Instead of:
Use:
Also, make sure TempTemplateid is being populated.
Thanks