REQUIRED_FIELD_MISSING,缺少必填字段:[Parent]:[Parent]

时间:2017-10-13 10:07:16

标签: pdf salesforce attachment visualforce apex

我正在尝试保存附件。附件已保存,但每当我打开VF页时,它都会出现以下错误:

插入失败。第0行的第一个例外;第一个错误:REQUIRED_FIELD_MISSING,缺少必填字段:[Parent]:[Parent]

我有两个VFPages这样做。一个是以PDF格式呈现的VFPage,另一个是保存附件和加载呈现为PDF的VFPage的参考。这里有没有人知道如何解决这个错误?

这是VFPage供参考            

这是Apex Classes:

public class attachPDFDocument{

public String parentId{get;set;}
public String curPage = ApexPages.currentPage().getParameters().get('id');

public String vrsn = 'AttachmentV';
public Integer vrsion;

public String attName {get;set;}
public String attNames {get;set;}
public String attNameSubString;
public String attNameSubStrings;
public Integer attCount;
public Integer attachCount;   

public attachPDFDocument(ApexPages.StandardController controller) {    

}

public PageReference pdfLoad(){   
      List<Attachment> pdfAttach = new List <Attachment>();
      PageReference pdf = Page.PDFAttachment;   

      List<Attachment> att = [SELECT Id, Name FROM Attachment where ParentId = :curPage Order by Name DESC]; //getting all attachments  

      //String attachName = att.Name;


      if(att.size() <= 0){             
          vrsion = 1;
      }else if(att.size() <= 9){

          Attachment attachmentPDF = att[0];
          attName = attachmentPDF.Name;  // yung latest Version
          attNameSubString = attName.subString(35,36);
          vrsion = Integer.valueOf(attNameSubString) + 1;     
      }else if(att.size() > 9){                                   

          List<Attachment> attachPDFDoc = [SELECT Id, Name FROM Attachment where ParentId = :curPage AND Name LIKE '___________' ORDER BY Name DESC LIMIT 1]; //getting all attachments with length is 41                  
          Attachment docpdf= attachPDFDoc[0];
          String pdfdoc = docpdf.Name;                
          attNameSubString = pdfdoc.subString(35,37);            
          vrsion = Integer.valueOf(attNameSubString) + 1; 
       }else if(att.size() > 99){                                   

          List<Attachment> attachPDFDoc = [SELECT Id, Name FROM Attachment where ParentId = :curPage AND Name LIKE '___________' ORDER BY Name DESC LIMIT 1]; //getting all attachments with length is 41                  
          Attachment docpdf= attachPDFDoc[0];
          String pdfdoc = docpdf.Name;                
          attNameSubString = pdfdoc.subString(35,38);            
          vrsion = Integer.valueOf(attNameSubString) + 1; 
       }

      Attachment attach = new Attachment();
      Blob body;

      try {
          body= pdf.getContent();            
      } catch (VisualforceException e) {
          body= Blob.valueOf(e.getMessage());
      } 

      attach.Body = body ;           
      attach.IsPrivate = false;        
      attach.ContentType = '/application/pdf';
      attach.ParentId = ApexPages.currentPage().getParameters().get('id');
      attach.Name = vrsn + vrsion + '.pdf';

      insert attach;
      return pdf;           
    }

}

1 个答案:

答案 0 :(得分:0)

你调试过这行ApexPages.currentPage()。getParameters()。get('id');看到它返回一个实际的ID?检查attach.parentid是否为空。