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
SamuelRobertSamuelRobert 

How to Print the order Summary and send the Attachment in the Email to the Customer

Hi I have a Form that will be submitted by our customer. At the end of the Form they will have submit button that will call the Apex class to insert the record in the Salesforce order object.
Here I have tried a Print functionality so before submitting Customer can print the order summary.
Now I was asked to make the print functionality in background and it should be sent to Customer along with the Mail after they submit the form.
 
public PageReference OrderSave(){                        



    if(!validateOrderContactInformation() )
        return pageF();

    if(order.Type!='Web Timesheet' && order.Type_Of_Order__c=='New Client or Web Time Branch'|| order.Type_Of_Order__c== 'Redeploy' || order.Type_Of_Order__c== 'Reactivation')
        if(!validateShippingAndNetworkInformation())
        return pageS();        
    else if(order.Type=='Web Timesheet' && order.Type_Of_Order__c=='New Client or Web Time Branch'|| order.Type_Of_Order__c== 'Redeploy' || order.Type_Of_Order__c== 'Reactivation')
        if(!validateWebTimesheetInformation())
        return pageS();

    if(order.Type!='Web Timesheet' && order.Type_Of_Order__c=='New Client or Web Time Branch'|| order.Type_Of_Order__c== 'Redeploy' || order.Type_Of_Order__c== 'Reactivation')
        if(!validateClockConfiguration())
        return pageT();                

    if(order.Type=='Web Timesheet' || Test.isRunningTest())
        order.Number_of_Clock_Required__c = 1;
    List<Order> orderList = fillNumberOfOrders(); 
    try {     
        //order.Group_Name__c = GroupLookUp;
        insert orderList;

    } catch (DMLException e) {     
        System.debug('Error creating new Order.' + e.getMessage() + ' : ' + e.getLineNumber());
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new Order.' + e.getMessage() + ' : ' + e.getLineNumber()));            
        if(!Test.isRunningTest())
            return null;
    }

The above code is my save method.
 
if(orderList!=null && !orderList.isEmpty() ){        

        if(order.Training_Required__c=='Yes')
            fillAndSaveTrainingDetails(true,orderList.get(0).Id); 

        if(order.Type_Of_Order__c=='New Client or Web Time Branch'|| order.Type_Of_Order__c== 'Redeploy' || order.Type_Of_Order__c== 'Reactivation' && order.Type!='Web Timesheet' )
        {     
            try{
            fillAndSaveDepartmentDetails(true,orderList.get(0).Id);            

            for(SoftSchedulingShift__c sss : softschedulingss)                                                        
                sss.OrderId__c = orderList.get(0).Id;                                              
            insert softschedulingss; 
            }
            catch(Exception ex)
        {      
            System.debug('Line Number is = '+ex.getLineNumber()+' Message is = ' +ex.getMessage());
        }
            if(order.IP_Restricted__c == 'Yes')
                fillAndSaveIPaddressDetails(true, orderList.get(0).Id);

        }

        if(order.Type_Of_Order__c=='New Client or Web Time Branch'|| order.Type_Of_Order__c== 'Redeploy' || order.Type_Of_Order__c== 'Reactivation' && order.Type=='Web Timesheet')
        {
            if(groupTClients!=null && !groupTClients.isEmpty())
            {
                for(GroupTimeSheet_Client__c client : groupTClients)
                    client.OrderId__c = orderList.get(0).Id;   
                insert groupTClients;
            }
        }

        if(order.Type_Of_Order__c=='New Client or Web Time Branch'|| order.Type_Of_Order__c== 'Redeploy' || order.Type_Of_Order__c== 'Reactivation' || order.Type_Of_Order__c=='Add-On to Existing Site' || order.Type_Of_Order__c=='Upgrade (Change in Hardware) to Existing Site')
        {
            //string attachmentName = '';
            try{
                if(attachment!=null && attachment.Name!=null){   
                    attachment.IsPrivate = true;        
                    //uploadFileName = attachment.Name;
                    mail_attach_yes_no ='Yes';
                    attachment.ParentId = orderList.get(0).Id;
                    insert attachment;
                    attachment =null;
                }
                mail_attach_yes_no =  'No';
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Thanks for submitting your request!'));          
            }
            catch (DMLException e) {   
                attachment = new Attachment();
                System.debug('Attachment issue: ' + e.getMessage() + ' : ' + e.getLineNumber());                        
            }
        }

        String succMes = (orderList.size()>1 ? 'Thank you for your order request.  Your Order Numbers are ' : 'Thank you for your order request.  Your Order Number is ');
        String OrderNumbers = '';

        List<Id> ids = new List<Id>();
        for(Order ord : orderList)            
            ids.add(ord.Id);

        List<Order> newOrders = [select Id, OrderNumber,Order_Contact__c,Order_Contact_Email__c from order where Id in :ids];            
        if(newOrders!=null && !newOrders.isEmpty())
            for(Order ord : newOrders)            
            OrderNumbers = (OrderNumbers=='' ? OrderNumbers + ord.OrderNumber : OrderNumbers +  ', ' + ord.OrderNumber);

        // Email Trigger
        String userName  = (conts!=null && !conts.isEmpty()) ? conts.get(0).name : 'Customer' ;
        emailTrigger(newOrders, OrderNumbers,userName);

        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.CONFIRM,succMes + OrderNumbers));
        order = new Order();
        orderList = new List<Order>();
        newOrders = new List<Order>();

        String pageref = '/apex/OOFormImpSuccess?OrderNumbers=' + OrderNumbers + ';CID=' + accountId;        
        PageReference pgRef =  new PageReference(pageref);     
        pgRef.setRedirect(true);
        return pgRef;

        //return movePage('OOFormImpSuccess'); 
    }

