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
Songze Li 15Songze Li 15 

pdf attach issue

Hi All,

Our system meet a strange error recently when using the code to generate the pdf and attach it to the event. We have a trigger in event object to call a future method in the APEX. It works well for most of the time but sometimes we will receive the error email generated by the class and the pdf was not attached successfully. Could anyone please kindly help us for this issue?

APEX Code:

/**********************************************************************************
 * Created Date - 04-Aug-2016
 * Purpose - Acts as a helper to the trigger CMS_Event_PDF_Gen. 
 ********************************************************************************/
public class CMS_Event_PDF_Gen_Helper
{
    public static boolean recursiveCheck = true;
    /**
     * Method Name: public static void addPDFAttach(String sessionId, list<id> eventIds)
     * Purpose: to render the vf page CMS_MedicalEvents in pdf format and upload it under the Notes and Attachments section
     * The method is called asynchronously to allow for running the vf page.
     * */
    @Future(callout=true)
    public static void addPDFAttach(String sessionId, list<id> eventIds)
    {
        //CMS_AddPdfToAttachment.addPDF(eventIdSet);
        //Instantiate a list of attachment object
         list<attachment> insertAttachment = new list<attachment>();
         Map<id,Medical_Event_vod__c> mapIdtoEvent = new Map<Id,Medical_Event_vod__c>();
         for(Medical_Event_vod__c event : [select id,name,CMS_Status__c from Medical_Event_vod__c where id IN :eventIds])
         {
             mapIdtoEvent.put(event.id,event);
         }
         for(Id eventId: eventIds)
         {
              PageReference pdf;
              pdf = Page.CMS_MedicalEvents;
              pdf.getParameters().put('id',eventId);
              
              if(pdf!=null)
              {
                  Blob body;
                  Attachment attach = new Attachment();
                  try
                  {
                      body = pdf.getContentASPDF();
                      attach.Body = body;
                      attach.contentType='application/pdf';
                    // add the Event name as an attachment name
                    if(mapIdtoEvent.get(eventId).CMS_Status__c.equals('Review Completed') || mapIdtoEvent.get(eventId).CMS_Status__c.equals('Planned'))
                    {
                    system.debug('EventId for Plannedevent'+eventId);
                    system.debug('EventName'+mapIdtoEvent.get(eventId).name);
                      attach.Name = mapIdtoEvent.get(eventId).name+'_'+'Planned_Report.pdf';
                      }
                    else if(mapIdtoEvent.get(eventId).CMS_Status__c.equals('Approved'))
                      attach.Name = mapIdtoEvent.get(eventId).name+'_'+'Actual_Report.pdf';
                      attach.IsPrivate = false;
                    // attach the pdf to the event
                      attach.ParentId = eventId;
                  }
                  catch(VisualforceException e)
                  {
                      //body = Blob.valueOf('Exception');
                      System.debug('Exception caught while creating an attachment' + e.getMessage());

                      //PG-3925: Event planned pdf report or actual pdf report is missing in special scenarios.
                      //Added email notification when exception occurs.
                      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                      String[] toAddresses = new String[]{'testemail@163.com'};
                      mail.setToAddresses(toAddresses);
                      mail.setSubject('CMS_Event_PDF_Gen_Helper class threw a VisualforceException');
                      mail.setPlainTextBody('CMS_Event_PDF_Gen_Helper class threw a VisualforceException: ' + e.getMessage() 
                            +' at line number ' + e.getLineNumber() + ' caused by ' + e.getCause());
                      Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
                  } 
                  
                  insertAttachment.add(attach);
              }      
        }

        if(insertAttachment != null && insertAttachment.size() > 0)
        {
            try
            {
               Database.SaveResult[] savResult = Database.insert(insertAttachment); 
                for(Database.SaveResult sr:savResult){
                    system.debug('Errors-------->'+sr.getErrors());
                }
            }
            catch(DMLException e)
            {
                //PG-3925: Event planned pdf report or actual pdf report is missing in special scenarios.
                //Added email notification when exception occurs.
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[]{'testemail@163.com'};
                mail.setToAddresses(toAddresses);
                system.debug('EventName is genrated if not dml exception'+e.getStackTraceString());
                mail.setSubject('CMS_Event_PDF_Gen_Helper class threw a DMLException');
                mail.setPlainTextBody('CMS_Event_PDF_Gen_Helper class threw a DMLException: ' + e.getMessage() 
                    +' at line number ' + e.getLineNumber() + ' caused by ' + e.getCause() + ' Stacktrace check -->' +e.getStackTraceString() 
                                     );
                Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
            }
        }
    }
}

Error Email:

CMS_Event_PDF_Gen_Helper class threw a DMLException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, ??? ??? ????: [Name]: [Name] at line number 73 caused by nullStackTraceClass.CMS_Event_PDF_Gen_Helper.addPDFAttach: line 73, column 1

Thanks a lot for your help.

David
Madhukar_HeptarcMadhukar_Heptarc
Hi Songze,

Can you please try replacing if condition in above code.
 
if(mapIdtoEvent.get(eventId).CMS_Status__c.contains('Review Completed') || mapIdtoEvent.get(eventId).CMS_Status__c.contains('Planned'))
                    {
                    system.debug('EventId for Plannedevent'+eventId);
                    system.debug('EventName'+mapIdtoEvent.get(eventId).name);
                      attach.Name = mapIdtoEvent.get(eventId).name+'_'+'Planned_Report.pdf';
                      }
                    else if(mapIdtoEvent.get(eventId).CMS_Status__c.contains(('Approved'))
                      attach.Name = mapIdtoEvent.get(eventId).name+'_'+'Actual_Report.pdf';
                      attach.IsPrivate = false;
                    // attach the pdf to the event
                      attach.ParentId = eventId;
                  }

(Or)

if(mapIdtoEvent.get(eventId).CMS_Status__c == 'Review Completed' || mapIdtoEvent.get(eventId).CMS_Status__c == 'Planned')
                    {
                    system.debug('EventId for Plannedevent'+eventId);
                    system.debug('EventName'+mapIdtoEvent.get(eventId).name);
                      attach.Name = mapIdtoEvent.get(eventId).name+'_'+'Planned_Report.pdf';
                      }
                    else if(mapIdtoEvent.get(eventId).CMS_Status__c == 'Approved')
                      attach.Name = mapIdtoEvent.get(eventId).name+'_'+'Actual_Report.pdf';
                      attach.IsPrivate = false;
                    // attach the pdf to the event
                      attach.ParentId = eventId;
                  }
Please let me know if it useful / Any help required.

Thanks & Regards 
Madhukar
 
Songze Li 15Songze Li 15
Hi Madhukar,

Since the CMS_Status__c is a picklist field so we are using equals in the APEX. Could you please kindly let us know what is the reason you suggest us to change the equals to contains or == ?

Thanks

David