function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Claire RookClaire Rook 

Writing test code that simulates clicking on a custom button

I'm trying to write test code for a class that is invoked by a custom button.  This is the first time I've done this and lots of googling has not provided me with the answer!

Thank you very much for your help on this!

This is the button:
/apex/SendAdverseEvents?id={!Adverse_Event__c.Id}

I have successfully created an Adverse Event in my test code but and I think I need to instantiate the SendAdverseEventsController but I'm not sure how to do this.

Here is my test code so far:
@isTest
public class SendAdverseEventsTest {
  //Setup test data
  static testMethod void SendAdverseEvtTest() {
        
    Id rtPA = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
  
    //create new account for testing       
    Account a = New Account ();
           a.FirstName = 'Robert';
           a.LastName = 'Smith';
           a.RecordTypeId = rtPA;
           a.Email__c = 'test@example.com';
           a.Gender__c = 'Male';
           Insert a;
    

   //find contact ID of the new account
     account RS = [Select PersonContactID from Account where LastName = 'Smith'];
    
    //create adverse event
    Adverse_Event__c AE = New Adverse_Event__c();
        AE.Patient_Identifier__c = RS.PersonContactId;
        AE.Does_the_patient_give_permission_to_call__c = 'Yes';
            AE.Does_the_patient_give_permission_to_cont__c = 'Yes';
          AE.Suspect_Drug_1__c = 'Suspect Drug 1';
          AE.Brand__c = 'Harvoni';
          AE.Is_Patient_Affiliated_with_an_Ambassador__c = 'No';
          Insert AE;
        system.debug(AE);
       
        //find ID of the new adverse event
       Adverse_Event__c AECreated = [Select ID, Name, Auto_Initials__c, Suspect_Drug_1__c, Document_File_Name__c, Document_File_Name_Gilead__c, CreatedDate from Adverse_Event__c where Patient_Identifier__r.LastName = 'Smith'];
    system.debug(AECreated);
        system.debug(AECreated.id);
    
    
    //test.startTest();
      //SendAdverseEventsController con = new SendAdverseEventsController(); // Create instance of controller class
      //con.adverseEvent.id = AECreated.Id; // Set the Id
      //con.SendAdverseEventsController(); // Call the class method, before calling method, you can populate the fields, which are require in this method
    //test.stopTest();
  }
}
    
The commented code at the end I copied from developer forums but I can't work out how to get it to work.

The SendAdverseEventsController is this:

