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
ssfdcssfdc 

How to access class variables from a Java RemoteMethod

Maybe someone can can help me out with this logic to access variables from outside of a remote Method

 

Say I have my controller as follows Sudo code,

 

global with sharing class myController {

 

   public Map<String, String> myMap {get; set;}

 

   public myController() {

      Populate myMap with values

   }

 

   @RemoteAction

   global Static String returnStrValue(String keyStr) {

   

      String value = myMap.get(keyStr);

      return value;

   }

 

}

 

 

// NOW on my VF page

 

I would call upon my javaremote method to return a map value when a given key is supplied which i have in my javascript

The problem that im having and i know is that the variables created in my controller are not accesible from my remote method, im thinking maybe querying the MAP in my VF and assigning to a JS varaible then passing it back into my remote method so my remote method would look like this.

 

I havent got this way working yet as i dont think the MAP assisngs correctly to the JS variable, is there a better way to do this?  Thanks

 

@RemoteAction

   global Static String returnStrValue(String keyStr, Map<String, String>) {

   

      String value = myMap.get(keyStr);

      return value;

   }

 

}

 

 

 

 

 

Rahul SharmaRahul Sharma

Hey ssfdc,

 

You could pass your map to a javascript function(suppose, remoting method is inside that function), then from that remoting method pass the map and the value to apex remote method. By this you don't have to additionally create the map again.

 

// Page 

<apex:page controller="AccountRemoter">
    <script type="text/javascript">
    function getRemoteAccount(value, mapToBePassed) {
		// Remoting Method
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.AccountRemoter.getAccount}',
            value,
			mapToBePassed,
            function(result, event){
                // Processing
            }, 
            {escape: true}
        );
    }
    </script>

    <apex:inputText id="acctSearch"/>
    <apex:commandButton value="Get Account" onclick="getRemoteAccount(document.getElementById('{!$Component.acctSearch}', '{!myMap}'))"></button>
    </apex:pageBlock>
</apex:page>

// Class
global with sharing class myController {
 
   public Map<String, String> myMap {get; set;}
 
   public myController() {
      // Populate myMap with values
   }
 
   @RemoteAction
   global Static String returnStrValue(String keyStr, Map<String, String> yourMap) {
   
      String value = myMap.get(keyStr);
      return value;
   }
 
}

 I'm not sure if you were referring to the same thing.