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
Pete RuddPete Rudd 

Reference a field on Standard Object in Javascript on VF page

I have posted this question before but didn't word it very well so didn't get the type of answer I was looking for (though some helpful person did provide an alternative solution.)

 

I have a button on Lead page layout that takes the user to a VF page. The standard controller for the VF page is Lead.

 

One of the fields on Leads is Status (which is a picklist).

 

My end goal is to test if the value of Lead Status on the current record is a certain value ("Pending") and if it is the to change it to a differnt value ("Working On") when the save button on the VF page is clicked

 

I want to know 2 things:

 

1) How do I reference the Lead Status field in my Javascript code? (I am currently trying to write an IF statement that says IF Lead Status ="Pending" change Lead Status to "Working On" but I cannot get the value from the field into my Javascript code. E.g. If I was trying to assign the Status value to a var in the code what would it look like: var currentStatus = ???)

 

2) Can I write an IF statement directly into the Apex tag? (E.g.

 

<apex:selectList value="{!Lead.Status}" >     

<apex:selectOption itemValue="IF ????

 

Thanks.

Peter

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You can reference the field by its field ID:

 

<apex:selectList id="StatusField" value="{!Lead.Status}">
   ...
</apex:selectList>

The function itself can then get the value thusly:

 

var leadStatus = document.getElementById('{!$Component.StatusField}').value

You can also assign to document.getElementById('{!$Component.StatusField}').value to set a new value.

 

The question is, though, why would you do this instead of placing it in your constructor?

 

public MyController(ApexPages.StandardController controller) {
  lead leadRecord = (lead)controller.getRecord();
  if(leadRecord.Status == 'Pending') {
    leadRecord.Status = 'Working On';
  }
}

When the user loads the page, the Lead Status will change because of the code in the constructor. No fancy JavaScript, no browser issues.

All Answers

sfdcfoxsfdcfox

You can reference the field by its field ID:

 

<apex:selectList id="StatusField" value="{!Lead.Status}">
   ...
</apex:selectList>

The function itself can then get the value thusly:

 

var leadStatus = document.getElementById('{!$Component.StatusField}').value

You can also assign to document.getElementById('{!$Component.StatusField}').value to set a new value.

 

The question is, though, why would you do this instead of placing it in your constructor?

 

public MyController(ApexPages.StandardController controller) {
  lead leadRecord = (lead)controller.getRecord();
  if(leadRecord.Status == 'Pending') {
    leadRecord.Status = 'Working On';
  }
}

When the user loads the page, the Lead Status will change because of the code in the constructor. No fancy JavaScript, no browser issues.

This was selected as the best answer
Pete RuddPete Rudd

Thanks for the response. I couldn't have hoped for a fuller / more constructive reply. I really appreciate your help.