0

Can you please help me with apex variable usage in javascript function.

function viewattachment(expId,contTypeattch,attname){
 var  def = '{!expBlob[expId]}';
}

expBlob is a Map[Id,Blob]
In the above function, I would like to query this map expBlob of the controller giving the expId. It is taking expId literally and not substituting the actual value of expId from javascript.

3

1 Answer 1

3

That's because '{!expBlob[expId]}' is evaluated at page creation. Instead you have two choices :

Either you Json.serialize your map in your controller

public String expBlobSerialized { get { return Json.serialize(expBlob).escapeEcmaScript(); }}

and then in your page :

<script type="text/javascript">
    var expBlob = {!expBlobSerialized};
    function viewattachment(expId,contTypeattch,attname){
       var  def = expBlob['expId'];
    }
</script>

Or if your map is too big or you don't want to expose all data in the page, make a webservice and use ajax to retrieve the value you need.

1
  • Thanks, all. It is great to know how JSON is being used to consider the object as key,value and retrieve the value. Thanks for the help. Commented Sep 27, 2017 at 9:20

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.