1

I am working on searching the contact by its first name. Is it Feasible to make a call to server from javascript every time when user presses a key ? Controller :

global class checkcontactduplicateandsave
{
public String contactName { get; set; }
public String contactEmail { get; set; }
public String contactPhone { get; set; }    
public static Contact[] contact { get; set; }
public checkcontactduplicateandsave()
{//default constructor}    
@RemoteAction
global static Contact[] getContact(String contactName ,String contactPhone,String contactEmail) 
{
     if((!(String.isBlank(contactName)))&&(!(String.isBlank(contactPhone)))&&(!(String.isBlank(contactEmail))))
     {
        contact=[select ID,Name,MobilePhone,Email from Contact where FirstName=: contactName OR MobilePhone=: contactPhone OR Email =: contactEmail];
     }
    return contact;
}}

VF

<script type="text/javascript">
    function getcontactJS() 
    {
        //get the values of input text and place into the variable.

        var contactNameJS = document.getElementById('conName').value;
        var contactEmailJS = document.getElementById('conEmail').value;
        var contactPhoneJS = document.getElementById('conPhone').value;
        checkcontactduplicateandsave.getContact(contactNameJS,contactPhoneJS,contactEmailJS, 
        function(result, event)
        {         
         document.getElementById('resultant').innerHTML='';         
            if (event.status) 
            {
                for(var i=0;i<result.length;i++)
                    { 
                        var acc = result[i];
                        document.getElementById('resultant').innerHTML += acc.Id;
                        document.getElementById('resultant').innerHTML += acc.Name + '<br/>';
                    }
            } 
            else if (event.type === 'exception') 
            {
                document.getElementById("errors-js").innerHTML = event.message;
            } 
            else 
            {
                document.getElementById("errors-js").innerHTML = 'No Records Found..';
            }
        }, {escape:true});
    }
    </script>
    <table>
    <tr>
    <td>Contact Name :</td>
    <td><input id="conName" type="text" onkeyup="getcontactJS();" /></td>
    </tr>
    <tr>
    <td>Contact Phone No :</td>
    <td><input id="conPhone" type="text" onkeyup="getcontactJS();" /></td>
    </tr>
    <tr>
    <td>Contact Email :</td>
    <td><input id="conEmail" type="text" onkeyup="getcontactJS();" /></td>
    </tr>    
    </table>
    <div id="errors-js"></div>
    <div id="resultant"></div>        

Is it feasible to have a server call from javascript each time ? Please Suggest any alternative.

2 Answers 2

1

I would add a timeout function to make the call every time there is a change and it has been at least 500ms between changes. Let the user type in what they want to type in, and make 1 callout to the server 500ms later. You can adjust the delay if necessary.

var timer;
var interval = 500;

function delayExecute()
{
    clearTimeout(timer);
        timer = setTimeout(
            function()
            {
                getcontactJS();
            },
        interval
    );

    return true;
}

<input id="conName" type="text" onkeyup="return delayExecute();" />
0

There is no issue of using Javascript Remoting each time user press a button. But consider few things:-

  • Make sure it is not in for loop. Also, response return are always asynchronous so, in case if user click/key-up multiple times, it will provide multiple results. Button should be disabled/delayed until response is not returned.

  • Make sure that response will be returned in javascript time-out limit.

  • Variable declaration public static Contact[] contact { get; set; } will be reset with each transaction completion.

1
  • "There is no issue of using Javascript Remoting each time user press a button" - Performance matters Oct 19, 2016 at 6:38

You must log in to answer this question.

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