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
SlanganSlangan 

List question - adding input field to checkbox list

Hi everyone & thanks again for the great advice I always get here. This is an awesome community.

 

I am having a bit of trouble that I hope someone will be able to help me with - I keep thinking the answer is obvious, but I just don't have the experience yet to figure it out on my own.

 

I have a list of automatically generated email addresses and names with checkboxes next to each name. When a user selects a name that value (person) is automatically added to an email that is sent when the user hits a send button. I would like to add an input field that would allow the user to enter email addresses not found on the list, but am unsure how to grab the value and have it added to the checkbox list values. I do not want the value to be permanent - it should be disgarded as soon as the email is sent.

 

Code is below - thank you so much for any advice or guidance.

 

Shannon

<apex:page standardController="Opportunity" Extensions="addERF" showHeader="false" sideBar="false">
<apex:form >
<apex:pageBlock >
<apex:pageblockSection >
<apex:outputText value="Please select the people to whom you would like to email your ERF:"/><br/><br/>
<apex:pageblockSectionItem >
<apex:selectCheckboxes value="{!Addresses}" layout="pageDirection" style="text-align:left;">
<apex:selectOptions value="{!items}"/>
</apex:selectCheckboxes>
</apex:pageblockSectionItem><br/>
<apex:commandButton value="Send" action="{!emailERF}" status="status" />
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>

 

 

public class addERF {

ApexPages.StandardController controller;
public addERF(ApexPages.StandardController c) {
controller = c;}

public PageReference emailERF() {
//Get the opportunity Id and name
Opportunity opportunity = [SELECT id, name, ERF_Revision_Number__c, Opportunity_No__c FROM opportunity WHERE Id = :System.currentPageReference().getParameters().get('id')];

//Create PDF
// Reference the page, pass in a parameter to force PDF
PageReference pdf = new PageReference('/apex/ERFpdf' + '?id=' + opportunity.id);
pdf.getParameters().put('p','p');
pdf.setRedirect(true);
// Grab the PDF!
Blob b = pdf.getContent();
/* create the attachment against the Opportunity */
Attachment a = new Attachment(parentId = opportunity.id, name=opportunity.Name+'-'+opportunity.Opportunity_No__c+'-'+'ERF'+'-'+'Rev_'+opportunity.ERF_Revision_Number__c+'.pdf', body = b);
/* insert the attachment */
insert a;


// Create an email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
email.setSaveAsActivity(true);
email.setToAddresses(addresses);
email.setSenderDisplayName('New ERF');
email.setSubject('New ERF Created for Opportunity # ' + opportunity.Opportunity_No__c);
email.setPlainTextBody('A new ERF has been created for ' + opportunity.Name +' , '+ opportunity.Opportunity_No__c+'. The ERF is attached.');
email.setHtmlBody('A new ERF has been created for' + '&nbsp;'+'<b>'+ opportunity.Name +'&nbsp;'+'</b>'+ opportunity.Opportunity_No__c+'. The ERF is attached.'+
' To view this Opportunity <a href=https://na1.salesforce.com/'+ opportunity.Id +'><b>click here</b></a>');

// Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
// Set name of PDF
efa.setFileName(opportunity.Name+'-'+opportunity.Opportunity_No__c+'-'+'ERF'+'-'+'Rev_'+opportunity.ERF_Revision_Number__c+'.pdf');
// Set body of PDF
efa.setBody(b);
// Attach the PDF to your email
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

// Send email & return to Opportunity
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});


/* send the user back to the opportunity detail page */
return controller.view();


}


String[] addresses = new String[]{};

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('a@a.com','A'));
options.add(new SelectOption('b@b.com','B'));
options.add(new SelectOption('c@c.com','C'));
options.add(new SelectOption('d@d.com','D'));
return options;
}
public String[] getAddresses() {
return addresses;
}
public void setAddresses(String[] addresses) {
this.addresses = addresses;
}


//******************
//Test Method
//******************

public static testMethod void TestaddERF() {

// Insert test Opportunity
Opportunity testOppty = new opportunity (
Name = 'Test Opportunity',
Amount = 5000,
StageName = 'Closed Won',
CloseDate = System.today());
insert testOppty;

// Instantiate VisualForce Page
PageReference pg = Page.ERFpdf;
Test.setCurrentPage(pg);
ApexPages.currentPage().getParameters().put('id', testOppty.id);

// Instantiate addERF controller
ApexPages.StandardController c = new ApexPages.standardController(testOppty);
addERF qe = new addERF(c);
Blob bTest = pg.getContent();

// Test the List Options getItems() method
List<SelectOption> options = qe.getItems();
System.assertEquals(options.size(), 14);
String[] addresses = new String[1];
addresses[0] = 'test@krausglobal.com';
qe.setAddresses(addresses);
System.assertEquals(qe.addresses[0], 'test@krausglobal.com');
qe.getAddresses();


// Create an email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
email.setSaveAsActivity(true);
email.setToAddresses(addresses);
email.setSenderDisplayName('TEST');
email.setSubject('TEST ' + testOppty.Opportunity_No__c);
email.setPlainTextBody('a test method has been created for ' + testOppty.Name +' , '+ testOppty.Opportunity_No__c+'. The SO Approval is attached.');
email.setHtmlBody('A new test method has been created for' + '&nbsp;'+'<b>'+ opportunity.Name +'&nbsp;'+'</b>'+ opportunity.Opportunity_No__c+'. The test SO is attached.'+
' To view this Opportunity <a href=https://na1.salesforce.com/'+ testOppty.Id +'><b>click here</b></a>');
// Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
// Set name of PDF
efa.setFileName(testOppty.Opportunity_No__c+'ERF.pdf');
// Set body of PDF
efa.setBody(bTest);
// Attach the PDF to your email
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

//call the emailSOA method
qe.emailERF();



}



}

 


 

 

 

Message Edited by Slangan on 02-23-2010 09:39 AM
NaishadhNaishadh

 

You cannot achieve the same by using standard apex select checkbox option. You need to write HTML select checkbox code. Also, to add user defined email address in select list box use javascript code. 

SlanganSlangan

Thanks for the advice - I am not well versed in Javascript, do you have any sample code you could provide to point me in the right direction? Would I place the Javascript in the header of my vf page?

 

Thanks again!

hisrinuhisrinu

You can use below url, for displaying checkboxes.

 

http://wiki.developerforce.com/index.php/Checkbox_in_DataTable