function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pooja biswaspooja biswas 

calling webservice using javascript remoting

Hello
I am trying to understand how to call a webservic eusing javascript remoting.
I have attached the code but after trying a lot it is not gving the desired result.
any help will be greatly appreciated.
global class remotingexample
{
    public String accountName { get; set; }
    public static Account account { get; set; }
    public remotingexample() { }
   
    @RemoteAction
    webservice static Account getAccount(String accountName)
    {
        account = [select id, name from Account where name=:accountName ];
        return account;
    }
}

<apex:page controller="remotingexample">
    <script type="text/javascript">
    function getAccountJS()
    {
        var accountNameJS = document.getElementById('accName').value;       
        sample.getAccount( accountNameJS,
        function(result, event)
        {
            if (event.status)
            {
                // demonstrates how to get ID for HTML and Visualforce tags
                document.getElementById("{!$Component.theBlock.thePageBlockSection.theFirstItem.accId}").innerHTML = result.Id;
                document.getElementById("{!$Component.theBlock.thePageBlockSection.theSecondItem.accNam}").innerHTML = result.Name;
            }
            else if (event.type === 'exception')
            {
                document.getElementById("errors-js").innerHTML = event.message;
            } else
            {
                document.getElementById("errors-js").innerHTML = event.message;
            }
        }, {escape:true});
    }
    </script>
    Account Name :<input id="accName" type="text" />
    <button onclick="getAccountJS()">Get Account</button>
    <div id="errors-js"> </div>
    <apex:pageBlock id="theBlock">
        <apex:pageBlockSection id="thePageBlockSection" columns="2">
            <apex:pageBlockSectionItem id="theFirstItem">
                <apex:outputText id="accId"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem id="theSecondItem" >
                <apex:outputText id="accNam" />
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Thanks
Pooja
Akshay Deshmukh1Akshay Deshmukh1
hi,

try using this when you get result.  "result.getArray(“records”);". You get associative array. You can not just use result.Id etc.

try to debug in console.log(); and let me know if it helps.