how to run a javascript function before saving record?

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

I want to execute some javascript during a save operation.  Yes, I realize that  it is not supported, but I don't know how to do what I want without using the api in javascript.


I found this code by dandiebolt_QB_Pirate, Pres. of Import Onload Sector

DoSaveAdd = (function(fn){    return function(){    //your code      var result=fn.apply(fn, arguments);      return result;    }  })(DoSaveAdd); 

but I don't know where to put it or what to hook it to?  Any ideas?
I also found this older reference but the site no longer exists... any thoughts? http://quickbase.posterous.com/a-quickbase-interposer


Thanks for the help, I am new to this as of the last couple months, but have a lot of IT experience..Dean
Photo of Dean

Dean

  • 708 Points 500 badge 2x thumb

Posted 3 years ago

  • 0
  • 1
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 29,984 Points 20k badge 2x thumb
The essential question is: "Does your script require the value of the newly create [Recod ID#] from the add new record page?" If so I have a method called the Save and Do technique that will give you access to the newly created [Record ID#] value so that your script can use it. However, if you just want to run some script on the page that will respond to user events or execute just before the Save button is clicked you can just use the Standard Image Onload technique to get your script to load and execute. See:

Image Onload Technique
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=294


For help with the Save and Do technique please contact me using the information in my profile.
Photo of David

David

  • 0 Points
Hi Dan,
Can you please help me understand how Save and Do is accomplished?
Photo of Kp

Kp

  • 14 Points
Can your save and do tech work on mobile? Mobile does not allow me redirect without saving, but there is no DoSaveAdd on mobile.
Photo of Anthony

Anthony

  • 10 Points
Hi Dan,
I too am interested in Save and Do technique. Basically I have table A where I would enter data in to fields, then I would like to have a button that saves the record, and preforms an add record function to  create a record in table B of the information put in table A. Then bring them back to the edit form where the populated fields are empty and they can repeat.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 29,984 Points 20k badge 2x thumb
S>I too am interested in Save and Do technique ...
You want the Save and Scooby Doo Technique:
https://haversineconsulting.quickbase.com/db/bkjdxnw2x?a=nwr

https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=467

https://quickbase-community.intuit.com/questions/1298548
>how to run a javascript function before saving record?
BTW the original question asked for running code before the record was saved no after the record was saved. I am not sure that is what was intended but the Save and Do / Save and Scooby Doo runs the code after the record is saved.
Photo of Anthony

Anthony

  • 10 Points
Thanks Dan, I will take a look into this. My javascript is non-existent, but I believe I can figure it out with the assistance you provided.
Photo of Anthony

Anthony

  • 10 Points
Hi Dan, Sorry to bug you again. I managed to get the Scooby Doo technique to work. I was wondering if you have any resources that I could read to figure out how to write the java for adding a child record from fields in the parent.after it saves but before it reloads back to the edit form.

Thanks,
Anthony Lin
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 29,984 Points 20k badge 2x thumb
David, Kunpeng, Anthony, Dean> "how do save and do"?

Save and Scooby Doo
https://quickbase-community.intuit.com/questions/1298548
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 29,984 Points 20k badge 2x thumb
I managed to get the Scooby Doo technique to work.

You are in the club but your choice of verbs is a little week. How about:  I dominated the Scooby Doo technique.

If you are adding only one child and you have determined the [Record ID#] of the newly created parent record you can use this code:

$.post(dbidChild, {
  act: "API_AddRecord",
  _fid_6: rid,
  _fid_7: "I am a little teapot"
}).then(function(xml) {
  //any additional code
});

In the above fid=6 is the [Related Parent] field in the child and _fid_7 represents any additional fields you want to set values for in the child table.

If you wanted to add multiple child records you would use the method API_ImportFromCSV rather than makes multiple calls to API_AddRecord.
Photo of Dean

Dean

  • 708 Points 500 badge 2x thumb
so lets say that adding the child is just one of several activities... how does one get the rid if the added child record?  Thanks for the help.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 29,984 Points 20k badge 2x thumb
within the then() you use this code:

var rid = $("rid", xml).text ():

also this code may help debug and understand the xml response:

console.dirxml (xml)
Photo of Dean

Dean

  • 708 Points 500 badge 2x thumb
Thanks Dan...

A problem I am having is that when I create my own save button that executes a javascript file, I get prompted with a "Save" or "Don't Save".  When I click the "Don't Save" all the variables get passed to the script and it works fine.  However, if I click the "Save" (Which is the intuitive thing to do and what my users will want to do) none of the parameters are passed to the script.

How do you solve this problem?  (I greatly appreciate your thoughts on this)

Quickbase support has not been able to help with this.  Ideally, there would be no prompt and the script would be called with all the associated parameters.

Here is my save button code:
"javascript:" &
"var TaskName=" & "'updateEventLead'" & ";" &
"var RelatedLead=" & [Related Lead] & ";" &
"var RelatedProject=0"   & ";" &
"var Status=" & [setStatusToSendToUpdateScript] & ";" &
"var RelatedWorkflow=" &  [Lead - Related workflow] & ";" &
"var WorkflowTask=" & [WorkflowTask] & ";" &
"var AssignedTask=" & [AssignedTask] & ";" &  
"var DateNow='" & Now() & "';" &
"$.getScript('https://mycompany.quickbase.com/db/xxxxxxxx?a=dbpage&pagename=z_Main_Javascript_Processor.js');"&
"void(0);"
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 29,984 Points 20k badge 2x thumb
First of all I don't recommend invoking JavaSscript from a formula in this fashion. Not that you can't get it to work but rather because the image onload technique is the most flexible way to inject JavaScript into a QuickBase authored page that works in just about all scenarios. Why have a grab bag of different ways to inject JavaScript when one way covers all use cases?

But I do recognize that some people are more comfortable with formulas and that these questions will keep being asked. So against my better judgement let me answer as follows:

1) First all your JavaSript variables such as TaskName, RelatedLead, etc should be prefixed (or namespaced) with QBU_ (ie QBU_TaskName, QBU_RelatedLead) so that they will not conflict with QuickBase variables that may have the same name.

2) In your z_Main_Javascript_Processor.js user defined page you need to place all of you code within a closure:

(function(){
  //your code here
})();

If you don't do this the variables you create within the user defined page may also conflict with QuickBase variables that may have the same name.

My advice if you want to stick with your current approach is to fix these two issues, repeat your testing and post a new question focusing on the exact problem you have at that point.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 29,984 Points 20k badge 2x thumb
BTW, your basic problem is probably malformed JavaScript. Your RelatedProject variable is assigned a number 0 while your Status variables is probably assigned a text value:

"var RelatedProject=0"   & ";" &
"var Status=" & [setStatusToSendToUpdateScript] & ";" &

All text values need to be quoted in JavaScript so you need to do this with the second statement:

"var Status='" & [setStatusToSendToUpdateScript] & "';" &

Another reason you should use the image onload technique is to get out of the formula language as quickly as possible so you never have to deal with all the oddities of escaping and embedding JavaScript statements within QuickBase formulas statements.
Photo of Dean

Dean

  • 708 Points 500 badge 2x thumb
Thanks Dan,
As I only have about 150 hours of experience in this product I am happy to follow your suggestions... thanks for the advice.  I am already using an image onload when required for some pages but do not have the save functions in that js file.

Lets say I move all the functions to that onload.js file.  How do I call the javascript function with a save button?  Sorry for asking if this has been explained in some other doc... but I have spent a lot of time reading and don't remember seeing that option.

Is it as simple as calling the function in the then() area?

thanks,
Dean
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 29,984 Points 20k badge 2x thumb
See this post:

What is the Easy Button Technique?
https://quickbase-community.intuit.com/questions/1307849

Ask any further question dealaing with buttons in the above thread or a new one.
Photo of Dean

Dean

  • 708 Points 500 badge 2x thumb
fyi.. Status is a number and not a text variable.  I will change all the variables to be QBU_

Thanks
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 29,984 Points 20k badge 2x thumb
But if there are *any* text field being assigned there will be a JavaScript error without the extra single quotes.
Photo of Dean

Dean

  • 708 Points 500 badge 2x thumb
Yep... I am fine with what is needed there.  :)  thanks for the button link.  I will give that a try.  How does that impact the "Save" "Don't Save" interaction?  Do we still get prompted for that?
Photo of Lina

Lina

  • 0 Points
I would like to know more about the Save and Do technique. I would like to update a different table when I submitted a new record, like a trigger.
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 29,984 Points 20k badge 2x thumb
I have never publicly published the Save & Do technique because it would only generate a flood of questions from users who did not understand it (it was somewhat invasive of QuickBase's code). I have however published an improvement to the original Save & Do technique called the Save and Scooby Do technique which you can read about here:

Save and Scooby Do
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=465
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=467
Photo of Lina

Lina

  • 0 Points
Thanks. I tried the code and it works very well. Furthermore, would you have an example code for an API_AddRecord on javascript? and how to get the [ID Record] after saved it? I will appreciate if you could help me with this problem.