• Alison Richards
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies
Created Apex Trigger not working to send Service Report PDF when email service report checkbox equals true on the Service Appointment Object, where contact email field contain's contact email. . 


trigger serviceReportSendEmail on ServiceReport (after insert) {
    ServiceAppointment so;
    string soId;
    List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
    List<String> sendTo = new List<String>();
     
    for(ServiceReport sr : trigger.new){
        try {
            // Get service appointment details inc contact email
            soId = sr.ParentId;
            so = [select Id, Email_Service_Report__c, Contact_Email__c, Appointment_Number__c from ServiceAppointment where Id = :soId limit 1];

            // Check contact email exists and report should be sent           
            if(so.Email_Service_Report__c && so.Contact_Email__c != '' && so.Contact_Email__c != null)
            {
                sendTo.add(so.Contact_Email__c);
                Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
                mail.setToAddresses(sendTo);
                mail.setSubject('Service Report Created for Appointment Number: '+ so.Appointment_Number__c);
                String body = 'Your service report is attached.';
                mail.setHtmlBody(body);
                mails.add(mail);
                 
                // Get file for attachment
                List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
                Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                efa.setContentType('application/pdf');
                efa.setInline(false);
                efa.setFileName(sr.ServiceReportNumber);
                // Have to fetch DocumentBody, it is null when accessing directly
                Blob attBody = [select DocumentBody from ServiceReport where Id = :sr.Id limit 1].DocumentBody;
                efa.setBody(attBody);
                fileAttachments.add(efa);
                mail.setFileAttachments(fileAttachments);
 
                Messaging.sendEmail(mails);
            }
        }
        catch(exception e) {
            System.debug('************************** Exception in serviceReportSendEmail trigger:'+e);
        }
    }
}
 
I need to create a trigger that updates the Service Appointment Object Assigned Resource Look up field when the related listed called Assinged Resource is assigned. 

Long story short I am trying to get the assigned resource printed on the Work Order. All that is revealed to be using the Related List is the ID. If I can easily covert the Id through a formula or trigger on the Assigned Resource Object easily than posting the Assigned Resource look up on the Service Appointment Object whichever is easiest. 
I have created a custom object called Project and which holds two look up fields Project_Manager__c and Project_Coordinator__c. I am trying to have those fields be updated by the look up fields on the Account object also named Project_Manager__c and Project_Coordinator__c when a project is created. Any help is greatly appreciated. I was told apex trigger was the way to go. 

Thanks. 
I have created a custom object called Project and which holds two look up fields Project_Manager__c and Project_Coordinator__c. I am trying to have those fields be updated by the look up fields on the Account object also named Project_Manager__c and Project_Coordinator__c when a project is created. Any help is greatly appreciated. I was told apex trigger was the way to go. 

Thanks. 
Hey do you know if it’s possible to send email with attachment when a new service report has been created through apex trigger? In field service when my techs create a service report we have to go in and manually email the pdf report each time, but there has to be a way to do this automatically. Could you help?