+ Start a Discussion
Amit Jadhav 13Amit Jadhav 13 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent]

Best Answer chosen by Amit Jadhav 13
Andrew GAndrew G
Then try:
public costsheet(ApexPages.StandardController controller) {
      OrderId = (Order)controller.getRecord().id;
    }

 

All Answers

Amit Jadhav 13Amit Jadhav 13
This Is Extension Controller
public with sharing class costsheet {
    
    public String OrderId;
    public costsheet(ApexPages.StandardController controller) {
     //OrderId= (ID)Order.getRecord().id;
     Id Order=Apexpages.currentpage().getParameters().get('id');
      
    }
    public pageReference SaveAttachment() {
        blob pdfBody;
        PageReference thePDF =  new PageReference('/apex/costsheet?id='+order.id);
        
        thePDF.setRedirect(true);
           
          if(Test.isRunningTest()) { 
             pdfBody = blob.valueOf('Unit.Test');
            } else {
             pdfBody = thePDF.getContentAsPDF();
           }
            Attachment attach = new Attachment();
            attach.body = pdfBody;
            attach.Name='CostSheet '+System.Today();
            attach.ParentID=OrderId;
            Insert attach;
            
        if (attach == null){
              ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Error will Saving a file .Please check the content of the file'));  
           }
        if (attach != null){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Quote Pdf generated successfully'));
            
          }
      PageReference qtpage = new PageReference('/' + Quote.Id);
      qtpage.setRedirect(true);
      return qtpage; 
   }


}
Andrew GAndrew G
The error message says you are missing the Parent. So this line is set incorrectly:
attach.ParentID=OrderId;
You are setting your Order ID here....but it is commented out
//OrderId= (ID)Order.getRecord().id;

 
Amit Jadhav 13Amit Jadhav 13
Error: costsheet Compile Error: Method does not exist or incorrect signature: void getRecord() from the type Order at line 5 column 25
 
Andrew GAndrew G
public costsheet(ApexPages.StandardController controller) {
      OrderId = (<myCustomObjectAPIname>)controller.getRecord().id;
    }

 
Amit Jadhav 13Amit Jadhav 13
but i use a order standard object 
 
Andrew GAndrew G
Then try:
public costsheet(ApexPages.StandardController controller) {
      OrderId = (Order)controller.getRecord().id;
    }

 
This was selected as the best answer