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
TechBlossomTechBlossom 

Making changes in the custom object fields from a controller extension

Hi,

 

Following is my code for sening email from a button on a custom object Safari__c. 

Safari__c is defined as the standard controller for the page. At the end of the code, if the mail sent is successful, I want to be able to update certain fields (Email_Flag__c and Email_Date__c) on the Safari__c object. I am defining a new Safari__c in the constructor, but I am not able to access these fields. 

How can this be done?? 

Any kind of help is greatly appreciated.

 

Thanks in advance.

 

public with sharing class SendEmailPage {

public String contactEmail1 {get; set;}
public String contactEmail2 {get; set;}
public String contactEmail3 {get; set;}
public Id AccId {get; set;}

public SendEmailPage(ApexPages.StandardController controller) {
try{

Safari__c test=new Safari__c();
test=[Select Account__c,Email_Flag__c,Email_Date__c From Safari__c Where id=:ApexPages.currentPage().getParameters().get('id')];
AccId=test.Account__c;}

catch(Exception e){
}

}
//Our collection of the class/wrapper objects attachmentwrapper
public List<attachmentwrapper> attachmentList = new List<attachmentwrapper>();

List<Attachment> selectedAttachments = new List<Attachment>();

//This method uses a simple SOQL query to return a List of Attachments
public List<attachmentwrapper> getAttachments()
{
attachmentList.clear();
for (Attachment a : [select Name from Attachment where ParentId = :ApexPages.currentPage().getParameters().get('id')])
attachmentList.add(new attachmentwrapper(a));
return attachmentList;
}
//We create a new list of Attachments that will be populated only with Attachments if they are selected
public void getselectedAttachments()
{
selectedAttachments.clear();
//We will cycle through our list of attachmentwrapper and will check to see if the selected property is set to true, if it is we add the Attachment to the selectedAttachment list
for(attachmentwrapper attwrapper : attachmentList)
{
if(attwrapper.selected == true)
{
selectedAttachments.add(attwrapper.att);
}
}
for(Attachment att: selectedAttachments) {
system.debug(att);
}
}

// This is our wrapper/container class
public class attachmentwrapper
{
public Attachment att{get; set;}
public Boolean selected {get; set;}
//This is the contructor method. When we create a new attachmentwrapper object we pass a Attachment that is set to the att property.
//We also set the selected value to false
public attachmentwrapper(Attachment a)
{
att = a;
selected = false;
}
}

public String response { get; set; }
public String body { get; set; }
public String subject { get; set; }

private final Safari__c safari;
// Create a constructor that populates the Proposal object
public Safari__c getSafari() {
return safari;
}

public PageReference sendEmail() {
// Define the email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = ContactEmail1.split(',',0);
mail.setToAddresses(toAddresses);
mail.setTargetObjectId(UserInfo.getUserId());
//mail.setTargetObjectId('003G0000017OONf');


system.debug('List of To Contacts: '+ contactEmail1);
mail.setReplyTo('noreply@yourcompany.com');
mail.setSenderDisplayName('Your Company Name');
mail.setSubject('Your Subject Here');
mail.setPlainTextBody(body);
mail.setWhatId(ApexPages.currentPage().getParameters().get('id'));
if(ContactEmail2 !=NULL && ContactEmail2.trim() != '')
{
String[] ccaddress=ContactEmail2.split(',', 0);
mail.setCcAddresses(ccaddress);
system.debug('my cc addresses here');

}

if(ContactEmail3 !=NULL && ContactEmail3.trim() != '')
{
String[] bccaddress=ContactEmail3.split(',', 0);
mail.setBccAddresses(bccaddress);
system.debug('my cc addresses here');
}
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
getselectedAttachments();
for(Integer j = 0; j < selectedAttachments.size(); j++)
{
// Add to attachment file list
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(selectedAttachments.get(j).Name);
string sname=selectedAttachments.get(j).Name;
Attachment a =[select Attachment.Name, Attachment.Body from Attachment where Attachment.Name =: sname ];
efa.setBody(a.body);
fileAttachments.add(efa);
efa=null;
}
mail.setFileAttachments(fileAttachments);
mail.setSaveAsActivity(true);
try{
Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
if(resultMail[0].isSuccess())
response = 'ok sent!';
else{
response = resultMail[0].getErrors().get(0).getMessage();
}
Id id= ApexPages.currentPage().getParameters().get('id');
PageReference newPage = new PageReference('/'+id);
newPage.setRedirect(true);
return newPage;
}catch(System.EmailException ex){
response = ex.getMessage();
}
return null;
}

public pagereference cancel()
{
Id id= ApexPages.currentPage().getParameters().get('id');
PageReference newPage = new PageReference('/'+id);
newPage.setRedirect(true);
return newPage;
}

}

Jay EcklesJay Eckles

Use a trigger to make changes to the object after the email is sent.