Skip to main content
added 524 characters in body
Source Link
Himanshu
  • 337
  • 3
  • 8

Yes, you can attach create documents and attachments using VF page. Sample VF page for inserting Attachment.

<apex:page controller="UploadAttchCtr">
apex:form apex:pageBlock

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

  <apex:pageBlockSection columns="2">

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File Name"/>
      <apex:inputText value="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File"/>
      <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

  </apex:pageBlockSection>
</apex:pageBlock>

</apex:form> </apex:page>

Sample Controller class

public class UploadAttchCtr {

    public Attachment attachment {
        get;
        set;
    }
    public UploadAttchCtr() {
        attachment = new Attachment();
    }

    public PageReferencevoid upload() {
        attachment.OwnerId = UserInfo.getUserId();
        attachment.ParentId = '';'0069000000BNuBc'; //specify the record ID;
        attachment.IsPrivate = true;
 
     
    try {
            insert attachment;
        } catch (DMLException e) {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error uploading attachment'));
        //return null;
   return null;}
    
    Document document = new Document();
    document.AuthorId = UserInfo.getUserId();
    document.FolderId = UserInfo.getUserId(); // put it in running user's folder
    document.Body = attachment.body;
    document.Name = attachment.Name;        
    try {
      insert document;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
    }
}
    
}

Yes, you can attach create documents and attachments using VF page. Sample VF page for inserting Attachment.

<apex:page controller="UploadAttchCtr">
apex:form apex:pageBlock

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

  <apex:pageBlockSection columns="2">

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File Name"/>
      <apex:inputText value="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File"/>
      <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

  </apex:pageBlockSection>
</apex:pageBlock>

</apex:form> </apex:page>

Sample Controller class

public class UploadAttchCtr {

    public Attachment attachment {
        get;
        set;
    }
    public UploadAttchCtr() {
        attachment = new Attachment();
    }

    public PageReference upload() {
        attachment.OwnerId = UserInfo.getUserId();
        attachment.ParentId = ''; //specify the record ID;
        attachment.IsPrivate = true;
 
        try {
            insert attachment;
        } catch (DMLException e) {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error uploading attachment'));
            return null;
        }
    }
}

Yes, you can attach create documents and attachments using VF page. Sample VF page for inserting Attachment.

<apex:page controller="UploadAttchCtr">
apex:form apex:pageBlock

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

  <apex:pageBlockSection columns="2">

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File Name"/>
      <apex:inputText value="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File"/>
      <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

  </apex:pageBlockSection>
</apex:pageBlock>

</apex:form> </apex:page>

Sample Controller class

public class UploadAttchCtr {

    public Attachment attachment {
        get;
        set;
    }
    public UploadAttchCtr() {
        attachment = new Attachment();
    }

    public void upload() {
    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = '0069000000BNuBc'; //specify the record ID;
    attachment.IsPrivate = true;
     
    try {
        insert attachment;
    } catch (DMLException e) {
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error uploading attachment'));
        //return null;
    }
    
    Document document = new Document();
    document.AuthorId = UserInfo.getUserId();
    document.FolderId = UserInfo.getUserId(); // put it in running user's folder
    document.Body = attachment.body;
    document.Name = attachment.Name;        
    try {
      insert document;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
    }
}
    
}

Yes, you can attach create documents and attachments using VF page. Sample VF page for inserting Attachment.

<apex:page controller="UploadAttchCtr">
apex:form apex:pageBlock

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

  <apex:pageBlockSection columns="2">

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File Name"/>
      <apex:inputText value="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File"/>
      <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

  </apex:pageBlockSection>
</apex:pageBlock>

</apex:form> </apex:page>

Sample Controller class

public class UploadAttchCtr {

public Attachment attachment {get; set;} public UploadAttchCtr(){ attachment = new Attachment(); }

public PageReference upload() { attachment.OwnerId = UserInfo.getUserId(); attachment.ParentId = ''; //specify the record ID; attachment.IsPrivate = true;

try { insert attachment; } catch (DMLException e) { ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error uploading attachment')); return null; } } }

public class UploadAttchCtr {

    public Attachment attachment {
        get;
        set;
    }
    public UploadAttchCtr() {
        attachment = new Attachment();
    }

    public PageReference upload() {
        attachment.OwnerId = UserInfo.getUserId();
        attachment.ParentId = ''; //specify the record ID;
        attachment.IsPrivate = true;

        try {
            insert attachment;
        } catch (DMLException e) {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error uploading attachment'));
            return null;
        }
    }
}

Yes, you can attach create documents and attachments using VF page. Sample VF page for inserting Attachment.

<apex:page controller="UploadAttchCtr">
apex:form apex:pageBlock

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

  <apex:pageBlockSection columns="2">

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File Name"/>
      <apex:inputText value="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File"/>
      <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

  </apex:pageBlockSection>
</apex:pageBlock>

</apex:form> </apex:page>

Sample Controller class

public class UploadAttchCtr {

public Attachment attachment {get; set;} public UploadAttchCtr(){ attachment = new Attachment(); }

public PageReference upload() { attachment.OwnerId = UserInfo.getUserId(); attachment.ParentId = ''; //specify the record ID; attachment.IsPrivate = true;

try { insert attachment; } catch (DMLException e) { ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error uploading attachment')); return null; } } }

Yes, you can attach create documents and attachments using VF page. Sample VF page for inserting Attachment.

<apex:page controller="UploadAttchCtr">
apex:form apex:pageBlock

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

  <apex:pageBlockSection columns="2">

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File Name"/>
      <apex:inputText value="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File"/>
      <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

  </apex:pageBlockSection>
</apex:pageBlock>

</apex:form> </apex:page>

Sample Controller class

public class UploadAttchCtr {

    public Attachment attachment {
        get;
        set;
    }
    public UploadAttchCtr() {
        attachment = new Attachment();
    }

    public PageReference upload() {
        attachment.OwnerId = UserInfo.getUserId();
        attachment.ParentId = ''; //specify the record ID;
        attachment.IsPrivate = true;

        try {
            insert attachment;
        } catch (DMLException e) {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error uploading attachment'));
            return null;
        }
    }
}
Source Link
Himanshu
  • 337
  • 3
  • 8

Yes, you can attach create documents and attachments using VF page. Sample VF page for inserting Attachment.

<apex:page controller="UploadAttchCtr">
apex:form apex:pageBlock

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

  <apex:pageBlockSection columns="2">

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File Name"/>
      <apex:inputText value="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

    <apex:pageBlockSectionItem >
      <apex:outputLabel value="File"/>
      <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}"/>
    </apex:pageBlockSectionItem>

  </apex:pageBlockSection>
</apex:pageBlock>

</apex:form> </apex:page>

Sample Controller class

public class UploadAttchCtr {

public Attachment attachment {get; set;} public UploadAttchCtr(){ attachment = new Attachment(); }

public PageReference upload() { attachment.OwnerId = UserInfo.getUserId(); attachment.ParentId = ''; //specify the record ID; attachment.IsPrivate = true;

try { insert attachment; } catch (DMLException e) { ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error uploading attachment')); return null; } } }