-4
public void gettemplatebody()
    {
        if(selectedtemplateName!='Select Template')
        {
            Emailtemplate selectedtet=[SELECT id,Name,subject,body,htmlvalue,markup,templatetype FROM Emailtemplate WHERE name=:selectedtemplateName and TemplateType !='Visualforce' LIMIT 1];

            templatesubject=selectedtet.subject;
            if(selectedtet.templatetype=='Custom')
            {
                templatebody=selectedtet.htmlvalue;
            }

            if(selectedtet.templatetype=='Visualforce')
            {
                templatebody=selectedtet.markup;
            }

            if(selectedtet.templatetype=='HTML')
            {
                templatebody=selectedtet.htmlvalue;
            }
            step1=false;
            step2=true;
            templateSelected = true;
            step3=false;
            step4=false;
        }
        else
        {
            step1=false;
            step2=true;
            templateSelected = false;
            step3=false;
            step4=false;  
        }
    }

=================================================================================

@test class

 Emailtemplate et=New Emailtemplate();
            et.name='testtemplate';
            et.body='test body';
            et.subject='test subject';
            et.isActive = true;
            //et.markup='Testmarkup';
            et.FolderId=label.Bulk_Email_folder_id;
            //et.FolderId=UserInfo.getUserId();
            et.DeveloperName='testtemplate';
            et.TemplateType='custom';
            et.HtmlValue = 'test';


        List<case> caseObj1 = [select id,Subject,Responsible_Department__c, ContactId from Case where subject = 'Complaints Case' LIMIT 10];
        ApexPages.StandardsetController sc = new ApexPages.standardsetController(caseObj1);
        sc.setSelected(caseObj1);

        MassEmailandUpdateController ref1=New MassEmailandUpdateController(sc);

        ref1.gettemplatebody();

=============================================================================== error message

executing the method it is giving the

System.QueryException: List has no rows for assignment to SObject,Class.MassEmailandUpdateController.gettemplatebody: line 469, column 1 Class.TestMassEmailandUpdateController.gettemplatebody: line 155, column 1

1
  • Can you edit your question to include relevant code snippet? In its current form, its not in entirety and difficult to understand.
    – Jayant Das
    Oct 30, 2018 at 15:59

3 Answers 3

0

By looking at your code, it seems you are having the issue in this line:

Emailtemplate selectedtet = 
    [SELECT id,Name,subject,body,htmlvalue,markup,templatetype 
     FROM Emailtemplate 
     WHERE name=:selectedtemplateName and TemplateType !='Visualforce' 
     LIMIT 1];

And the reason because if at all there's no matching record found, then you get the exception as you are receiving.

The best approach in such scenarios are to assign the query result to a list instead of the Sobject and then verify the list for presence of a record. You should use something as eblow:

List<Emailtemplate> lst = 
    [SELECT id,Name,subject,body,htmlvalue,markup,templatetype 
     FROM Emailtemplate 
     WHERE name=:selectedtemplateName and TemplateType !='Visualforce' 
     LIMIT 1];
if(lst.size() > 0) {
    selectedtet = lst.get(0);
}
.. rest of my code ..
2
  • Hi Jayant Das thanks for reply , But i have inserted the email template Oct 30, 2018 at 15:57
  • Make sure whatever you have created is available in your test context. As of now it seems it is not, but the resolution provided above is specific to the exception you are facing. If you have other issues, the way it works on the forum is to keep the original question specific to one problem and that any other issue is qualified as a different question.
    – Jayant Das
    Oct 30, 2018 at 16:26
0

In your test class I cannot see if you are setting the value of the variable selectedtemplateName

You have to set it so that it is passed in the controller and then can get results in querry. Also you can insert the EmailTemplate in test class as the records are available in test class context.

@test class

Emailtemplate et=New Emailtemplate();
et.name='testtemplate';
et.body='test body';
et.subject='test subject';
et.isActive = true;
et.FolderId=label.Bulk_Email_folder_id;

et.DeveloperName='testtemplate';
et.TemplateType='custom';
et.HtmlValue = 'test';

insert test;


List<case> caseObj1 = [select id,Subject,Responsible_Department__c, ContactId from Case where subject = 'Complaints Case' LIMIT 10];
ApexPages.StandardsetController sc = new ApexPages.standardsetController(caseObj1);
sc.setSelected(caseObj1);

MassEmailandUpdateController ref1=New MassEmailandUpdateController(sc);
ref1.selectedtemplateName = et.name;

ref1.gettemplatebody();
-3

You dont have an an email template record, Create a email template in your test.

I see you created one EmailTemplate (variable et) but have not inserted it

 Emailtemplate et=New Emailtemplate();
            et.name='testtemplate';
            et.body='test body';
            et.subject='test subject';
            et.isActive = true;
            //et.markup='Testmarkup';
            et.FolderId=label.Bulk_Email_folder_id;
            //et.FolderId=UserInfo.getUserId();
            et.DeveloperName='testtemplate';
            et.TemplateType='custom';
            et.HtmlValue = 'test';

 insert et;

How can Email Template be created in unit tests?

Unit tests do not have access to the data within the org unless you add add the @isTest(SeeAllData=true) annotation which is frowned upon.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_seealldata_using.htm

2
  • seeAllData=true is very bad practice. The link you provided suggests it should not be necessary here.
    – David Reed
    Oct 30, 2018 at 15:57
  • Agree seeAllData=true is not the better option, thats why I include 'a better yet option'. By adding this annotation and link I hope to help new apex programmers understand how Unit Tests do not refect the current state of the database by default. Oct 30, 2018 at 17:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .