+ Start a Discussion
Kathryn BullockKathryn Bullock 

unable to get the code to work but no errors

I have no errors in the code, but the button is not working.  The idea is that it is supposed to send an email to the contact email of X3_Party_Installer and then create an activity as well.  Yet, when I press the button nothing happens.  Is there something wrong with the code?

Controller:
public class SendSiteSurveyEmailController {

    public String Site_Survey {get;set;}
    
    Public SendSiteSurveyEmailController(ApexPages.StandardController controller) {
        Site_Survey = ApexPages.currentPage().getParameters().get('Id');
    }
    
    Public Pagereference SendSiteSurveyFunction() {
        list<Site_Survey__c> sitelist = [SELECT X3rd_Party_Installer__c, X3rd_Party_Installer__r.email FROM Site_Survey__c LIMIT 1];
		String toaddress= sitelist[0].X3rd_Party_Installer__r.email;
        try{
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] addr = new String[] {toaddress};
                mail.setToAddresses(addr);
            	mail.setReplyTo(toaddress);
            	mail.setSenderDisplayName('Name');
            	mail.setSubject('DarPro Site Survey');
            	mail.setBccSender(false);
            	mail.setUseSignature(true);
            	mail.setHtmlBody('<b> BODY </b>');
            
            List<Messaging.EmailFileAttachment> fileAttachments = new List<Messaging.Emailfileattachment>();
            for (Attachment a : [SELECT Name, Body, BodyLength FROM Attachment WHERE ParentId = :Site_Survey Order By LastModifiedDate DESC Limit 1]){
                Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                efa.setFileName(a.Name);
                efa.setBody(a.Body);
                fileAttachments.add(efa);
                //mail.setFileAttachments(new Messaging.SingleEmailMessage[] {efa});
            }
            mail.setFileAttachments(fileAttachments);
            
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            
            Contact ct = [SELECT id FROM Contact WHERE Email = :toaddress LIMIT 1];
            
            Task newTask = new Task(Description = 'Site Survey Email',
                                   Priority = 'Normal',
                                   Status = 'Completed',
                                   Subject = 'Site Survey',
                                   Type = 'Email',
                                   WhoId = ct.Id);
        } catch(Exception e) {}
        
        PageReference reference = new PageReference('https://darlingingredients--griffsdbx.cs11.my.salesforce.com/'+Site_Survey);
        reference.setRedirect(true);
        return reference;
    }
}

VF Page: 
<apex:page standardController="Site_Survey__c" extensions="SendSiteSurveyEmailController">
<apex:form >
<script type="text/javascript">
function init() {
sendEmail();
}
if(window.addEventListener)
window.addEventListener('load',init,true)
else
window.attachEvent('onload',init)
</script>

<apex:actionFunction name="sendEmail" action="{!sendSiteSurveyFunction}">
</apex:actionFunction>
</apex:form>
</apex:page>

 
PawanKumarPawanKumar
can you please try below.

<apex:page standardController="Site_Survey__c" extensions="SendSiteSurveyEmailController">
<apex:form >
<apex:actionFunction name="sendEmail" action="{!sendSiteSurveyFunction}">
</apex:actionFunction>
        <script>
           window.onload=function()
           {
             sendEmail();
           };
        </script>

</apex:form>
</apex:page>

Please mark it best if it helps you. Thanks.
Kathryn BullockKathryn Bullock
Unfortunately, it is still not working.  What else should I try?
PawanKumarPawanKumar
Okay. Please try below.

<apex:page standardController="Site_Survey__c" extensions="SendSiteSurveyEmailController" action="{!sendSiteSurveyFunction}">
</apex:page>
Kathryn BullockKathryn Bullock
Thank you for the help!  That one does not work either.  Maybe I have something wrong with the controller?
PawanKumarPawanKumar
is the button on survey detail page?
Kathryn BullockKathryn Bullock
It's on the SIte Survey (Custom Object) Page