0

Very new VisualForce and Apex user here.

Currently working on a Visualforce page javascript function in which I'd like to update fields on a custom object based on certain conditions.

Each custom object is linked to a Case object. On the controller the object is defined public Custom_Object_Name custom1{get;set;} and is queried based on Case.Id. A new custom object is created if none are found.

The Visualforce page has apex:outputField commands to update fields on custom1 via apex:outputField value="{!custom1.field_Name__c}. Separately, my javascript function is supposed to update a Datetime field when a picklist value is changed. My onchange call to this function works fine, but I don't know how to access the fields for alteration in the function.

I thought it would be something like custom1.Datetime_field__c = current_Date_variable; but it throws the exception "custom1 is not defined" What's the correct way to do this?

Picture: enter image description here

  • 1
    can you post your code? – Arabinda Sep 11 '19 at 23:54
  • The file is pretty long so I only added a picture of the most relevant snippets. – hohos6 Sep 12 '19 at 0:27
0

Your on-page JavaScript does not have direct access to the member variables of your page controller.

You may populate values from the controller to your JavaScript using expression syntax, {! someValue }. However, expressions are essentially just text substitution performed by the Visualforce runtime: they aren't live references to the page's view state (controller data), and you can't mutate the source values in JavaScript.

To perform updates to the view state from JavaScript, you'd need to use an <apex:actionFunction> to create a JavaScript function that executes a server action, thereby updating the view state and optionally rerendering some or all of your page.

Alternately, you may want to just get a reference to a specific component on your page (such as an input bound to your custom field) using $Component and set its value on the frontend using JavaScript. Then, the next Apex action that's executed by the page will update the view state with that and all other user-initiated changes.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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