Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I want to access a Sobject field using javascript in a visual force page and based on its value perform an action. How can I access a Sobject field using javascript?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

What sort of access do you need? You could use javascript remoting, but then if you're already working on a page that has the record loaded in the controller during construction/init you can just use regular old merge fields:

Controller

public class with sharing SomeController()
{
  public Account a {get; set;

  public SomeController()
  {
    a = [select Id, Name, Rating from Account limit 1];
  }
}

Page

<script type="text/javascript">

  var accountName = '{!a.Name}';
  var accountRating = '{!a.Rating}'

  if(accountRating == 'Hot')
    alert('HOT ACCOUNT!');

</script>

If you need the script to be a bit more dynamic (and not just setup when the page is rendered) then remoting is probably your best bet, but it's hard to suggest the best solution without more information.

share|improve this answer
    
can we access reference columns in java scripts like company__r.name? –  Pramod Kumar Jul 26 '13 at 12:33
    
That should still work with the variable assignment, though if you look at remoting it's pretty easy to get a javascript version of an SObject too. –  LaceySnr Jul 28 '13 at 4:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.