Edit a record and redirect to javascript?

  • 0
  • 1
  • Question
  • Updated 3 years ago
  • Answered

Does anyone know a syntax that will maker this work

var text UpdateUser = 

URLRoot() & "db/" & [_DBID_USER_FOCUS] 

& "?act=api_editrecord&key=" & ToText([Current User])

& "&_fid_36=" & [Record ID#]; 


var text DuplicateWordOrder = 

"javascript:void(copyMasterDetailButtonHandler('&relfids=41&recurse=false&copyFid=654&destrid=0&sourcerid=" & [Record ID#] & "', 'bkbhbmcg8'))";


$UpdateUser

& "&rdr=" & URLEncode($DuplicateWordOrder )


My problem is that I have a need to redirect to that javascript code and the  URL formula instead returns the XML success message for the 1st step to the screen but does not do the Redirect.


 

Photo of QuickBaseCoach App Dev./Training (Mark Shnier)

Posted 3 years ago

  • 0
  • 1
Photo of Xavier Fan

Xavier Fan, Champion

  • 720 Points 500 badge 2x thumb
I wonder if you can do a version of the solution from here:

https://quickbase-community.intuit.com/questions/1121235-javascript-refresh-and-position-to-a-spot-o...


So use Javascript to call both - with the copyMasterDetailButtonHandler being called after the UpdateUser is complete.

Note that I changed DuplicateWordOrder to be just the straight JavaScript call, without the "javascript:void" etc. in front.

Also - I changed the URL to be the Javascript "$.get" call.



var text UpdateUser = 

URLRoot() & "db/" & [_DBID_USER_FOCUS] 


& "?act=api_editrecord&key=" & ToText([Current User])


& "&_fid_36=" & [Record ID#]; 


var text DuplicateWordOrder = 


"copyMasterDetailButtonHandler('&relfids=41&recurse=false&copyFid=654&destrid=0&sourcerid=" & [Record ID#] & "', 'bkbhbmcg8');";





"javascript:" &


"$.get('" & 

$UpdateUser & 

"',function(){" &

$DuplicateWordOrder &

"});" 

& "void(0);"

(Edited to fix single / double quote typos)
Xavier, there were no syntax errors, but when I click the button nothing happens.
Photo of Xavier Fan

Xavier Fan, Champion

  • 720 Points 500 badge 2x thumb
Hmm - looking at it again - I think there are probably Javascript syntax errors in the DuplicateWordOrder section - because of the mix of single and double quotes.  Let me replicate the structure to do some testing.
... much appreciated.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,224 Points 20k badge 2x thumb
>Edit a record and redirect to javascript?

It doesn't make any sense to "redirect to javascript"


The function copyMasterDetailButtonHandler() is a JavaScript function that QuickBase has included into the source of some of their pages. It appears you want to first call the API method API_EditRecord and then follow that by calling copyMasterDetailButtonHandler(). You are only asking for trouble trying to pack these two actions into a QuickBase formula as it will be difficult to get the syntax correct, difficult to debug logical problems and severely limit how errors or edge cases can be handled.


In general you want to get out of the formula language as quickly as possible and implement everything in JavaScript. So you should be calling $.getScript() and placing all your JavaScript in a user defined page. You may think you need to use the formula language to "pass parameters" to the JavaScript stored in the user defined page. This isn't true either. Since the JavaScript becomes part of the current page, it can detect which event (say a button click) was used to invoked the script in the first place and it can determine any additional information needed including the implied parameters.

I know this isn't a compile solution but I have only a few minutes before my QuickBase Category Theory Hangout starts so I will come back later and show you how to do this task.
Photo of Xavier Fan

Xavier Fan, Champion

  • 720 Points 500 badge 2x thumb
Please try it again - I edited the answer.  I had extra single quotes in the "function()" section.
Xavier, yes in indeed that worked just fine.  Very useful.

Would you know how this would extend to a general case, say for example to simply string three typical URL formuals together in a row to be executed successively?  Or to execute two of them in a row and then that last copymasterdetail script, which I gather is differently formatted in the syntax.

If you don't get a chance to test that I will play around with some of my own guesses.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,224 Points 20k badge 2x thumb
Mark,

I have a simple solution but would like to make my answer unambiguous. You identify one table by  [_DBID_USER_FOCUS] and a second table by a literal dbid (bkbhbmcg8). What is the name of the second table?
Thx Dan
The second table is [_DBID_SPRAY_WO]
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,224 Points 20k badge 2x thumb
Untested but this is what I would do:

Pastie Database
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=481

>Would you know how this would extend to a general case, say for example to simply string three typical URL formuals together in a row to be executed successively? 


Once you are in JavaScript land you can string together as many actions as you want without any special handling.

Also I don't understand why anyone would use the synchronous function copyMasterDetailButtonHandler instead of the asynchronous API method API_CopyMasterDetail. You will not reliably be able to chain additional behavior onto the function call because you will not be able to reliably detect when the function completes. Well that previous statement may not be entirely true  because the underlying jax call QuickBase uses may be synchronous (see jax.DoSyncCmd()). In general all AJAX calls should be asynchronous as a best practice.

FWIW, to see the source of copyMasterDetailButtonHandler use this console command:

 console.log(copyMasterDetailButtonHandler.toString());

Then jsbeautify the console output:

http://jsbeautifier.org/
The reason I was trying to use the JavaScript method to do the copy master detail instead of the API method, was that the API method was running into what I believe is a QuickBase bug that is currently being research by QuickBase support.  QuickBase was reporting that I had a looping relationship and the copy master detail API was failing, but surprisingly the exact same copy master detail done through the JavaScript that the wizard creates worked just fine.

I do find that I frequently need to string successive URL API calls together.    Typically, I use a technique of nesting the URLs successively with successively deeper URL encoding.

 But this does tend to create very long URLs which at some point will probably blow up a browser, particularly IE which has a lower character limit on the maximum length of the URL.

 So I was trying to understand if there is an alternative method of stringing successive of URL API calls together. For the time being we can leave out the special case where JavaScript is required like that JavaScript copy master detail button. That is a very unusual case that I was trying to use that simply because I was having to work around a QuickBase.

 So to continue this dialogue, what I'm hoping is that there is a way to specify a series of URL APIs as formula variables. And then have generic way of stringing them together right in the formula builder box itself.    

 The scary part about going off in writing code in the cold page, is that it just seems to me that there is so much knowledge required about the syntax and brackets commas and semicolons and indenting required to build these formulas.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,224 Points 20k badge 2x thumb
Got it. FWIW, there is a limit on how many redirects a browser will do before it throws a message something like "too many redirects".
Photo of Ceci New York

Ceci New York

  • 0 Points
.
Photo of Xavier Fan

Xavier Fan, Champion

  • 720 Points 500 badge 2x thumb
As Dan points out in his example - the general approach is to use multiple $.get functions, with the next one being called in the success function of the first.  I'll play around with this a little bit to see if we can get to the 3 URL case.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 30,224 Points 20k badge 2x thumb
Well that it not exactly what my example demonstrates. I don't use a success function callback. Rather I use the then() method of the promise returned by the AJAX call. All jQuery AJAX methods (and animations) return promises and a promise is "thenable" (yeah that is a real word) meaning that there is a then() method which when it resolves will pass control to the function specified as the argument of the then().
Photo of Xavier Fan

Xavier Fan, Champion

  • 720 Points 500 badge 2x thumb
Gotcha thanks!
Photo of Xavier Fan

Xavier Fan, Champion

  • 720 Points 500 badge 2x thumb
Mark - a more generic case for stringing together 3 URL formulas together:  (I put it as a new answer so that I can do some bolding):

These 3 URLs (URLOne, URLTwo, URLThree) are doing an Edit Record call.

var text URLOne

URLRoot() & "db/" & [_DBID_TABLE_1]

& "?act=api_editrecord&rid=" & ToText([Record ID#])

& "&_fid_7=" & "1";

var text URLTwo


URLRoot() & "db/" & [_DBID_TABLE_1]

& "?act=api_editrecord&rid=" & ToText([Record ID#])

& "&_fid_8=" & "2";

var text URLThree


URLRoot() & "db/" & [_DBID_TABLE_1]

& "?act=api_editrecord&rid=" & ToText([Record ID#])

& "&_fid_9=" & "3";



"javascript:" &


"$.get('" & 

$URLOne

"').then(function(){" &

"$.get('" &

$URLTwo &

"').then(function(){" &

"$.get('" &

$URLThree

"')" &

"})" & 

"});"

& "void(0);"


====

If you want the last URL (URLThree) to do a javascript function call (such as a refresh, or to call copyMasterDetail), you need to call the javascript function directly, instead of using "$.get" to call the URL.

(Note the bolded parts below and compare with the above.  The following parts for $.get surrounding URLThree have been removed:

"$.get('" &

"')" &





var text URLOne = 

URLRoot() & "db/" & [_DBID_TABLE_1]

& "?act=api_editrecord&rid=" & ToText([Record ID#])

& "&_fid_7=" & "1";

var text URLTwo = 


URLRoot() & "db/" & [_DBID_TABLE_1]

& "?act=api_editrecord&rid=" & ToText([Record ID#])

& "&_fid_8=" & "2";

var text URLThree = "location.reload(true);";





"javascript:" &


"$.get('" & 

$URLOne & 

"').then(function(){" &

"$.get('" &

$URLTwo &

"').then(function(){" &

$URLThree & 

"})" & 

"});"

& "void(0);"
Thx for the Friday afternoon gift.  I will play with this over the weekend.
Photo of Xavier Fan

Xavier Fan, Champion

  • 720 Points 500 badge 2x thumb
You're welcome!
Photo of Robert

Robert

  • 30 Points
If i was to switch the two scripts around would I be correct in saying that after a copy is made the original would have its user changed?
Photo of Xavier Fan

Xavier Fan, Champion

  • 720 Points 500 badge 2x thumb
I don't think it'd work that way - because the "copy master detail" call doesn't function like a normal URL call (like UpdateUser).  The reason it works in the original example is because the "copy master detail" was last, and there wasn't anything that was supposed to be called after it.