You need to sign in to do that
Don't have an account?
Rahul Sharma
Variable does not exists in javascript remoting method
Hello Guys,
I'm trying to use javascript remoting for my one of the requirement but facing a problem when i try to use the class variable inside the remoting method to set the value.
Code in controller i'm using is something like this:
global with sharing class MyJSController { public String strContactName { get; set; } @RemoteAction global static List<Account> getAccount(String accountName) { accountName += '%'; String str = 'select id, name, phone from Account where name like :accountName limit 1'; List<Account> lstAccount = database.query(str); if(!lstAccount.isEmpty()) { strContactName = [Select Id, LastName from Contact where AccountId =: lstAccount[0].Id limit 1].LastName; return lstAccount; } else return null; } }
JS code:
MyJSController.getAccount(buttonValue, function(result, event) { if(event.status) { alert('{!strContactName}'); } }
I just want to set a variable in the remote function which could be used in the Page.
Have anyone faced same issue or please suggest if there's any work around for this.
Thanks for you time.
AFAIK, you cannot access the properties of the class directly through javascript.
for the java remoting, the only items in the java result object are the returned results.
currently your result object will contain the Account[]
if you are trying to access the ContactName, set the return type to String and then return strContactName
alert(result);
if you need to access the account[] then you will have to return that and you could do
alert(result[0].phone); //for the first account in the list
If you need to return a combination of the items or field accessed through a relationship like a lookup, you wil have to create a wrapper class and return that......
All Answers
Declarer the variable in class as global and also write getter and setter for the variable and try it.
Regards,
Pankaj Raijade,
Thanks pankaj,
I tried giving :
but was unable to save due to error - Variable does not exist: strAccountName
Also tried the following
It got saved but still nothing is displayed (blank value) in javascript when i put it in alert.
Any other idea?
Hi
The problem is 'strContactName' null when class is initialised.
it will have value when 'getAccount' method is called. but your page will not get this new value untill the javascript portion is rerendered.
Thats why it is giving you null value.
when you call getaccount rerender the Javascript section. I think this should solve the problem
Regards,
Pankaj Raijade.
yeah that make sense, was also thinking the same.
but Actually i'm not using the value anywhere, just trying to alert.
AFAIK, you cannot access the properties of the class directly through javascript.
for the java remoting, the only items in the java result object are the returned results.
currently your result object will contain the Account[]
if you are trying to access the ContactName, set the return type to String and then return strContactName
alert(result);
if you need to access the account[] then you will have to return that and you could do
alert(result[0].phone); //for the first account in the list
If you need to return a combination of the items or field accessed through a relationship like a lookup, you wil have to create a wrapper class and return that......
thanks starz16 for the clarification.
Will try using wrapper class.
Thanks all for your time.
Thanks a lot Starz26, I'm your fan now!!
The idea of using Wrapper class worked.
Now i'm able to retrive multiple values to Javascript Remoting Function from apex Function.
My requirement was : I was calling an apex function using JS remoting from Javascript which performs http request and passes back the response to the JS function.
But, i was stuck while catching the error code and status like 503, etc.
I wanted to display appropriate message to user while any exception. so, i think same thing should work in that case.
In case any one faces the same issue, i'm posting a small demo example.
Controller:
Page: