+ Start a Discussion

How to create a new record while Attachment with record name as an Attached file name??



I need to create a new record in the related list.. but that record should get created only after i attached a file to it...


i mean... in order to create the record in the related list first i'll upload the file.. and with the uploaded file name will become the record name... and the new record will be created...


if i am not able to explain..well..  please let me know...


You can only really do that with a Visualforce page.  You can write a page to take an attachment, and create the object and the attachment at the same time (although you'll have to create the object first and insert it so that you get its ID to attach the object to, but from the user's perspective they'll be created at the same time).


Hello Sir,


I shall really be thankful to you... please provide me the code to achieve this....




Currently i am using.. the following


Visualforce Page Code:


<apex:page standardController="opportunity_proposals__c" extensions="PropUpload">
<apex:form >
  <apex:pageMessages />
     <apex:pageblock >
    <div id="upload" class="upload">
      <td><apex:inputFile value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/></td> 
      <td><apex:commandButton value="Upload Attachment" action="{!uploadFile}"  styleClass="upload-button"/></td>



And the Following Apex Code:


public class PropUpload  
    public Id recId  
    {    get;set;    }  
    public PropUpload(ApexPages.StandardController ctlr)  
       recId = ctlr.getRecord().Id;       
    public string fileName   
    {    get;set;    }  
    public Blob fileBody   
    {    get;set;    }  
    public PageReference UploadFile()  
        PageReference pr;  
        if(fileBody != null && fileName != null)  
          Attachment myAttachment  = new Attachment();  
          myAttachment.Body = fileBody;  
          myAttachment.Name = fileName;  
          myAttachment.ParentId = recId;  
          insert myAttachment;  
           //PageReference page = ApexPages.currentPage();
           //return page;

        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));
              return null;