+ Start a Discussion

Apex method from javascript not working

I have a custom Borrow, Item and Employee object. The following code has the purpose of taking in the autonumber Employee ID and string Barcode ID from the user and as soon as user presses enter key, it should save the record in the Borrow database. I tested the controller code seprately in the execute anonymous code wiindow, it is able to create the record in the database. And the visualforce page is running fine too, as its displaying the barcodeId in the output text field 
<apex:outputText id="out" value="{!barcodeId}"></apex:outputText> 
once user inputs the Barcode ID in the text field and presses the enter key. If the apex method was not called properly then this output text field would have not populated as it is getting populated from {!barcodeId} (apex controller field). But its giving me an query exception.
System.QueryException: List has no rows for assignment to SObject
Error is in expression '{!createBorrowRecord}' in page checkoutitems: Class.BorrowController.createBorrowRecord: line 9, column 1
Class.BorrowController.createBorrowRecord: line 9, column 1

which is the query for retrieving employee record.
I'm unsure where can the problem be ? 
Your help will be highly appreciated.

public class BorrowController {
    public String barcodeId {get; set;}
    public String employeeId {get; set;}
    public void createBorrowRecord() {
        try {
            Item__c item = [SELECT Id, Item_ID__c, Barcode_ID__c FROM Item__c WHERE Barcode_ID__c =:barcodeId LIMIT 1];
            Employee__c emp = [SELECT Id, Name, Employee_ID__c FROM Employee__c WHERE Employee_ID__c =:employeeId LIMIT 1];
            Borrow__c b = new Borrow__c();
            b.Name = item.Name + 'Borrow';
            b.Item_Name__c = item.Id;
            b.Employee_Name__c = emp.Id;
            insert b;
        catch(DmlException e) {
            System.debug('The following error has occured'+ e.getMessage());

Visualforce Page
<apex:page controller="BorrowController" showHeader="false" applyHtmlTag="false" applyBodyTag="false" docType="Html-5.0">  
    <apex:form >
        <apex:actionFunction action="{!createBorrowRecord}" name="createBorrow" reRender="out">
            <apex:param name="barcode" value="" assignTo="{!barcodeId}"/>
            <apex:param name="employee" value="" assignTo="{!employeeId}"/>
    <apex:outputText id="out" value="{!barcodeId}"></apex:outputText>
    <apex:outputText id="out1" value="{!employeeId}"></apex:outputText>
            <title>Checkout Items</title>
            <br />
            <label for = "employeeId">Employee ID: </label>
            <input type = "text" id = "employeeId" />
            <br />
            <br />
            <label for = "barcodeText">Enter Barcode ID: </label>
            <input type = "text" id = "barcodeText" />
            <br />
            <span id = "addedItems"/>
            <script type = "text/javascript">
                var barcodeText = document.getElementById("barcodeText");
                var empId = document.getElementById("employeeId")
                var items = document.getElementById("addedItems");
                var completeText = "";
                barcodeText.addEventListener("keydown", function(e) {
                    if(e.which != 13) {
                        if(e.which >= 48 && e.which <= 57 || e.which >= 65 && e.which <= 90) {
                        console.log("Inside If-Block");
                        completeText = completeText + e.key;   
                        else {
                        console.log("Invalid Key");
                        outputText.innerHTML = "Invalid Key Pressed"; 
                        barcodeText.value = "";
                    else {
                        console.log("Inside Else-Block");
                        items.innerHTML = completeText;
                        console.log("Calling Apex Method:");
                        createBorrow(completeText, empId);
                        console.log("Done calling apex method:");
                        barcodeText.value = "";
                        completeText = "";

Foram Rana RForam Rana R
Hi GGill,

var empId = document.getElementById("employeeId");
Can you please put alert('@@@empId  = '+empId );  and check wheather you get the value that you pass ?
Let me know.

Foram Rana