1

I am trying to download the data which is coming from Apex Class to be download in Text format to the Page whenever on clicking a button. i.e, If any one Clicks the Quick Download Button, The Data generating on Apex Class should be download in a text.

Here is my Visualforce Page Code,

<apex:page standardController="Account" Extensions="CreateFileAndDownloadOntheFly" showChat="false" sidebar="false" showHeader="false">
    <apex:includeScript value="{!URLFOR($Resource.filesaver, 'filesaver.js')}"/>
    <script type="text/javascript">
    function myFunction(){
        var con = confirm('Please confirm to download');
        if(con){
            var recordId = '{!Account.Id}';
            FileDownload();
            var files = response;
            var blob = new Blob([files], {type: "application/text",endings: "native"});
            saveData(blob,"document.txt");
        }
        else{
            alert('You cancelled the Process');
        }
    }
    </script>
    <apex:form>
        <apex:actionFunction name="FileDownload" action="{!FileDownload}">
            <apex:param assignTo="{!pId}" name="AcctId" value="{!Account.Id}"/>
        </apex:actionFunction>
        <apex:commandButton value="Quick Download" onclick="myFunction();"/>
    </apex:form>
</apex:page>

My Problem is, I am not able to send the Account ID to the Controller for the Operation. Does anyone know how to send the Id from Javascript to Controller?

NOTE: I cannot use the ApexPages.currentPage().getParameters().get('id'); I have to use only the Javascript and need to send the ID.

Any suggestion would be great...

Thanks in advance.

0

2 Answers 2

1

I tried of using the Remote Action annotation and it is working fine.

VF Page Command Button Section :

<script type="text/javascript">
    function myFunction(accountId) 
    {
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.ApexClassController.YourMethod}',accountId,
            function(result, event)
            {
                    var con = confirm('Please confirm to download');
                    if(con){
                        var files = result;
                        var blob = new Blob([files], {type: "application/text",endings: "native"});
                        saveAs(blob, "document.txt",false);
                    }
                    else{
                        alert('You cancelled the Process');
                    }
            }, {escape:true});
    }
    </script>
    <apex:form >
        <apex:commandButton value="Quick Download" onclick="myFunction('{!Account.Id}');return false;" />
    </apex:form>

Apex Class Method :

@RemoteAction
    global static String YourMethod(Id pId) 
    {
        String blobValue = 'Your Text';
        return blobValue;
    }

Hope this might help someone too...

0

You just need to pass Account Id from javascript to action function as parameter

 var recordId = '{!Account.Id}';
 FileDownload(recordId );//Pass the Account Id 

and you action function will be like

<apex:actionFunction name="FileDownload" action="{!FileDownload}">
     <apex:param assignTo="{!pId}" name="AcctId" value=""/>
</apex:actionFunction>

Now check pId in FileDownload method


Just noticed since you are using Standard controller you can get the Account Id from standard controller

 public with sharing class CreateFileAndDownloadOntheFly
{
   private Account objAccount;

   public CreateFileAndDownloadOntheFly(ApexPages.StandardController stndCtrl)
   {
     objAccount = (Account) stndCtrl.getRecord();

     system.debug('====objAccount===='+objAccount)
   }
   public void FileDownload()
   {
       //check Id here
       system.debug('====objAccount.Id===='+objAccount.Id)
   }
}
7
  • 1) FileDownload(recordId );//Pass the Account Id ..............This is not working 2) objAccount = (Account) stndCtrl.getRecord();..................The record ID is not coming in the Method, Only coming to the Constructor. The Method itself is not firing.
    – Subhash
    May 4, 2016 at 10:30
  • @Subhash are you passing accountid in url ? add alert and check recordId null or not?
    – Ratan Paul
    May 4, 2016 at 10:31
  • Ofcourse...I am
    – Subhash
    May 4, 2016 at 10:31
  • @Subhash ahh you are getting the value in constructor then when on click of quick download call the fileDownload method ` <apex:commandButton value="Quick Download" action="{!FileDownload}"/>` and check debug
    – Ratan Paul
    May 4, 2016 at 10:34
  • Passing through the Action in CommandButton is working. But this is not my case, The Apex Class is returning the Blob result and needs to be download in text document which was i am doing through the Javascript...
    – Subhash
    May 4, 2016 at 10:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .