Refresh time before editing record with current time using the now() method.

  • 0
  • 1
  • Question
  • Updated 6 months ago
  • Answered
Can anyone help me with refreshing the time before editing a record via url formula with current time being posted to end date/time field.

Here is my current code:

If(IsNull([End Date/Time]
),
var text URL =
URLRoot()
& "db/" & Dbid() & "?a
=API_EditRecord&apptoken
=[token]\n &rid=" &
[Record ID#] &
"&_fid_19="&Now();
"javascript:" &
"location.reload(true);"
&
"$.get('" &
$URL &
"',function(){" &
"location.reload(true);"
&
"});"
& "void(0);",
"javascript:alert('You
marked this as ended
working already.')")

The problem is that if the user is currently on the page a while it will not update the current time instead it will execute with original time that the record was viewed.

Thanks
Photo of Micro Dev

Micro Dev

  • 834 Points 500 badge 2x thumb

Posted 6 months ago

  • 0
  • 1
Photo of Matthew Neil

Matthew Neil

  • 31,438 Points 20k badge 2x thumb
You either have to force them to refresh the page manually prior, or use a code page to 'get' the time and then write the record.
Photo of QuickBaseCoach App Dev./Training

QuickBaseCoach App Dev./Training, Champion

  • 51,256 Points 50k badge 2x thumb
This question has been asked many times in this forum and has never really had a satisfactory answer.

But here is a low tech solution..

Instead of directly updating the date/time, use an Action or an Automation to set the current time. So the edit of a record will request that the date time be updated, rather than directly updating it.
Photo of Micro Dev

Micro Dev

  • 834 Points 500 badge 2x thumb
How about adding a get line of code to get the time from any given date time field in any table which I can set a formula field to be now().

Can you help me with this line of code to get the date time from that field?

Thanks
Photo of Micro Dev

Micro Dev

  • 834 Points 500 badge 2x thumb
Thanks for your reply.
Photo of Jason

Jason

  • 1,012 Points 1k badge 2x thumb
You can do 2 of the following as well.

1. Put the word "today" in the date time field as below:
& "&_fid_645="& URLEncode("today")

2. Or you can use separate tables and take advantage of the Date Created field Kurt Trachy has an app showing how to do this. The app is ABC Tracking In/Out Time

Either way is much better than the dysfunctional Now(). Depending on the use case we alternate between the 2 methods .
Photo of Micro Dev

Micro Dev

  • 834 Points 500 badge 2x thumb
Today won't help me since I need the time as well...
Photo of Micro Dev

Micro Dev

  • 834 Points 500 badge 2x thumb
Thanks for your reply.
Photo of Jason

Jason

  • 1,012 Points 1k badge 2x thumb
Today does put the time in as well when used like I showed.
Photo of Scott

Scott

  • 314 Points 250 badge 2x thumb
You could always have your API call redirect to ANOTHER API call that then modifies the end time with Now().  That way, no matter how long someone is on the page, the Now() is always called on the second half and is always "up to date".

//First section of your API call that changes record values n such, Now() is not included in this part..
URLRoot()&"db/"&Dbid()&"?a=API_EditRecord&rid="&[Record ID#]&"&apptoken=xxxxxxx&<DO YOUR STUFF HERE FIRST>
//Second section which executes the NOW() function, which has been "refreshed" in a second api call.
&"&rdr="&URLRoot()&"db/"&Dbid()&"?a=API_EditRecord&rid="&[Record ID#]&"&apptoken=xxxxxxx&_fid_19=Now()
Photo of QuickBaseCoach App Dev./Training

QuickBaseCoach App Dev./Training, Champion

  • 51,036 Points 50k badge 2x thumb
Scott, that won't work.
  
If you did that formula and exposed the code, you would see that it is all resolved into a text string URL and the second Now() is immediately populated.

I'm still liking the Action method, myself.  Trigger an Action to do the update if you really need the Now() time.
Photo of Scott

Scott

  • 314 Points 250 badge 2x thumb
Hmm actually you're right.  My mistake.  

Perhaps you could redirect to a code page that takes the rid url param, as well as dbid and use some javascript to run a call after the fact with the current time.

I think that should work, I'll see if I can setup a demo. 
Photo of Scott

Scott

  • 314 Points 250 badge 2x thumb
Alright, so to start this method off...

You will need the following code page:

<HTML>
<HEAD>
<title>Refresh Time</title>
<script type="text/javascript">

var urlParam = function(name, w){
    w = w || window;
    var rx = new RegExp('[\&|\?]'+name+'=([^\&\#]+)'),
        val = w.location.search.match(rx);
    return !val ? '':val[1];
}

var dbid = urlParam('dbid');
var rid = urlParam('rid');
var fid = urlParam('fid');
var token = "YOUR TOKEN HERE";
var base_url = window.location.origin;
var date = new Date;



function refresh(){
     window.location.replace(base_url + "/db/" + dbid + "?a=API_EditRecord&rid=" + rid + "&apptoken=" + token + "&_fid_" + fid + "=" + date);
}


</script>


</HEAD>

<BODY>

	<script>refresh()</script>

</BODY>
</HTML>

Lastly, you will need to format your INITIAL API call to redirect to this page. 

As said in a previous post, it'll be something like this:
URLRoot()&"db/"&Dbid()&"?a=API_EditRecord&rid="&[Record ID#]&"&apptoken=xxxxxxx&<DO YOUR STUFF HERE FIRST>&"&rdr=<LINK TO NEW .HTML CODE PAGE WITH SCRIPT>&rid="&[Record ID]&"dbid="&Dbid()&"fid=<YOUR FID HERE>&rdr="&<YOUR FINAL REDIRECT LINK>

After you sift through and correct the inevitable syntax errors, and probably adjust the Javascript Date function to output correctly for QB formatting, this should solve your issue. 

Scott
(Edited)
Photo of Scott

Scott

  • 314 Points 250 badge 2x thumb
That said, the action trigger method is much simpler.  :^)

EDIT: And you can actually skip the JS date function, and just use "=Now()" at the end of the redirect in the code page instead of the "=" + date piece.  Derp.
(Edited)
Photo of Jason

Jason

  • 1,012 Points 1k badge 2x thumb
Did you try the following? We encountered this over a year ago with a Check-In and Out button and went through many complex solutions. Finally to our surprise putting in the word 'today' worked and we still use it today (pun intended). That reminds me to fix a button post where I was using that pesky Now().....thanks for that.


"&_fid_19="& URLEncode("today");
(Edited)
Photo of Micro Dev

Micro Dev

  • 834 Points 500 badge 2x thumb
This worked like a charm. Thanks so much.
Photo of QuickBaseCoach App Dev./Training

QuickBaseCoach App Dev./Training, Champion

  • 51,256 Points 50k badge 2x thumb
Jason,
Wow, that is a pretty obscure Easter egg.  I can't imagine how you found that out that works other than perhaps dumb luck and bad syntax which happened to work.
Photo of Jason

Jason

  • 1,012 Points 1k badge 2x thumb
Actually it wasn't dumb luck I talked with Jeff Jones who was at the time Quick Base support and he provided the solution. We have used it thousands of times since thanks to Jeff Jones.
Photo of Micro Dev

Micro Dev

  • 834 Points 500 badge 2x thumb
Thanks to everyone for replying. I really appreciate it.