+ Start a Discussion
Rahul SharmaRahul 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.

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

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

pankaj.raijadepankaj.raijade

Declarer the variable in class as global and also write getter and setter for the variable and try it.

 

Regards,

Pankaj Raijade,

Rahul SharmaRahul Sharma

Thanks pankaj,

I tried giving : 

global String strContactName { get; set; }

but was unable to save due to error - Variable does not exist: strAccountName

 

Also tried the following

public static String strAccountName { get; set; }

It got saved but still nothing is displayed (blank value) in javascript when i put it in alert.

Any other idea?

pankaj.raijadepankaj.raijade

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.

Rahul SharmaRahul Sharma

yeah that make sense, was also thinking the same.

but Actually i'm not using the value anywhere, just trying to alert.

Starz26Starz26

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......

 

 

 

 

This was selected as the best answer
Rahul SharmaRahul Sharma

thanks starz16 for the clarification.

Will try using wrapper class.

Thanks all for your time.

Rahul SharmaRahul Sharma

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:

public with sharing class RemotingWithWrapperClass
{
    @RemoteAction
    public static WrapperClass getInfo() 
    {
        return new WrapperClass('Test Account 1', 'Test Contact 1');
    }
    public class WrapperClass
    {
        String strAccountName 
        {   get;set;    }
        String strContactName 
        {   get;set;    }
        public WrapperClass(String strAName, String strCName)
        {
            strAccountName = strAName;
            strContactName = strCName;
        }
    }
}

 Page:

<apex:page controller="RemotingWithWrapperClass">
<apex:form >
    <script type="text/javascript">
        function getAccountJS() 
        {
            RemotingWithWrapperClass.getInfo(function(result, event)
            {
                if(event.status)
                {
                    alert(result.strAccountName + ' : ' + result.strContactName);
                }
            }, {escape:true});
        } 
    </script>
    <apex:outputText value="Click on the button to get the value : "></apex:outputText>
    <apex:commandButton value="Get Values" onclick="getAccountJS(); return false;"/>
</apex:form>
</apex:page>