0

I Have a Apex class:

global with sharing class AccountRemoter {



    public String accountName { get; set; }

    public static Account account { get; set; }

    public AccountRemoter() { } // empty constructor



    @RemoteAction

    global static list<Account> getAccount(String accountName) {

        list<account> account1 = [SELECT Id, Name, Phone, Type, NumberOfEmployees

                   FROM Account WHERE Name = :accountName];

        return account1;

    }

}

Visual Force Page:

<apex:page controller="AccountRemoter">

    <script type="text/javascript">

    function getRemoteAccount() {

        var accountName = document.getElementById('acctSearch').value;



        Visualforce.remoting.Manager.invokeAction(

            '{!$RemoteAction.AccountRemoter.getAccount}',

            accountName,

            function(result, event){

                if (event.status) {

                    // Get DOM IDs for HTML and Visualforce elements like this

                    document.getElementById('remoteAcctId').innerHTML = result.Id

                    document.getElementById(

                        "{!$Component.block.blockSection.secondItem.acctNumEmployees}"

                        ).innerHTML = result.NumberOfEmployees;

                } else if (event.type === 'exception') {

                    document.getElementById("responseErrors").innerHTML =

                        event.message + "<br/>\n<pre>" + event.where + "</pre>";

                } else {

                    document.getElementById("responseErrors").innerHTML = event.message;

                }

            },

            {escape: true}

        );
    }

    </script>



    <input id="acctSearch" type="text"/>

    <button onclick="getRemoteAccount()">Get Account</button>

    <div id="responseErrors"></div>



    <apex:pageBlock id="block">

        <apex:pageBlockSection id="blockSection" columns="2">

            <apex:pageBlockSectionItem id="firstItem">

                <span id="remoteAcctId"/>

            </apex:pageBlockSectionItem>

            <apex:pageBlockSectionItem id="secondItem">

                <apex:outputText id="acctNumEmployees"/>

            </apex:pageBlockSectionItem>

        </apex:pageBlockSection>

    </apex:pageBlock>

</apex:page>

Output for the Above Code:

enter image description here

But the output is Showing is undefined and undefined?when entering the account in the search account textbox? why? I want perfect output and why we have to use @Remote action annotation in the Apex class? what is the use?Please anyone guide me for the Answer

1
1

Your Apex code return a list of Account objects not a single Account object so you need to handle the result value accordingly:

if (event.status) {
    for (var i = 0; i < result.length; i++) {
        var account = result[i];
        var id = account.Id;
        var n = result.NumberOfEmployees;
        // You will need to create e.g. a table here
        console.log('id=' + id + ' n=' + n);
    }
}

You get undefined when you try to access a field of a JavaScript object that does not exist.

Note that the debug tools in browser's are excellent and a small investment in time learning about them will quickly pay back.

Have you considered not using JavaScript here? You can write a Visualforce page that has no JavaScript that does the same thing with much less code.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.