1

this is my first try at a VF Remoting call with a controller, and i'm getting a standard VF error when using the call. My setup is as follows:

This is the VF page code:

    <apex:page showHeader="false" sidebar="false" standardController="Contact" extensions="WS_getRemoteLeadStatus">
<script src="{!URLFOR($Resource.jquery19validate, 'jquery.min.js')}"/>
<script src="{!URLFOR($Resource.jquery19validate, 'jquery.validate.js')}"/>
<script type="text/javascript">
var j$ = jQuery.noConflict();

j$(document).ready(function(){


j$.urlParam = function(name){
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results==null){
       return null;
    }
    else{
       return results[1] || 0;
    }
};

var usrid = j$.urlParam('usrid');




function getStatus(usrid)
{


    Visualforce.remoting.timeout = 120000; // Set timeout at page level
    var result = $Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.WS_getRemoteLeadStatus.getLeadStatus}', usrid,HandleResult);    //calling the remote action & 
                                                                                             //getting the vals
                                                                                              // for inserting into the DOM
        function HandleResult(result, event)
        {
            if (event.status) 
            {
            document.getElementById("amount").textContent = result;
            }
            else if (event.type === 'exception') 
            {
            document.getElementById("exception") = 
            event.message + "<br/>\n<pre>" + event.where + "</pre>";
            } 
            else 
            {
            document.getElementById("exception").textContent = event.message;
            }
        }

};
});

</script>
<apex:form>
<div id="black">
<div id="amount" style="align:left;">Amount:  </div><div id="until" style="align:right;" >Valid: </div><div id="exception">
Errors: </div>
<button onclick="getStatus();">Getstatus</button>
</div>
</apex:form>
</apex:page>

And this is the controller code:

global class WS_getRemoteLeadStatus {

    private final ApexPages.StandardController controller;
    public WS_GoMobileStatus(ApexPages.StandardController controller){
    this.controller = controller;
    }

    webservice String usrid { get; private set; }

    public WS_getRemoteLeadStatus() {
    kennitala = ApexPages.currentPage().getParameters().get('usrid');
    }
    @RemoteAction
    global static String GetLeadStatus(String usrid){

        String endp = 'https://ENDPOINT_URL/getLead?action=GetRemoteLeadStatus&usrid=';


         HttpRequest req = new HttpRequest();
         req.setMethod('GET');
         req.setHeader('Content-Type','application/json; charset=utf-8');
         req.setHeader('User-Agent', 'force.com');
         req.setEndpoint(endp+=usrid);

         Http http = new Http();
         HTTPResponse res = new HTTPResponse();

          try {

        //Execute web service call here     
        res = http.send(req);   



        //Helpful debug messages
        System.debug(res.toString());
        System.debug('STATUS:'+res.getStatus());
        System.debug('STATUS_CODE:'+res.getStatusCode());

} catch(System.CalloutException e) {
    //Exception handling goes here....
}       


        return res.toString();


    }
}

i'm using Jquery 1.9 and the first Javascript function is supposed to get the current parameters from the page, like you do with

String var = ApexPages.currentPage().getParameters().get('var');

but it isn't working. So i created a custom button on the Contact layout and set a usrid parameter from a custom field on the Contact object. The Apex code for the LeadStatus web service works fine when called via Anonymous Apex but whenever I call it via the custom button it throws a "Formula Expression is required on the action attributes. " error. As far as i can see, this has got something to do with PageReference - my idea is to use VF remoting to fetch a lead Status value and an dynamically update the HTML. is that possible using VF Remoting & Jquery (or other such DOM manipulation)?

1 Answer 1

1

The apex:commandButton component is designed to render HTML buttons that make calls to an Apex Controller only, e.g. to call Apex code not JavaScript code. I am actually suprised it let you save your Visualforce page, having tested it myself it appears it does, anyway, you get the exception because the value you've given in the 'action' attribute does not follow the merge-field syntax.

The action method invoked by the AJAX request to the server. Use merge-field syntax to reference the method. For example, action="{!save}" references the save method in the controller.

You need to use a regular HTML button element to render your own button and use the onclick attribute to call your JavaScript function getStatus. Take a closer look at this topic in the Visualforce Developers Guide on JavaScript Remoting, where this example is shown.

 <button onclick="getStatus();">Get Lead Status</button>
7
  • thanks for that input Andrew, of course the error was because of the apex:commandButton. I'll keep trying this with the regular button.
    – akarnid
    Commented Dec 3, 2013 at 21:45
  • Your welcome! Glad this was able to help you out, if it ultimately happens to resolve your issue please indicate its been helpful with an upvote and/or a tick for the correct answer. It not only puts a smile on my face, but more importantly helps future readers know it resolve the problem! :-) Commented Dec 3, 2013 at 22:07
  • I'm wondering about this REST callout service - When I test the endpoint using a tool like Advanced REST Client I get a response from the endpoint. But I'm actually getting nothing at all from the service when I've implemented it via the Apex class. Am I doing something wrong when working with the response? One method of doing it should be instantiating a String var, and then assign the stringified value of the response to that variable. But as far as I can see it's not working. I see when I invoke it via Anonymous Apex that the only thing the response contains are the status code(200 OK)
    – akarnid
    Commented Dec 6, 2013 at 23:42
  • Well this is not really a Apex REST service, its a JavaScript Remoting service for use explicitly through the JS remoting proxies injected into the page. As regards Apex REST services, thats a different question you might want to raise here with more detail, but basically yeah if your method returns a String this forms the response (wrapped in a JSON response) or if you return an object this is serialised into a JSON string or you can control the response directly, there is more in the Apex Developers guide on Apex REST services. Commented Dec 7, 2013 at 20:29
  • Yah, I figured out that problem. I guess that my problem is now just related to Javascript syntax, since my html/js code is failing and the JS error console in my Chrome throws a ReferenceError: Can't find variable: getStatus . But as far as I can see it's already declared correctly as inside a <script> tag.
    – akarnid
    Commented Dec 8, 2013 at 21:55

You must log in to answer this question.

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