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
Deepak ChouhanDeepak Chouhan 

MS Doc attachement not working on save operation

I am getting an error on VF page. An error is System.VisualforceException: Subscript is invalid because list is empty which is occurred in saveCAR method on Blob docBlob = docPage.getContent(); I have created two VF page one for saving record and another VF page is generate a ms doc file (it's work fine when I pass the accountId and carId in URL or when I define static accountID and carId in saveCAR Method).

<apex:page standardController="CAR__c" extensions="CAReportDocAE" contentType="application/msWord#CAReport.doc" cache="true" standardStylesheets="false" showheader="false"> <html> <head> <style> @page Section1{ size:8.5in 11in; mso-page-orientation:portrait; margin:.5in .5in .5in .5in ; mso-header-margin:1.0in; mso-footer-margin:.5in; } body { font-family: "Segoe UI",Arial,sans-serif; } .heading{ background-color:#2c5ea2; color:#fff; padding: 10px 5px 10px 5px; } .tableBorder{ border:1px solid black;border-collapse:collapse; width:100%; } .textAlign{ text-align:justify; } .pagebreak { display:block; page-break-after:always; margin-top:50px; } </style> </head> <div> <div> <p class="heading">INSTRUCTION</p> <ol> <li>Fill out CA, current Date, and Assignee Name.</li> <li>Fill out event description and client involved. Additional comments should be included as necessary to aid in the resolution of the issue.</li> </ol> </div> <div style="background-color:#ccc;"> <table class="tableBorder"> <tr class="tableBorder"> <td>CAPA #: {!newCarList[0].CA__c}</td> </tr> <tr class="tableBorder"> <td>Date: {!newCarList[0].Date__c}</td> </tr> <tr class="tableBorder"> <td>Assignee Name:{!newCarList[0].Assignee_Name__c}</td> </tr> <tr> <td> <tr><td>Event Description: </td></tr> <tr><td>What Found:{!newCarList[0].WFound__c}</td></tr> <tr><td>When {!newCarList[0].When__c}</td></tr> <tr><td>Found: Who {!newCarList[0].FWho__c}</td></tr> </td> </tr> </table> </div> <div> <p class="heading">INSTRUCTION</p> <ol> <li><p class="textAlign">Complete containment actions within 24 hours of receipt of this CAR.</p></li> </ol> </div> <br clear="all" style="mso-special-character:line-break;page-break-before:always;"/> <div class="pagebreak"> <table class="tableBorder"> <tr> <td> <tr> <td> Assigned Team Members: </td> </tr> </td> </tr> <tr class="tableBorder"> <td>Additional Comments:</td> </tr> </table> </div> <div style="margin-top:50px;"> <table class="tableBorder" style="margin-bottom: -1px;"> <tr class="tableBorder" style="background-color:#ccc;"> <td><i >*The following are approved changes incorporated into the revision numbers indicated </i></td> </tr> </table> <table class="tableBorder"> <tr class="tableBorder"> <td class="tableBorder" style="width:10%;"><b>Revision</b></td> <td class="tableBorder"><b>Description of Change</b></td> </tr> <tr class="tableBorder"> <td class="tableBorder" style="width:10%;">HI</td> <td class="tableBorder" >Hi</td> </tr> <tr class="tableBorder"> <td class="tableBorder" style="width:10%;">HI</td> <td class="tableBorder">HI</td> </tr> </table> </div> </div> </html> </apex:page>

public with sharing class CAReportDocAE{ public List<CAR__c> newCarList {get;set;} public String accountId {get;set;} public String carId {get;set;} public CAReportDocAE(ApexPages.StandardController controller) { newCarList = new List<CAR__c>(); accountId = ApexPages.currentPage().getParameters().get('aid'); carId = ApexPages.currentPage().getParameters().get('cid'); Initalization(); } public void Initalization(){ newCarList = [Select CA__c,Date__c,Assignee_Name__c,WFound__c,When__c,FWho__c from CAR__c where id=:carId and Account__c=:accountId limit 1]; } }

<apex:page standardController="CAR__c" extensions="CAReportAE" > <apex:form id="IdFrm" > <apex:sectionHeader title="CAR Edit" subtitle="New CAR"/> <apex:panelGroup ><apex:pageMessages id="error"/></apex:panelGroup> <apex:pageBlock id="IdPBlock" title="CAR Edit" mode="edit"> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!saveCAR}"/> <apex:commandButton value="Cancel" action="{!cancelMetod}" immediate="true" /> </apex:pageBlockButtons> <apex:pageBlockSection id="IdPBSection" title="Information" columns="1"> <apex:pageBlockSectionItem > <apex:outputText value="Account"/> <apex:inputField value="{!newCar.Account__c}" required="true"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputText value="CAPA #"/> <apex:inputField value="{!newCar.CA__c}" required="true"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputText value="Date"/> <apex:inputField value="{!newCar.Date__c}" required="true"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputText value="Assignee Name"/> <apex:inputField value="{!newCar.Assignee_Name__c}" required="true"/> </apex:pageBlockSectionItem> </apex:pageBlockSection> <apex:pageBlockSection title="Event Description"> <apex:pageBlockSectionItem > <apex:outputText value="What Found"/> <apex:inputField value="{!newCar.WFound__c}" style="width:80%;"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputText value="When"/> <apex:inputField value="{!newCar.When__c}" style="width:80%;"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputText value="Found Who"/> <apex:inputField value="{!newCar.FWho__c}" style="width:80%;"/> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

public with sharing class CAReportAE { public CAR__c newCar {get;set;} public String accountId {get;set;} public String carId {get;set;} public CAReportAE(ApexPages.StandardController controller) { newCar = new CAR__c(); accountId = ApexPages.currentPage().getParameters().get('aid'); newCar.Account__c=accountId; } public PageReference saveCAR(){ if(accountId!=null){ try{ insert newCar; carId = newCar.Id; attachDOC(); return new PageReference('/' + accountId); }catch(DMLException e){ ApexPages.addMessages(e); ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Due to miscellaneous reasons the save operation failed.'); ApexPages.addMessage(msg); return null; } }else{ ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Warning, 'Please select at Account.'); ApexPages.addMessage(msg); } return null; } public PageReference cancelMetod() { return new PageReference('/' + accountId); } public void attachDOC(){ PageReference docPage = Page.CAReportDoc; docPage.getParameters().put('cid',carId); docPage.getParameters().put('aid',accountId); Blob docBlob = docPage.getContent(); Attachment attachDoc = new Attachment(parentId = accountId, Name = 'CAR.doc', body = docBlob); insert attachDoc; } }