The above code is where the Order number will be generated and the page will move to the Success page.
 
public void emailTrigger(List<Order> orders, String orderNumbers, String orderContactName)
{                        
    String subject = orders.size() >1 ? ('Your Orders are created and Numbers are : ' + orderNumbers ): ('Your Order is created and Number is : ' + orderNumbers ) ;
        String toEmail = contactEmail;

    System.debug('toEmailtoEmail : ' + toEmail);
    String bot_firstLine = 'Dear ' + orderContactName +','; 

    String succMes = (orders.size()>1 ? 'Thank you for your order request.  Your Order Numbers are ' : 'Thank you for your order request.  Your Order Number is ');

    String bot_firstLine_1 = succMes + orderNumbers; 

    String bot_secondLine= 'Thank you for your Peoplenet order.';
    String bot_thirdLine='An Implementation Consultant (IC) will be assigned to assist you with this implementation and will contact you if they have any questions about the information provided on the form.';        
    String bot_fourthLine = 'If you have questions regarding this order, please contact your IC and reference the order number provided above.';        
    String bot_fifthLine  = 'If you do not know who your assigned IC is, please email Orders@peoplenet.com';                        
    String bot_sixthLine = 'Thank you,';        
    String bot_seventhLine = 'Peoplenet Implementation Services';                

    String space = '<br>';

    String paraStart = '<html><body><div><p>';
    String paraEnd = '</p></div></body></html>';

    String bot =paraStart + bot_firstLine + space + space + bot_firstLine_1+ space + space + bot_secondLine + space + bot_thirdLine + space +space + bot_fourthLine + space +space+ bot_fifthLine + space +space + bot_sixthLine + space +bot_seventhLine + space +space + paraEnd;
    if(toEmail!=null){
        System.debug('toEmailtoEmail : ' + toEmail);
        Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        String[] sendingTo = new String[]{toEmail};
            semail.setToAddresses(sendingTo);
        String[] sendingCC =  new String[]{'orders@peoplenet.com'};
            semail.setCcAddresses(sendingCC);
        semail.setSubject(subject);                                                
        semail.setHtmlBody(bot);   

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});     
    }
}

The above code is for to Send a Email trigger to the customer once the Order is submitted.
Here I have to include the Attachment of the order summary and send mail to the Customer.
Please advise how this can be achieved.