+ Start a Discussion
AkkiAkki 

Upload File using Customer Portal

I'm currently designing a new customer portal and was wondering if it's possible to allow the user to upload a Document.

It would be easier if the user just get an inputFile option and the file that he selects is mailed to another person. I've got the apex and visualforce code for that setup but the file contained in the email is 0KB and there's nothing in the file.

Here's a part of my visualforce code

<apex:outputPanel id="MigrateBlock">
<apex:pageBlock title="Migrate Block" rendered="{!RenderMigrateType}">
<apex:pageBlockButtons >
<apex:commandButton action="{!SaveMigrate}" value="Migrate License"/>
</apex:pageBlockButtons>
<apex:pageBlockSection showHeader="false" columns="2" id="block76">

<apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!document.name}" id="fileName"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!document.body}" filename="{!document.name}" id="file"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="New Host IP Address" for="adasds"/>
          <apex:inputText value="{!HostIpAddress}" id="adasds"/>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>



And here's the Apex Code

public Document document
   {
   get{
   if (document == null)
        document = new Document();
   return document;
   }
   set;
}

public PageReference SaveMigrate()
{
document.FolderId = UserInfo.getUserId();
       insert document;

      document.body = null;
       document = new Document();
  if(HostIPAddress != null)
  {
  ContractAsset__c n = [SELECT id, Contract__r.id FROM ContractAsset__c WHERE Asset__r.Product2Code__c =: selectedAssets.Product2Code__c AND Contract__r.ContractNumber = :contractNumber LIMIT 1];
  Asset a = [SELECT Id from Asset WHERE Product2Code__c =: selectedAssets.Product2Code__c LIMIT 1];
  LicenseKey__c newData = new LicenseKey__c(ContractAsset__c = n.id,
  Ip_Address__c = HostIpAddress,
  LicenseType__c = 'Migrate',
  asset__c = a.id,
  Contract__c = n.Contract__r.id);
  sendEmail(); // Sends the email with a setDocumentAttachment
  insert newData;
  }

return null;

}

It's all for the Customer Portal. Any ideas how to mail the file without uploading it onto salesforce before?

Best Answer chosen by Admin (Salesforce Developers) 
sravusravu

Make the following changes in the code and see:


Visualforce Page:

 

<apex:outputPanel id="MigrateBlock">
<apex:pageBlock title="Migrate Block" rendered="{!RenderMigrateType}">
<apex:pageBlockButtons >
<apex:commandButton action="{!SaveMigrate}" value="Migrate License"/>
</apex:pageBlockButtons>
<apex:pageBlockSection showHeader="false" columns="2" id="block76">
<apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!document.name}" id="fileName"/>
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!filebody}" filename="{!filename}" id="file"/>
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem >
          <apex:outputLabel value="New Host IP Address" for="adasds"/>
          <apex:inputText value="{!HostIpAddress}" id="adasds"/>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>

 

Apex Class:

 

public transient Blob filebody{get;set;}

public String filename{get;set;}

public PageReference SaveMigrate()
{

Document doc = new Document();

doc.FolderId = UserInfo.getUserId();

doc.Name=filename;

doc.body=filebody;

insert doc; //Insert all the required fields

  if(HostIPAddress != null)
  {
  ContractAsset__c n = [SELECT id, Contract__r.id FROM ContractAsset__c WHERE Asset__r.Product2Code__c =: selectedAssets.Product2Code__c AND Contract__r.ContractNumber = :contractNumber LIMIT 1];
  Asset a = [SELECT Id from Asset WHERE Product2Code__c =: selectedAssets.Product2Code__c LIMIT 1];
  LicenseKey__c newData = new LicenseKey__c(ContractAsset__c = n.id,
  Ip_Address__c = HostIpAddress,
  LicenseType__c = 'Migrate',
  asset__c = a.id,
  Contract__c = n.Contract__r.id);
  sendEmail(); // Sends the email with a setDocumentAttachment
  insert newData;
  }
 return null;
}

 

Please let me know if you face any difficulty....

All Answers

dmchengdmcheng

I assume you've checked CRUD permissions in the portal user profile for the Document object?  Is the document actually created in the Salesforce folder?

sravusravu

I can see two possibilities to resolve your issue,

 

1. For the customer portal profile check CRUD operation so that they get access to the documents object

2. You are accepting the file using inputfile tag but in the controller you are attaching that file to the document. When you upload the file try to create a document and attach the file

 

 

Attachment attachmentTarget = new Attachment();  //create a new attachment

AkkiAkki

Access to the document object has already been granted in the profile. But we can only enable read access for Documents 

 

And I've tried using attachments. but if I use that then the body value is saved as blob under Attachment.body. And in this case aswell, the file thats being mailed is 0KB. Can you give me an example (Some code) of what you mean by create a document and attach the file.

 

Thanks

sravusravu

Make the following changes in the code and see:


Visualforce Page:

 

<apex:outputPanel id="MigrateBlock">
<apex:pageBlock title="Migrate Block" rendered="{!RenderMigrateType}">
<apex:pageBlockButtons >
<apex:commandButton action="{!SaveMigrate}" value="Migrate License"/>
</apex:pageBlockButtons>
<apex:pageBlockSection showHeader="false" columns="2" id="block76">
<apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!document.name}" id="fileName"/>
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!filebody}" filename="{!filename}" id="file"/>
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem >
          <apex:outputLabel value="New Host IP Address" for="adasds"/>
          <apex:inputText value="{!HostIpAddress}" id="adasds"/>
        </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>

 

Apex Class:

 

public transient Blob filebody{get;set;}

public String filename{get;set;}

public PageReference SaveMigrate()
{

Document doc = new Document();

doc.FolderId = UserInfo.getUserId();

doc.Name=filename;

doc.body=filebody;

insert doc; //Insert all the required fields

  if(HostIPAddress != null)
  {
  ContractAsset__c n = [SELECT id, Contract__r.id FROM ContractAsset__c WHERE Asset__r.Product2Code__c =: selectedAssets.Product2Code__c AND Contract__r.ContractNumber = :contractNumber LIMIT 1];
  Asset a = [SELECT Id from Asset WHERE Product2Code__c =: selectedAssets.Product2Code__c LIMIT 1];
  LicenseKey__c newData = new LicenseKey__c(ContractAsset__c = n.id,
  Ip_Address__c = HostIpAddress,
  LicenseType__c = 'Migrate',
  asset__c = a.id,
  Contract__c = n.Contract__r.id);
  sendEmail(); // Sends the email with a setDocumentAttachment
  insert newData;
  }
 return null;
}

 

Please let me know if you face any difficulty....

This was selected as the best answer
AkkiAkki

Thanks for the help. It seems to be working fine now.

NK@BITNK@BIT
https://nitinkhunalsalesforce.wordpress.com/2021/03/28/salesforce-upload-files-to-azure-portal/