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
sarahdevsarahdev 

Attachments

I have a new requirement from the business that I am trying to meet.  They want to require an attachement on a custom module form before it gets submitted for an approval process.  They also would like the file name of the attachement to start with a particular naming convention.  Has anyone does this? 

Sonam_SFDCSonam_SFDC

Hi Sarah,

 

I understand that you wish to customize the notes and attachments related list (as in the attachment part of this piece) 

This related list is non customizable as can be seen from the page layout of any Object record where this list is available .

 

However, you can work with the Attachment Object(http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_attachment.htm) and create your own VF page to upload attachments to a record(put validations for name etc through code) 

Read the following blog to see how it can be done:

http://developer.force.com/cookbook/recipe/uploading-a-document-using-visualforce-and-a-custom-controller

 

Once you have the VF page working you can then place this VF page on the Object page layout so that users can use this piece to upload attachments.

sarahdevsarahdev

Thank you so much for this start.  I tried to create a button that would launch this VF page but the VF is not showing up as a page to select.  I am not sure if it is because i have 0 code coverage on the controller or not.  I am really new to this VF stuff and this is actually my first one. 

 

I have a few questions. 

 

I also what the file name to always be the same, how do I hard code that?

I also want to pull the ID of the custom module that this attachment is being added to, how do i do that?

I also want to only allow .xls or .xlsx files to be added, how would i do that?

Could i potentially create a checkbox for them to select if the file they just uploaded is a BTN List that way i don't have to hard code the file name and this VF can be used for more than this type of file upload and can replace the attachements section altogether.

 

Here is the code that i am using modified of course from your code:

Controller:

public with sharing class FileUploadController {
  public Attachment attachment {
    get {
      if (attachment == null)
        attachment = new Attachment();
       return attachment;
    }
set;
  }
public PageReference upload() {
attachment.OwnerId = UserInfo.getUserId();

try
{
insert attachment;
    } catch (DMLException e) {
     ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
     return null;
    } finally {
     attachment.body = null; // clears the viewstate
      attachment = new Attachment();
    }
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));
return null;
  }
}

 

VF Page:

<apex:page controller="FileUploadController">

  <apex:sectionHeader title="Add BTN List" subtitle="BTN List File Upload"/>

  <apex:form enctype="multipart/form-data">

    <apex:pageMessages />

    <apex:pageBlock title="Upload a File">

      <apex:pageBlockButtons >

        <apex:commandButton action="{!upload}" value="Save"/>

      </apex:pageBlockButtons>

      <apex:pageBlockSection showHeader="false" columns="2" id="block1">

        <apex:pageBlockSectionItem >

          <apex:outputLabel value="File Name" for="fileName"/>

          <apex:inputText value="{!Attachment.name}" id="fileName"/>

          </apex:pageBlockSectionItem>
         
          <apex:pageBlockSectionItem >

          <apex:outputLabel value="Pricing Request ID" for="ParentId"/>

          <apex:inputText value="{!Attachment.ParentId}" id="ParentId"/>

          </apex:pageBlockSectionItem>
         
        <apex:pageBlockSectionItem >

          <apex:outputLabel value="File" for="file"/>

          <apex:inputFile value="{!Attachment.body}" filename="{!attachment.name}" id="file"/>

        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >

          <apex:outputLabel value="Description" for="description"/>

          <apex:inputTextarea value="{!attachment.description}" id="description"/>

        </apex:pageBlockSectionItem>

        </apex:pageBlockSection>

    </apex:pageBlock>

  </apex:form>

</apex:page>