Is it possible to define a temporary varible on the form?

gee
Tera Guru

Hi Team,

I am looking for defining a calculated field on the form, not displaying a field from the base table. An example would be calculating the contract during in months. Contract starts and ends dates are in the contract table. Calculated duration should be displayed on the contract form below the 'Ends' field as 'Duration (in Months): 24 months'. Location of this field may not be an issue.

Is this is possible / doable in Fuji release? I appreciate you comments and sample codes if this is doable.

Many thanks & Regards,

Gee

1 ACCEPTED SOLUTION

brianiac
Giga Contributor

You could create a UI Macro and put it on your page after creating a Formatter for it.   With the UI Macro you can do your GR queries/Javascript calculations, then just add the HTML for how you want to present it.   Here's an example adding the date that the current logged in user's account was created, somewhere on a Change Record.   After creating the UI Macro and the Formatter, just add the Formatter to your page via the Configure->Form Layout.



UI Macro:


Name: u_sample_macro


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


<html>


  <head>


  <script>


  function doStuff(){


        var gr = new GlideRecord('sys_user');


        gr.get(g_user.userID);


        // Do math here if you want to put some figured amount in the HTML of your page.


        gel('placeholder').innerHTML = "Your account was created on: <b>" + gr.sys_created_on + "</b>";


  }


  </script>


  </head>


  <body onload="doStuff()">


          <div id="placeholder">


          </div>


  </body>


</html>



</j:jelly>



UI Formatter:


Name: Sample Formatter


Formatter: u_sample_macro


Table: Change Request [change_request]


View solution in original post

3 REPLIES 3

Uncle Rob
Kilo Patron

There's not really a capability for a throw away field like that.   You can build calculations that are either strict database calcs, or strings/numbers that you update via business rule (but are still part of the table).



If its FYI only and you really don't want it stored, you could possibly do a display Business Rule that fires up an addInfoMessage (appears in the header) that contains the results of hte calculation, which would be performed in the BR


brianiac
Giga Contributor

You could create a UI Macro and put it on your page after creating a Formatter for it.   With the UI Macro you can do your GR queries/Javascript calculations, then just add the HTML for how you want to present it.   Here's an example adding the date that the current logged in user's account was created, somewhere on a Change Record.   After creating the UI Macro and the Formatter, just add the Formatter to your page via the Configure->Form Layout.



UI Macro:


Name: u_sample_macro


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


<html>


  <head>


  <script>


  function doStuff(){


        var gr = new GlideRecord('sys_user');


        gr.get(g_user.userID);


        // Do math here if you want to put some figured amount in the HTML of your page.


        gel('placeholder').innerHTML = "Your account was created on: <b>" + gr.sys_created_on + "</b>";


  }


  </script>


  </head>


  <body onload="doStuff()">


          <div id="placeholder">


          </div>


  </body>


</html>



</j:jelly>



UI Formatter:


Name: Sample Formatter


Formatter: u_sample_macro


Table: Change Request [change_request]


Hi Brian,


Many thanks for quick response and correct solution. The given code is very useful.




Best Regards,


Gee