Jump to content

Reference a calculated field in js function


Recommended Posts

I need to reference a calculated field's value for use in an If/Then function in js. The calculated field uses a SELECT to calculate a View's total records, which is either null or with some records in the result. If the calculated field has a null value I need something to happen vs if it's not null. Here's the code: 

<script>
   var v_itemsid = parseFloat(document.getElementById("EditRecordcalcfield18").value);

if(!isNaN(v_itemsid))
{
  document.getElementById('section1').style.display = "block";
  document.getElementById('section2').style.display = "none";
}
else
{
  document.getElementById('section1').style.display = "none";
  document.getElementById('section2').style.display = "block";
}
</script>

I've used this code before with a regular table field: how is the calculated field referenced where EditRecord is (EditRecordcalcfield18 obviously isn't right)? I don't know the syntax. Any help would be great- thanks in advance. 

Link to post
Share on other sites
  • 1 year later...

Hi Greg,

To do this use a Virtual field to get the calculated field's value. Set the Virtual field to Hidden and use the Default Value to get the calculated field's value. 

Then in the footer use this script:

<script>
  var v_virta = document.getElementById("cbParamVirtualField").value;

if(v_virta=="whatevervalue")
{
  document.getElementById('section1').style.display = "block";
}
else
{
  document.getElementById('section1').style.display = "none";
}
</script>

The "v_virta" is getting the value of the virtual field and making it a variable so the js can use it. The 'if' section uses the variable (that comes from the virtual field, that itself comes from the calculated field) to decide what to do based on the value. 

Link to post
Share on other sites
  • 7 months later...

If I understand this correctly, I cannot reliably use Formula fields in a script at the end of a form.  Indeed, it does not seem to work for my case, so I tried what you describe here and I still can't get it to work.   

My form is a Report form with a Details page.  I have several Formula Fields set up in a table.  Once the user submits the form, the Formula Fields get recalculated and are in the table nicely.  The form has Ajax loading enabled, and the wizard is set to "Display Search Form Above the Results".  In the JS at the end of this form, I want to sum up several of the Formula Fields, store the sum and do some other operations with the sum.

I've tried setting up Virtual fields for each of the Formula fields, but I do not seem to get the Virtual values into the script.  

Example Details Page Setup:

Formula field called ADJ set to Display Only and do not pass field value as a parameter [@ADJ]   (note: I did try passing it directly, that doesn't work either)
Virtual field called Virtual1 set to Hidden, set to receive value of ADJ on load, and pass field value as a parameter [@Virtual1]

Here's a stub of the footer JS

<script type="text/javascript">
document.addEventListener('BeforeFormSubmit', function(event) 
{
var v1 = document.getElementById("EditRecordVirtual1").value;
do some math with v1...
// end of function
});

</script>

I'm not getting the v1 value, the script stops/fails at that line.

Thanks for any advice!

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...