public with sharing class SendAdverseEventsController extends BaseClass {
    
    public Adverse_Event__c adverseEvent { get; set; }
    public String toAddresses { get; set; }
    public String toContacts { get; set; }
    public String ccAddresses { get; set; }
    public String ccContacts { get; set; }
    public String bccAddresses { get; set; }
    public String bccContacts { get; set; }
    public String documentTemplateId { get; set; }
    public String subject { get; set; }
    public String body { get; set; } 
    public User contactLookup {get; set;}
    public Boolean hasAccess { get; set; }
    
    public static final String GLOBAL_EMAIL_NAME = 'AE Reports';
    public static final String GLOBAL_EMAIL = 'aereports@snow-companies.com';
    
    public String defaultBccEmail {get;set;}
    
    private Map<Id,String> documentTemplates;
    private Map<String,String> contactNames;
    
    
    public SendAdverseEventsController() {
        
        this.defaultBccEmail = GLOBAL_EMAIL;
        this.bccAddresses = GLOBAL_EMAIL;
        
        contactLookup = new User();

        Map<String,String> params = ApexPages.currentPage().getParameters();

        for (Adverse_Event__c ae : [select Name, Auto_Initials__c, Suspect_Drug_1__c, Document_File_Name__c, Document_File_Name_Gilead__c, CreatedDate from Adverse_Event__c where Id = :params.get('id')]) {
            adverseEvent = ae;  
        }
        
        for (EmailTemplate et : [select Subject, HtmlValue from EmailTemplate 
                                  where Folder.Name = 'Adverse Events Emails' 
                                    and Name = 'Email Template']) {
            //subject = et.Subject;
            subject = adverseEvent.Document_File_Name__c;
            body = et.HtmlValue;
        }

        String username = UserInfo.getUserName().split('@')[0];

        if (ApexPages.currentPage().getParameters().get('signature') != null) {
            username = ApexPages.currentPage().getParameters().get('signature');
        }

        for (EmailTemplate et : [select Subject, HtmlValue from EmailTemplate 
                                  where Folder.Name = 'Signatures' 
                                    and Name = :username]) {
            
            if (body == null) {
                body = '';
            }
            
            body += et.HtmlValue;
        }
        List<Adverse_Events_Access__c> access = Adverse_Events_Access__c.getAll().values();
        hasAccess = false;
        for(Adverse_Events_Access__c aea : access){
            if(aea.Username__c == UserInfo.getUserName()){
                hasAccess = true;
                break;
            }
        }
    }
    
    public String getGileadSubject(Adverse_Event__c  ae){
       String dateString = ae.CreatedDate.format('ddMMMyyyy').toUpperCase();
       String subjectFormatted = dateString + '-' + ae.Auto_Initials__c + '-' + ae.Suspect_Drug_1__c + '-' + ae.Name;
       
       return subjectFormatted;
   }
   
   public void setSubjectName(){
       if(this.documentTemplateId != null && documentTemplates.get(this.documentTemplateId) == 'Gilead AE Form'){
            subject = this.getGileadSubject(adverseEvent);
       }
       else{
           subject = adverseEvent.Document_File_Name__c;       
       }
   }
    
    public SelectOption[] getTemplates() {
        SelectOption[] templates = new SelectOption[]{ new SelectOption('', '-None-') };
        
        documentTemplates = new Map<Id,String>();
        for (EmailTemplate et : [select Name from EmailTemplate where Folder.Name = 'Adverse Events Templates']) {
            templates.add(new SelectOption(et.Id, et.Name));
            documentTemplates.put(et.Id, et.Name);  
        }
        
        return templates;
    }
    
    
    public PageReference sendEmail() {
        
        //mergeContactsAddresses();
        
        MandrillClient.Message message = new MandrillClient.Message();
        addEmailAddresses(message.to, toAddresses, 'to');
        addEmailAddresses(message.to, ccAddresses, 'cc');
        addEmailAddresses(message.to, bccAddresses, 'bcc');
        
        if (!message.to.isEmpty()) {
            
            //User user = [select Name, Email from User where Id = :UserInfo.getUserId()];                    
            //message.from_name = user.Name;
            //message.from_email = user.Email;
            
            message.from_name = GLOBAL_EMAIL_NAME;
            message.from_email = GLOBAL_EMAIL;
            
            this.setSubjectName();
            
            message.html = body;
            message.subject = subject;
            message.campaign = 'GPS'; //TODO: Change for other
            message.what = adverseEvent.Id;
            
            //Attachment
            Blob file = new PageReference('/apex/DocumentMerge?id=' + adverseEvent.Id + '&tid=' + documentTemplateId).getContent();
            MandrillClient.MandrillAttachment attach = new MandrillClient.MandrillAttachment();
            //attach.name = documentTemplates.get(documentTemplateId) + '.pdf';
            attach.name = subject + '.pdf';
            attach.type = 'application/pdf';
            attach.content = Encodingutil.base64Encode(file);
            message.attachments.add(attach); 
            
            String allRecipients = '';
            
            for (MandrillClient.Recipient recipient : message.to) {
                allRecipients += ', ' + recipient.email;
            }
            
            allRecipients = (allRecipients != '' ? allRecipients.substring(2) : '');
            
            try {
                Map<String,String> response = MandrillClient.send(new MandrillClient.Message[]{ message });
                Task[] tasks = MandrillClient.insertTasks();
                
                for (Task t : tasks) {
                    t.Adverse_Events_Recipients__c = allRecipients;
                }
                update tasks;
                
                Attachment a = new Attachment();
                a.Name = attach.name;
                a.ContentType = attach.type;
                a.Body = file;
                a.ParentId = adverseEvent.Id;
                insert a;
                
                return new PageReference('/' + adverseEvent.Id);
            }
            catch (Exception e) {
                addErrorMessage(e.getMessage());
            }                            
        }
        
                        
        return null;
    }
    
    
    private void mergeContactsAddresses() {
        String[] cids = new String[]{};
        
        for (String cid : toContacts.split(';')) {
            cids.add(cid);
        }
        for (String cid : ccContacts.split(';')) {
            cids.add(cid);
        }
        for (String cid : bccContacts.split(';')) {
            cids.add(cid);
        }
        
        Map<String,String> contactEmails = new Map<String,String>();
                
        contactNames = new Map<String,String>();
        for (Contact c : [select Name, Email from Contact where Id = :cids and Email <> null]) {
            contactNames.put(c.Email.toLowerCase(), c.Name);
            contactEmails.put(c.Id, c.Email.toLowerCase());
        }
        
        for (String cid : toContacts.split(',')) {
            if (contactEmails.containsKey(cid)) {
                if (toAddresses != null && toAddresses.trim() != '') {
                    toAddresses += ',';
                }
                toAddresses += contactEmails.get(cid) + ',';
            }
        }
        for (String cid : ccContacts.split(';')) {
            if (contactEmails.containsKey(cid)) {
                if (ccAddresses != null && ccAddresses.trim() != '') {
                    ccAddresses += ',';
                }
                ccAddresses += contactEmails.get(cid) + ',';
            }
        }
        for (String cid : bccContacts.split(';')) {
            if (contactEmails.containsKey(cid)) {
                if (bccAddresses != null && bccAddresses.trim() != '') {
                    bccAddresses += ',';
                }
                bccAddresses += contactEmails.get(cid) + ',';
            }
        }        
    }
    
    
    private void addEmailAddresses(MandrillClient.Recipient[] recipients, String addr, String type) {
        if (addr != null) {
            for (String email : addr.split(',')) {
                if (email != null && email.trim() != '') {
                    MandrillClient.Recipient recipient = new MandrillClient.Recipient();
                    recipient.email = email.trim().toLowerCase();
                    
                    //if (contactNames.containsKey(recipient.email)) {
                    //   recipient.name = contactNames.get(recipient.email);    
                    //}
                    
                    recipient.type = type;
                    recipients.add(recipient);              
                }
            }
        }
    }
    
    
    public PageReference cancel() {
        if (adverseEvent != null) {
            return new PageReference('/' + adverseEvent.Id);
        }
        else {
            return new PageReference('/home/home.jsp');
        }
    }
}
 
Best Answer chosen by Claire Rook
RaidanRaidan
Hi Claire,

This is how you instantiate and pass the parameter to the controller:
PageReference pr = Page.SendAdverseEvents;
Test.setCurrentPage(pr);

ApexPages.currentPage().getParameters().put('id', AE.Id);
SendAdverseEventsController con = new SendAdverseEventsController();

 

All Answers

RaidanRaidan
Hi Claire,

This is how you instantiate and pass the parameter to the controller:
PageReference pr = Page.SendAdverseEvents;
Test.setCurrentPage(pr);

ApexPages.currentPage().getParameters().put('id', AE.Id);
SendAdverseEventsController con = new SendAdverseEventsController();

 
This was selected as the best answer
Claire RookClaire Rook
Thank you!