0

I am trying to change a StandardConroller to StandardSetController in order to use list button with visualforce. I have updated to setSelected before b.sendemail(), however I now get "Variable setcon is used before it is declared".

The button will be used to send an email to each of the selected records in a list. I have a Visualforce page that will call the apex, but I need to make it accessible to "Choose" in the Visualforce listbutton option.

The class worked before for a custom non list button, but now I am trying to modify the code to apply for a custom list view visual force button.

Any help is much appreciated. Thanks in advance.

Apex Class:

public class CustomButtonSendEmailListView {

    @testVisible private ApexPages.StandardSetController setCon;

    public CustomButtonSendEmailListView(ApexPages.StandardSetController controller) {
        setCon = setCon;
    }

    public pageReference sendemail() {
        List<SalesstatementInvoice__c> sslist = (List<SalesstatementInvoice__c>) setCon.getSelected();
        SalesStatementInvoice__c salesStat = [Select Id, Name, Faktura__c, Contact__r.LastName, Contact__r.FirstName, Contact__r.Name, Afregningsdato__c, Show__r.Name, Account__c, Invoice_Number_E_conomic__c, Contact__c, Contact__r.Email, Valuta__c FROM SalesStatementInvoice__c where id =:sslist];
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        EmailTemplate SendPdfDK = [Select id, Body from EmailTemplate where Name = 'SalesInvoiceDK'];
        EmailTemplate SendPdfENG = [Select id, Body from EmailTemplate where Name = 'SalesInvoiceENG'];
        message.setTargetObjectId(salesStat.Contact__c);
        message.setwhatId(salesStat.Id);
        message.setSubject('FAKTURA ' + salesStat.Faktura__c + ' - ' + salesStat.Show__r.Name + ' - ' +
                salesStat.Afregningsdato__c.format());
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        pageReference pdf;
        Currency__c curr = Currency__c.getInstance(salesStat.Valuta__c);
        if (curr != null && curr.Invoice_Language__c == 'Danish') {
            pdf = Page.SalesInvoicePDF;
            message.setplainTextBody(reOpenCaseTempBody(SendPdfDK.Body, salesStat));

        }
        else {
            pdf = Page.SalesInvoicePDFEng;
            message.setplainTextBody(reOpenCaseTempBody(SendPdfENG.Body, salesStat));
        }
        pdf.getParameters().put('id', salesStat.Id);
        pdf.getParameters().put('invNum', salesStat.Invoice_Number_E_conomic__c);
        Attachment attach = new Attachment();
        Blob body;
        if (Test.IsRunningTest())
        {
            body = Blob.valueOf('UNIT.TEST');
        }
        else
        {
            body = pdf.getContent();
        }
        attach.Body = body;
        attach.Name = salesStat.Name + '.pdf';
        attach.IsPrivate = false;
        attach.ParentId = salesStat.Id;
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName('FAKTURA ' + salesStat.Faktura__c + ' - ' + salesStat.Show__r.Name + ' - ' +
                salesStat.Afregningsdato__c.format() + '.pdf');
        efa.setBody(body);
        fileAttachments.add(efa);
        message.setFileAttachments(fileAttachments);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{message});
        //insert attach;
        return new PageReference('/' + salesStat.Id);
    }

    public string reOpenCaseTempBody(string emailBody, SalesStatementInvoice__c s) {
        emailBody = emailBody.replaceAll('\\{!SalesStatementInvoice__c\\.Contact__r\\.FirstName\\}',
                String.valueOf(s.Contact__r.FirstName));

        return emailBody;
    }
}

Test Class:

@isTest
global class CustomButtonSendEmailListViewTest
{

    @isTest static void SendEmailTestMethod() {

        Account acc = new Account();
        acc.Name = 'Test Name';
        Insert acc;

        Contact con = new Contact();
        con.LastName = 'ContTest1';
        con.FirstName = 'ConFirstName';
        con.Email = '[email protected]';
        con.AccountId = acc.Id;
        Insert con;

        SalesStatementInvoice__c objSales = new SalesStatementInvoice__c ();
        objSales.Faktura__c = '513';
        objSales.Account__c = acc.Id ;
        objSales.Contact__c = con.Id ;
        objSales.Invoice_Number_E_conomic__c = '733';
        objSales.Valuta__c = 'DKK';
        insert objSales;

        System.currentPagereference().getParameters().put('id', objSales.id);

        Attachment objAtt = new Attachment();
        objAtt.Name = 'Test';
        objAtt.body = Blob.valueof('string');
        objAtt.ParentId = objSales.Id;
        insert objAtt;

        List<SalesstatementInvoice__c> sslist = [SELECT Id FROM SalesstatementInvoice__c LIMIT 1000];
        ApexPages.StandardSetController setCon = new ApexPages.StandardSetController(sslist);
        CustomButtonSendEmailListView b = new CustomButtonSendEmailListView (setCon);
        b.setCon.setSelected(sslist);
        b.sendemail();

    }
}

2 Answers 2

2

You are passing in ApexPages.StandardController but your constructor only accepts ApexPages.StandardSetController. The documentation is pretty clear on how to construct this type.

List<SObject> records; // e.g. = [SELECT Id FROM Opportunity LIMIT 100];
ApexPages.StandardSetController controller = new ApexPages.StandardSetController(records);
MyExtension extension = new MyExtension(controller);
1
0

You are calling getSelected() in the first line of sendEmail but you never set any selected records try this:

change:

private ApexPages.StandardSetController setCon;

to

@testVisible private ApexPages.StandardSetController setCon;

Then before you call b.sendEmail() do this:

b.setCon.setSelected(sslist);

To set the selected records. Then when you do the getSelected in the first line it will have some records to work with

12
  • Thanks for your help. I have now made another edit, however it is still not working.. :/ I am fairly new to this so I appreciate very much your comments.
    – mrtjj
    Oct 28, 2015 at 0:22
  • @mrtjj - Reread my post, you did not do it correct. You put the getSelected before the call in your test method not the setSelected(splits)
    – Eric
    Oct 28, 2015 at 0:23
  • Thanks. Error went away but now I only get 5% coverage... What should I replace (splits) with? sslist? It still only return 5 % coverage. Updating an edit with the updated code now.
    – mrtjj
    Oct 28, 2015 at 0:42
  • Yes...dang auto correct
    – Eric
    Oct 28, 2015 at 0:42
  • 1
    @Eric Doesn't it just essentially coerce = to IN? Agreed that these edits are getting out of hand.
    – Adrian Larson
    Oct 28, 2015 at 14:41

You must log in to answer this question.

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