[SalesForce] Action Function not working

I have to upload a file using HTML input file and create a case and attach it to file.

I am ablt to acomplish this using Javascript. Now I want to send the case Id to the controller. But I am not able to do so.

PFB the code for reference. Where Am I going wrong ?

<apex:page standardController="Case" extensions="InputfileUploadCont">
<script type="text/javascript">
    var __sfdcSessionId = '{!GETSESSIONID()}';

  function uploadFile()
  {       
    var input = document.getElementById('file-input');
  //  var parentId = "500g0000004UEb2" ;
    var caseId;
    var filesToUpload = input.files;

    for(var i = 0, f; f = filesToUpload[i]; i++)
    {
        var reader = new FileReader();     

        // Keep a reference to the File in the FileReader so it can be accessed in callbacks
        reader.file = f; 

        reader.onerror = function(e) 
        {
            switch(e.target.error.code) 
            {
                case e.target.error.NOT_FOUND_ERR:
                    alert('File Not Found!');
                    break;
                case e.target.error.NOT_READABLE_ERR:
                    alert('File is not readable');
                    break;
                case e.target.error.ABORT_ERR:
                    break; // noop
                default:
                    alert('An error occurred reading this file.');
            };
        };     

        reader.onabort = function(e) 
        {
            alert('File read cancelled');
        };

        reader.onload = function(e) 
        {
           var newRecords = [];

            var newCase = new sforce.SObject("Case");
            newCase.Case_Language__c = "Russian";
            newCase.subject = document.getElementById("subject");
            newRecords.push(newCase);
            var resultCase = sforce.connection.create(newRecords);
            if(resultCase [0].getBoolean("success")){
                   alert('case created' +  resultCase[0].id);

            }else{
                   alert('Could not create record '+result);
            }

            var att = new sforce.SObject("Attachment");
            att.Name = this.file.name;
            att.ContentType = this.file.type;
            att.ParentId =  resultCase[0].id;

            att.Body = (new sforce.Base64Binary(e.target.result)).toString();

            sforce.connection.create([att],
            {
                onSuccess : function(result, source) 
                {
                    if (result[0].getBoolean("success")) 
                    {
                        alert('Done : ' + resultCase[0].id);
                        updateCase(resultCase[0].Id);
                        console.log("new attachment created with id " + result[0].id); 
                    } 
                    else 
                    {
                        console.log("failed to create attachment " + result[0]);
                    }
                }, 
                onFailure : function(error, source) 
                {
                    console.log("An error has occurred " + error);
                }
            });
        };

        reader.readAsBinaryString(f);



    }
}
</script>
<script src="/soap/ajax/29.0/connection.js" type="text/javascript"></script>

<apex:form>
  <input id="file-input" type="file" name="file"/>
        <input type="button" value="Upload" onclick="uploadFile();"/>

      <apex:actionFunction name="updateCase" action="{!updateCase}" >
            <apex:param name="scaseId" value="" />
        </apex:actionFunction>



<b><apex:outputLabel value="{!val}" /></b>



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


public with sharing class InputfileUploadCont{

    public InputfileUploadCont(ApexPages.StandardController controller) {

    }

    public String val{get;set;}

    public void updateCase()
    {
     val = 'CAse ID  :  - '+Apexpages.currentPage().getParameters().get('scaseId');
    }
}

Best Answer

You're not assigning the scaseId to anything. Try adding the assignTo attribute to your apex:param element and then adding a variable in your controller to handle the case Id.

For instance your apex:actionFunction would look like the following:

<apex:actionFunction name="updateCase" action="{!updateCase}" >
    <apex:param name="scaseId" assignTo="{!updatedCaseId}" value="" />
</apex:actionFunction>

And your controller would look like the following:

public with sharing class InputfileUploadCont{

    public Id updatedCaseId {get; set;}
    public String val{get;set;}

    public InputfileUploadCont(ApexPages.StandardController controller) {}

    public void updateCase(){
        val = 'Case ID  :  - ' + updatedCaseId;
    }
}
Related Topic