Discussions

 View Only
Expand all | Collapse all

how to run a javascript function before saving record?

  • 1.  how to run a javascript function before saving record?

    Posted 11-26-2015 16:50

    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


  • 2.  RE: how to run a javascript function before saving record?

    Posted 01-03-2016 18:15
    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.


  • 3.  RE: how to run a javascript function before saving record?

    Posted 01-05-2016 03:43
    Hi Dan,
    Can you please help me understand how Save and Do is accomplished?


  • 4.  RE: how to run a javascript function before saving record?

     
    Posted 01-05-2016 16:44
    Can your save and do tech work on mobile? Mobile does not allow me redirect without saving, but there is no DoSaveAdd on mobile.


  • 5.  RE: how to run a javascript function before saving record?

    Posted 01-28-2016 00:15
    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.


  • 6.  RE: how to run a javascript function before saving record?

    Posted 01-28-2016 00:27
    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.


  • 7.  RE: how to run a javascript function before saving record?

    Posted 01-28-2016 00:31
    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.


  • 8.  RE: how to run a javascript function before saving record?

    Posted 01-30-2016 02:09
    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


  • 9.  RE: how to run a javascript function before saving record?

    Posted 01-30-2016 12:20
    David, Kunpeng, Anthony, Dean> "how do save and do"?

    Save and Scooby Doo
    https://quickbase-community.intuit.com/questions/1298548


  • 10.  RE: how to run a javascript function before saving record?

    Posted 01-30-2016 12:29
    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.


  • 11.  RE: how to run a javascript function before saving record?

    Posted 01-31-2016 13:41
    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.


  • 12.  RE: how to run a javascript function before saving record?

    Posted 01-31-2016 13:47
    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)


  • 13.  RE: how to run a javascript function before saving record?

    Posted 02-01-2016 12:51
    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);"


  • 14.  RE: how to run a javascript function before saving record?

    Posted 02-01-2016 13:13
    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.


  • 15.  RE: how to run a javascript function before saving record?

    Posted 02-01-2016 13:39
    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.


  • 16.  RE: how to run a javascript function before saving record?

    Posted 02-01-2016 14:38
    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


  • 17.  RE: how to run a javascript function before saving record?

    Posted 02-01-2016 16:50
    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.


  • 18.  RE: how to run a javascript function before saving record?

    Posted 02-01-2016 17:46
    fyi.. Status is a number and not a text variable.  I will change all the variables to be QBU_

    Thanks


  • 19.  RE: how to run a javascript function before saving record?

    Posted 02-01-2016 17:52
    But if there are *any* text field being assigned there will be a JavaScript error without the extra single quotes.


  • 20.  RE: how to run a javascript function before saving record?

    Posted 02-01-2016 18:14
    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?


  • 21.  RE: how to run a javascript function before saving record?

    Posted 05-01-2016 15:14
    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.


  • 22.  RE: how to run a javascript function before saving record?

    Posted 05-01-2016 15:20
    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


  • 23.  RE: how to run a javascript function before saving record?

    Posted 05-02-2016 18:03
    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.