Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public with sharing class EmailPDFController {
- public Travel_Service__c opp { get; set; }
- public String selected { get; set; }
- public String emailCC {get; set;}
- public String emailBCC {get; set;}
- public Contact_lookup_for_API__c tempAccount {get; set;}
- public String htmlBodyStr {get; set;}
- public Account emailTo {get; set;}
- public Boolean failed = false;
- public String errMsg;
- public User activeUser {get; set;}
- public EmailPDFController(){
- errMsg='';
- if(ApexPages.currentPage().getParameters().get('Id') == ''){
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'No Id Specified'));
- opp = new Travel_Service__c();
- errMsg='No Id Specified';
- failed = true;
- }
- else{
- Id oppId=ApexPages.currentPage().getParameters().get('Id');
- try{
- opp = [select Id, Name, Main_passenger_name__c from Travel_Service__c where Id = :oppId];
- tempAccount = new Contact_lookup_for_API__c();
- }
- catch(Exception e){
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Invalid Travel Services Id'));
- opp = new Travel_Service__c();
- errMsg='Invalid Travel Services Id';
- failed = true;
- }
- }
- if(ApexPages.currentPage().getParameters().get('success') == 'true'){
- ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'The invoice was sent.'));
- }
- try{
- EmailTo=[select Id, PersonEmail from Account where Id=:opp.Main_Passenger__c];
- tempAccount.Account__c=emailTo.Id;
- }
- catch(Exception e){
- emailTo=new Account();
- // tempAccount.Account__c=new Id();
- }
- activeUser = [Select Email, Phone, Mobilephone, Title From User where Id = : UserInfo.getUserId() limit 1];
- emailBCC=activeUser.Email;
- htmlBodyStr = '<p>Dear '+opp.Main_Passenger_Name__c+',</p><p>Please find attached your invoice.</p><p>Kind regards,<br /><br />'+
- UserInfo.getName()+' | '+ activeUser.Title+' | Stratix Corp. | '+ activeUser.Email +' | Direct Line '+activeUser.Phone +' | Mobile '+activeUser.Mobilephone;
- //UserInfo.getName()+'<br />'+activeUser.Title+'<br />GSMA Ltd.';
- /*
- if(activeUser.Phone!=null){
- htmlBodyStr=htmlBodyStr +'<br />Office: '+activeUser.Phone+'<br />';
- }
- if(activeUser.Mobilephone!=null){
- htmlBodyStr=htmlBodyStr +'<br />Mobile: '+activeUser.Mobilephone+'<br />';
- }*/
- }
- /*public PageReference checkApproved(){
- if(!salesQuotes.approved(quote.id)){
- pagereference pr = new pagereference('/apex/proposalError?error=approvals&id='+quote.id);
- pr.setredirect(false);
- return pr;
- }
- else{
- return null;
- }
- }*/
- public PageReference sendReport() {
- if(!failed){
- System.Debug('In sendreport');
- System.Debug('body is'+htmlBodyStr);
- //System.debug(ApexPages.currentPage().getParameters());
- //return null;
- Messaging.SingleEmailMessage emailMsg = new Messaging.SingleEmailMessage();
- emailMsg.setSaveAsActivity(false);
- //emailMsg.setTargetObjectId(UserInfo.getUserId());
- System.Debug('Created email object');
- //emailMsg.setWhatId(quote.Id);
- System.Debug('Associated invoice');
- //emailMsg.setTemplateId([select Id from EmailTemplate where DeveloperName = 'quoteEmail'].Id);
- System.Debug('Set template');
- //System.Debug('Template Id is: ' + [select Id from EmailTemplate where DeveloperName = 'quoteEmail'].Id);
- System.Debug('Id is: ' + tempAccount.Account__c);
- emailMsg.setSubject('Your requested invoice from Costamar');
- if(tempAccount.Account__c == null){
- failed=true;
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Invalid Contact'));
- return null;
- }
- EmailTo= [select Id, PersonEmail, Name from Account where Id= :tempAccount.Account__c];
- System.Debug('Email is' + EmailTo.PersonEmail);
- if(EmailTo.PersonEmail == null || !EmailTo.PersonEmail.contains('@')){
- System.Debug('No valid email for Contact');
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Contact does not have a valid Email address'));
- failed=true;
- return null;
- }
- System.Debug('Email is: ' + EmailTo.PersonEmail);
- String[] toAddresses = new String[] {EmailTo.PersonEmail};
- String userEmail = activeUser.Email;
- String txtBodyStr='Dear '+EmailTo.Name+',\nPlease find attached your order form and terms and conditions. When you have reviewed both documents, please return the signed order form by fax to + 44 (0)207 356 0691.\nRegards,\n\n'+
- UserInfo.getName()+'\n';
- if(activeUser.Phone!=null){
- txtBodyStr=txtBodyStr+'\nOffice: '+activeUser.Phone+'\n';
- }
- if(activeUser.Mobilephone!=null){
- txtBodyStr=txtBodyStr +'\nMobile: '+activeUser.Mobilephone+'\n';
- }
- emailMsg.setPlainTextBody(txtBodyStr);
- emailMsg.setHtmlBody(htmlBodyStr);
- emailMsg.setToAddresses(toAddresses);
- System.Debug('Set to addresses');
- String[] emailAddresses;
- try{
- emailAddresses = emailCC.split(';');
- emailMsg.setCcAddresses(removeBlanks(emailAddresses));
- System.Debug('Set CC addresses to :' + emailAddresses);
- emailAddresses = emailBCC.split(';');
- emailMsg.setBccAddresses(removeBlanks(emailAddresses));
- System.Debug('Set BCC addresses to :' + emailAddresses);
- }
- catch(Exception e){
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Invalid CC or BCC Address. Addresses must be separated by a semicolon (;)'));
- failed=true;
- return null;
- }
- //Prepare URL for PDF page
- String profileURL = Page.CostPDF.getURL() + '?Id=' + opp.Id;
- //Add title query parm and file name
- String fileName = opp.Name + '.pdf';
- //Instantitiate PDF page
- PageReference profilePage = new PageReference(profileURL);
- //Prepare attachment
- Attachment profileAttachment = new Attachment();
- profileAttachment.Body = profilePage.getContent();
- profileAttachment.Name = fileName;
- profileAttachment.ParentId = opp.Id;
- //Insert attachment to email
- Messaging.EmailFileAttachment emailAttachment = new Messaging.EmailFileAttachment();
- emailAttachment.setFileName(fileName);
- emailAttachment.setBody(profilePage.getContent());
- emailMsg.setFileAttachments(new Messaging.EmailFileAttachment[] {emailAttachment});
- emailMsg.setFileAttachments(new Messaging.EmailFileAttachment[] {emailAttachment});
- Messaging.SendEmailResult[] sers;
- //Insert PDF as quote attachment & send email
- try
- {
- //insert profileAttachment;
- sers = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {emailMsg});
- Task task = new Task();
- task.WhatId = opp.Id;
- task.WhoId = EmailTo.Id;
- task.Subject = 'Sent Email with invoice';
- task.Type='Email';
- //task.priority= Object.Priority__c;
- task.ActivityDate = date.today();
- task.status = 'Completed';
- task.description = 'Emailed invoice ' + opp.Name;
- insert task;
- }
- catch(Exception e)
- {
- ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
- failed=true;
- return null;
- }
- Boolean allSuccessful = true;
- for(Messaging.SendEmailResult ser : sers)
- {
- if(!ser.isSuccess())
- {
- allSuccessful = false;
- break;
- }
- }
- if(allSuccessful){
- PageReference newPage = new PageReference(Page.emailInvoice.getURL()+ '?Id='+ opp.Id + '&success=true');
- failed=true;
- newPage.setRedirect(true);
- return newPage;
- }
- else
- {
- for(Messaging.SendEmailResult ser : sers)
- {
- for(Database.Error de : ser.getErrors())
- ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, de.getMessage()));
- }
- failed=true;
- return null;
- }
- }
- else{
- ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, errMsg));
- }
- return null;
- }
- private String[] removeBlanks(String[] emailAddresses)
- {
- String[] returnAddresses = new String[0];
- for(Integer i=0; i < emailAddresses.size(); i++)
- {
- emailAddresses[i] = emailAddresses[i].trim();
- if(emailAddresses[i].length() > 0) returnAddresses.add(emailAddresses[i]);
- }
- return returnAddresses;
- }
- public PageReference goBack(){
- String newPageUrl = '/'+ opp.Id;
- PageReference newPage = new PageReference(newPageUrl);
- newPage.setRedirect(true);
- return newPage;
- }
- /*** test methods ***/
- public static testmethod void testConstructor()
- {
- Test.startTest();
- /** test no id in url */
- System.Debug(' test no id in url');
- Test.setCurrentPageReference(new PageReference(Page.EmailInvoice.getURL()));
- EmailPDFController ssc = new EmailPDFController();
- System.assertEquals(true, ssc.failed);
- /** test empty id in url */
- System.Debug(' test empty id in url ');
- Test.setCurrentPageReference(new PageReference(Page.emailInvoice.getURL()+ '?Id='));
- ssc = new EmailPDFController();
- System.assertEquals(true, ssc.failed);
- /** test garbage id **/
- // System.Debug(' test garbage id ');
- // Test.setCurrentPageReference(new PageReference(Page.emailQuote.getURL()+ '?Id=asas'));
- // ssc = new emailQuoteController();
- // System.assertEquals(true, ssc.failed);
- /** test good random id **/
- System.Debug(' test good random id');
- Travel_Service__c testAccount= [select Id from Travel_Service__c limit 1];
- Test.setCurrentPageReference(new PageReference(Page.emailInvoice.getURL()+ '?id='+ testAccount.Id));
- ssc = new EmailPDFController();
- System.assertEquals(false, ssc.failed);
- /** test no contact selected **/
- System.Debug(' test no contact selected ');
- ssc.sendReport();
- System.assertEquals(true, ssc.failed);
- /* load a contact with no email */
- System.Debug('load a contact with no email');
- ssc = new EmailPDFController();
- ssc.tempAccount.Account__c=[select Id, Email from Contact where Email = '' limit 1].Id;
- ssc.emailCC='';
- ssc.emailBCC='';
- ssc.sendReport();
- System.assertEquals(true, ssc.failed);
- /* load a random contact with email */
- System.Debug('load a random contact with email');
- ssc = new EmailPDFController();
- ssc.tempAccount.Account__c=[select Id, Email from Contact where Email <> '' limit 1].Id;
- ssc.emailCC='';
- ssc.emailBCC='';
- ssc.sendReport();
- System.assertEquals(true, ssc.failed);
- /* add some CCs and BCCs */
- System.Debug(' add some CCs and BCCs');
- ssc = new EmailPDFController();
- ssc.tempAccount.Account__c=[select Id, Email from Contact where Email <> '' limit 1].Id;
- ssc.emailCC='test@email.com';
- ssc.emailBCC='test@email.com; test@email.com;test@email.com ; test@email.com';
- ssc.sendReport();
- System.assertEquals(true, ssc.failed);
- ssc.goBack();
- Test.stopTest();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement