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
DannyK89DannyK89 

Apex Method called remote from Javascript problem

Hi everyone,

 

So I am getting the hand of apex methods being called remote from javascript.

Of course I am having some issues. I have put a temparary method in my apex class that I want to be called from some javascript in the visual force page.

However it seems that I have a bug somewhere because the javascript does not run. 

I have been looking at this code for i'm guessing a couple of hours and can't see what is wrong. 

Maybe someone on here can give me a hand.

 

Visualforce Javascript / code:

 

<apex:page controller="AMManagementController">
<script>
        function switchMenu(obj,obj1,obj2) 
        {
            var el = document.getElementById(obj);                                       
            if ( el.style.display != 'none' ) {
            el.style.display = 'none';
            }
            else {
            el.style.display = '';
            }
            var e2 = document.getElementById(obj1);                                       
            if ( e2.style.display != 'none' ) {
            e2.style.display = 'none';
            }
            else {
            e2.style.display = '';
            }
             var e3 = document.getElementById(obj2);                                       
            if ( e2.style.display != 'none' ) {
            e3.style.display = 'none';
            }
            else {
            e3.style.display = '';
            }

        }
        
        function CheckUpdate(){
            AMManagementController.UpdateCheck('Update',function(result, event){
                if(result == false)
                    alert('Are you sure you want to do this?');
            },{escape: true});
        }
</script>
<apex:form >
<apex:pageBlock tabStyle="Account" title="Requirement Management">
    <apex:pageBlockButtons >
        <apex:commandButton value="Back" onclick="CheckUpdate();"/>

 

Apex method trying to call:

 

global class AMManagementController {

public AMManagementController(){}

@RemoteAction
    global static boolean UpdateCheck(String test){
        return true;
    }

}

 

sandeep@Salesforcesandeep@Salesforce

We can call Controller's method from Javascript using

1. apex::Action function

2. using RemoteAction  keyword

Scott.MScott.M

It looks like your remote function UpdateCheck always returns true and your javascript alert will only get called if result is false. I assume the javascritp alert  is what you're expecting to run?

 

If you're using chrome option + cmd + i will bring up the developer console in the bottom. If you click on network and then click on xhr(at the bottom to filter only xhr requests), refresh the page and click your action button. You should be able to see the ajax request and response which is useful for debugging javascript remoting :) 

 

Hope that helps!

 

Cheers,

Scott

DannyK89DannyK89

Sorry for taking so long to respond. Work is getting busy around here.

 

So I did what you said but I have never really used the Developer console so I don't know what I am looking for.

 

Also I updated my code so that the apex class sends out "true" as a string.

Here is my updated javascript code:

 

When the window pops up it looks like tempRes is "undefined".

Does that mean that my class is not being called for some reason?

 

function CheckUpdate(){
            var tempRes;
            AMManagementController.UpdateCheck('Update', function(result, event){
                tempRes = result;
            },{escape: true});
                alert('Are you sure you want to do this? ' + tempRes);
        }

 

Suman KuchSuman Kuch
Hi DannyK89,
Did you fix your problem? Im getting simillar, I don't see error but not invoking controller from javascript.