Discussions

 View Only
Expand all | Collapse all

Formula URL button - save and redirect

QuickBaseCoach Dev./Training

QuickBaseCoach Dev./Training11-09-2018 20:32

  • 1.  Formula URL button - save and redirect

    Posted 10-25-2018 17:58

    Hi there

    I?ve created a form accessible to ?everyone on the internet? for our clients to submit service requests. I added ?&ifv=20? to the url to hide the qb branding. However, doing this causes the save button to become hidden. I created a formula url button that will save it  using this javascript I found on the forum:

    var text URL = "javascript:void(DoSaveAdd())";var Text Image = "<a id='saveButton' class='Vibrant Success' onclick='DoSaveAdd()' href='#'>Submit</a>";"<b href =" & $URL &">" & $Image & "</b>"

    My problem now is how do I get the page to redirect after the form is saved. Here?s a link to the form, you can try it and see what happens.

    https://sparkav.quickbase.com/db/bnzwm7ykp?a=nwr&ifv=20

    I'd like it to redirect to an external webpage. If that is not natively possible than I'd like it to redirect to a rich text page I've created in quickbase.

    Any help with this would be greatly appreciated!

    Thanks,
    Elisha


  • 2.  RE: Formula URL button - save and redirect

    Posted 10-25-2018 19:25
    Submit the form with script using FormData:

    var editform = document.getElementsByName("editform")[0];
    var formData = new FormData(editform);
    formData.set("subact", "save");

    window.onbeforeunload = null;

    fetch("bnzwm7ykp?a=FinishEditRecord", {
      body: formData,
      method: "POST",
      credentials: "include"
    }).then(function(response) {
      document.location.href = "https://ibm.com";
    });

    This script (1) creates a FormData object equivalent to the QuickBase form, (2) sets the subact hidden field to "save" and (3) submits the from via fetch which returns a promise. When the promise resolves, the page redirects to ibm.com.

    Speaking of promises, I got to get me a one of those big async yard signs:





  • 3.  RE: Formula URL button - save and redirect

    Posted 10-25-2018 19:34
    You can add a redirect in your formula by replacing $URL with

    $URL & "&rdr=" & $URLTWO

    You'll then need to add another variable for URLTWO (var text URLTWO = )

    You are correct that you cannot redirect outside of QuickBase, so URL TWO would be something like this (where the pageID=3 is replaced with your pageID).

    URLRoot() & "db/" & Dbid() & "?a=dbpage&pageID=3"; 

    -Sharon



  • 4.  RE: Formula URL button - save and redirect

    Posted 06-17-2019 23:56
    I was recently asked about the code I provided here and I have a better alternative using the onclick method below.

    var text SAVE = "onclick=\"$('#saveButton').click()";
    var text REDIRECT = URLRoot() & "db/" & Dbid() & "?a=dbpage&pageID=2"; 

    "<a class='Vibrant Success' & " & $SAVE & "; window.location.href=' "& $REDIRECT &" '\">SAVE & Go to Page</a>"


  • 5.  RE: Formula URL button - save and redirect

    Posted 06-20-2019 19:50
    You can actually do this without using JavaScript as well.

    You'll need to use the table setting called "Save parent record automatically when a child record is created." When this box is checked for your table, any time has the form open for adding / editing a record, and they click a link, they won't get the pop-up window asking if they want to save the record first. Instead, the user will see the "Saving" message briefly, then they will be redirected based on whatever link they clicked.

    You can use standard a Formula - URL here, which essentially becomes your own custom save button. Without needing to call DoSaveAdd.



  • 6.  RE: Formula URL button - save and redirect

    Posted 06-20-2019 19:53
    Thanks Brian!

    I was actually working with someone who had a Save & Redirect formula that was working that did not explicitly have Save included... I was baffled for a bit then realized they had this setting you mentioned engaged. 

    Great call out!


  • 7.  RE: Formula URL button - save and redirect

    Posted 10-25-2018 19:36
    Thanks Dan!

    I'm getting a syntax error on the period in the first line of the script.
    Is there a change that needs to be made to the code?


  • 8.  RE: Formula URL button - save and redirect

    Posted 10-26-2018 13:12
    I pasted the code into the console and it worked for me unchanged:

    https://sparkav.quickbase.com/db/bnzwm7ykp?a=dr&r=p


  • 9.  RE: Formula URL button - save and redirect

    Posted 10-25-2018 21:44
    Elisha, there is possibly an easier solution.

    Your "add record" can specify where to go to upon Save.  ie the NextURL.

    var text ADD = URLRoot() & "db/" & [_DBIDxxxx] & "?API_GenAddRecordForm&ifv=1":

    var text RedirectTo = the url to redirect to;

    $ADD
    & "&NextURL=" & URELEncode($RedirectTo )



  • 10.  RE: Formula URL button - save and redirect

    Posted 01-07-2019 19:39
    I've never quite understood how redirects work and I could not get any of these solutions to work for me.  I created a URL button intended to be used in the edit view of a form that has "ifv=0" added.  No matter what I try to put in the "&NextURL" portion of this I am always just redirected to the dashboard of the app.  I don't know why.  Why?!?!

    HOWEVER, if I make sure that the "Save parent record automatically" is checked in the table's advanced settings I simply put the URL I want to redirect to in the button.  The record saves on it's own because that setting is engaged and I end up at the redirect page.  Great!  BUT.....

    These buttons do not work on mobile.  The last button I describe with the URL I want end up shows in the edit view in mobile (I haven't seen any buttons that shows up in a report) and successfully redirects to the URL. but for reasons I don't understand the record does not save.  Why?!?!

    Any ideas about how I can get a save and redirect to work in mobile?


  • 11.  RE: Formula URL button - save and redirect

    Posted 01-07-2019 19:45
    I can't help you for the mobile question but if you have a question about Desktop mode i can try to help.  I'm not sure from your post above what specific question you have what your current non working code is.


  • 12.  RE: Formula URL button - save and redirect

    Posted 01-07-2019 20:09
    Thanks, Coach.  Let's start with the code that is your response here.  Is this a button you would put on a dashboard?  That API is going to generate the form to add a record, right?  So, it seems like it doesn't make sense to put it on a record, but maybe I'm wrong about that.

    I'm looking for a situation where I open the edit view of a record, then someone edits the record and I replace the save button with a button that saves and redirects to a rich text page I have created.  Is this type of code the way I would do that?


  • 13.  RE: Formula URL button - save and redirect

    Posted 11-09-2018 20:09

    Hi there

    My apologies for the delayed response. I'm new to javascript & quickbase and at the time of my initial posting these responses were beyond my skill level. 

    I've advanced a little and decided to give these solutions another go. I�m not sure how & where to plug in these different lines of code. 

    I tried putting Dan�s code 
    1) directly into a formula-url field and I get a syntax error. 
    2) directly into a formula-rich text I�d be redirected to ibm.com whenever I viewed a record
    3) into a javascript page and loading it via iol and it would save the record but the redirect wouldn�t work

    Similar challenges with Sharons & QBChampions solutions. I'm no sure how/where to put together and plug in the code.

    I've had a bunch of success using some IOL scripts for other issues.

    Any guidance on how to plug in your answers?

    Thanks!


  • 14.  RE: Formula URL button - save and redirect

    Posted 11-09-2018 23:22
    Both my and Mark's (QB Coach) suggestions work in the Formula URL field.

    Here is the formula I suggested more explicitly written out using your inital formula as a base:

    var text URL = "javascript:void(DoSaveAdd())";
    var text URLTWO = URLRoot() & "db/" & Dbid() & "?a=dbpage&pageID=3"; 
    var Text Image = "<a id='saveButton' class='Vibrant Success' onclick='DoSaveAdd()' href='#'>Submit</a>";
    "<b href =" & $URL & "&rdr=" & $URLTWO &">" & $Image & "</b>"

    (where the pageID=3 is replaced with your pageID).

    -Sharon


  • 15.  RE: Formula URL button - save and redirect

    Posted 05-18-2022 16:55
    Edited by Brittany Scheid 05-18-2022 17:46
    @Quick Base Junkie What field type does this code get used in? When I tried both Formula-URL & Formula-Rich Text, I received syntax errors saying javascript was not allowed. (screenshot below). I've included my code below as well.
    My scenario is that I'm actually not saving a parent record, but wanting to save the record being edited, and bring up that same record but in a different form. Almost like the "save & keep working" but using a different form. Would turning on the "Save parent record automatically when a child record is created" option address this too, negating the need for the javascript? 

    var text URL = "javascript:void(DoSaveAdd())";
    var text URLTWO = URLRoot() & "db/" & [_DBID_EVENTS] & "?a=er&dfid=12&rid=" & [Record ID#];
    var Text Image = "<a id='saveButton' class='Vibrant Success' onclick='DoSaveAdd()' href='#'>Submit</a>";
    "<b href =" & $URL & "&rdr=" & $URLTWO &">" & $Image & "</b>"



    ------------------------------
    Brittany Scheid
    Special Projects Manager
    SimiTree
    ------------------------------



  • 16.  RE: Formula URL button - save and redirect

    Posted 05-18-2022 17:46
    Never mind! I found another post that explained how to create a formula-rich text field that acts as the "Save & Keep Working". I just updated the URL to include the form ID to get what I needed.  Here's the post for anyone else who's interested.

    https://community.quickbase.com/communities/community-home/digestviewer/viewthread?MessageKey=d959cde4-8af7-4499-b793-c3f8807c57cd&CommunityKey=d860b0f8-6a48-487b-b346-44c47a19a804&tab=digestviewer

    ------------------------------
    Brittany Scheid
    Special Projects Manager
    SimiTree
    ------------------------------



  • 17.  RE: Formula URL button - save and redirect

    Posted 05-18-2022 18:59
    @Brittany Scheid I'm glad you found a solution.

    I also have a video that covers the "SaveBeforeNavigating" class (about halfway through the video below) that might be helpful to you or others. AND if you want to learn even more about building buttons in Quickbase I'd recommend The Button Masterclass.



    ------------------------------
    Quick Base Junkie
    Quick Base Junkie
    https://quickbasejunkie.com
    ------------------------------



  • 18.  RE: Formula URL button - save and redirect

    Posted 05-18-2022 21:07
    That is helpful, thanks! Is there a way to have the action pause in between when the record saves and the next page displays? I have a pipeline that runs when the record is saved, and I want that to be finished before displaying the next page. I tried looking at the examples on Code Pages App, but the examples are either Prompt & Redirect, or Run, Pause & Refresh. I would need something like Save, Pause & Redirect. I don't know javascript really at all to be able to figure out what needs to be changed.

    ------------------------------
    Brittany Scheid
    Special Projects Manager
    SimiTree
    ------------------------------



  • 19.  RE: Formula URL button - save and redirect

    Posted 05-19-2022 13:25
    @Brittany Scheid, you should be able to use the Run, Pause, & Redirect.

    Only in place of an API call for the 'run', navigate to say the home page (anything away from the page you're on).

    So taking the formula from the app you mentioned...

    / Creating a toggle for the Checkbox
    var text toggle = If([Checkbox 2], "false", "true");

    // API URL to Add/Edit/etc.
    var text urlToExecute = URLRoot() & "db/" & Dbid() & "?a=API_EditRecord&apptoken=" & [App Token]
        & "&rid=" & [Record ID#] & "&_fid_20=" & $toggle;

    URLRoot() & "db/" & AppID() & "?a=dbpage&pageid=6" // Open code page 6
    & "&url=" & URLEncode($urlToExecute) // Pass in the URL to execute

    Change the urlToExecute variable to URLRoot() & "db/" & AppID()

    -Sharon​

    ------------------------------
    Quick Base Junkie
    Quick Base Junkie
    https://quickbasejunkie.com
    ------------------------------



  • 20.  RE: Formula URL button - save and redirect

    Posted 05-19-2022 14:35
    Thank Sharon. I tried this, but it's still just sending me back to the same page after it goes to the code page. I tried sending it to the form that I want and also just to the app home page and neither worked. Both times it just went back to the same edit form screen.  Any thoughts? Below is the code from the formula-URL field.  I used the code directly from the code page Body on this page without any changes.

    var text rid = 
        If([Record ID#]>0,
            ToText([Record ID#]), // Record ID already exists
            "%%rid%%" // New record, no Record ID exists yet
        )
    ;
    
    // API URL to Add/Edit/etc.
    var text urlToExecute = URLRoot() & "db/" & Dbid() & "?a=er&dfid=12&rid=" & $rid;
    
    //& Dbid() & "?a=er&dfid=12&rid=" & $rid;
    
    URLRoot() & "db/" & AppID() & "?a=dbpage&pageid=4" // Open code page 4
    & "&url=" & urlencode($urlToExecute) // Pass in the URL to execute​


    ------------------------------
    Brittany Scheid
    Special Projects Manager
    SimiTree
    ------------------------------



  • 21.  RE: Formula URL button - save and redirect

    Posted 05-19-2022 15:02
    @Brittany Scheid Ahhhh, I missed that in my last reply.​

    On the code page, update this line of text:

    window.location.href = document.referrer;

    to

    window.location.href = document.referrer + '&dfid=12';

    assuming your form id is 12

    Please note, this will render the code page less versatile for multiple uses since it will be hard coded to return to this specific form. But this is the simplest way to achive your goal.

    -Sharon

    ------------------------------
    Quick Base Junkie
    Quick Base Junkie
    https://quickbasejunkie.com
    ------------------------------



  • 22.  RE: Formula URL button - save and redirect

    Posted 05-19-2022 15:05
    Edited by Brittany Scheid 05-19-2022 15:10
    So that got me to the right form, but because the starting page is adding a new record, it pulled up the correct form, but for adding a new record. Is there any way to get it to save that new record, go to the pause code page, then redirect to form ID 12 for that record that was just ssaved?

    ------------------------------
    Brittany Scheid
    Special Projects Manager
    SimiTree
    ------------------------------



  • 23.  RE: Formula URL button - save and redirect

    Posted 05-19-2022 15:45
    @Brittany Scheid I had a sneaking feeling you might bring that up...

    ​​I really recommend The Button Masterclass so that these elements can be better understood.

    Several additional changes will need to be made.

    1. Change your field from a URL Formula to a Rich Text Formula.

    2. Change the formula to something like this:
    var text RID = If(IsNull([Record ID#]),"%%rid%%",ToText([Record ID#]));
    
    var text urlToExecute = URLRoot() & "db/" & Dbid() & "?a=er&dfid=12&rid=";
    
    var text URL = URLRoot() & "db/" & AppID() & "?a=dbpage&pageid=6" // Open code page 6
        & "&url=" & URLEncode($urlToExecute) & $RID ; // Pass in the URL to execute
    
    var text LABEL = "Custom Save & Keep Working";
    
    "<a class='Vibrant Success SaveBeforeNavigating' data-replaceRid=true href='" & $URL & "'>" & $LABEL & "</a>"​

    3. Update the code page's errRdr function

        function errRdr(){
            let urlParams = new URLSearchParams(window.location.search);
            let url = urlParams.get('url');
            // Redirects to the previous page, if this page was the previous page as well, then redirect to the app home page
            if(document.referrer && document.referrer !== window.location.href) {
                window.location.href = url;
            }else{
                window.location.href = window.location.origin + window.location.pathname;
            }
        }​


    CAUTION the use of the %%rid%% record id replacer will only work if an actual change is made to the new record before the button is pressed. (ie starting a new record and then immediately clicking the button will not force the record to save because there is nothing to save).

    -Sharon



    ------------------------------
    Quick Base Junkie
    Quick Base Junkie
    https://quickbasejunkie.com
    ------------------------------



  • 24.  RE: Formula URL button - save and redirect

    Posted 05-24-2022 12:01
    That worked like a charm! Thank you so much Sharon! I will definitely look into taking the Button Masterclass. Thanks again!

    ------------------------------
    Brittany Scheid
    Special Projects Manager
    SimiTree
    ------------------------------



  • 25.  RE: Formula URL button - save and redirect

    Posted 01-07-2019 21:19
    @Elisha
    My suggestion was this

    Do not make a custom save button, but rather have the user launch the Add Record off this formula URL button



    var text ADD = URLRoot() & "db/" & [_DBIDxxxx] & "?API_GenAddRecordForm":

    var text RedirectTo = the url to redirect to;

    $ADD
    & "&NextURL=" & URELEncode($RedirectTo )


    You can build that button on a any record and copy the generated code and paste it into a dashboard button.  The formula with them calculate the URLEncoding.




  • 26.  RE: Formula URL button - save and redirect

    Posted 11-09-2018 20:32
    Try my suggestion, it�s stupid simple.


  • 27.  RE: Formula URL button - save and redirect

    Posted 08-26-2020 14:50
    Edited by Rohit 08-26-2020 14:51
    Hi,
    None of these suggestions works in my case. Any help is greatly appreciated!

    I'm in the edit mode for a record that already exists. I need to save the current record which triggers certain form rules and automations and then I want to redirect back to the parent record on a specific form. Is this possible?




  • 28.  RE: Formula URL button - save and redirect

    Posted 08-26-2020 17:52
    I think this is an easy one

    Go to the child table and Advanced Properties and check the box for
    Save parent record automatically when a child record is created

    Then just make a URL formula link to go to the parent record in edit mode.

    URLRoot() & "db/" & [_DBID_Parent_Table_Alias] & "?a=er&rid=" & [related parent]

    Then when you click that link to navigate away from the child which has been "dirtied" with unsaved input, Quick Base will auto save and then go to your link.




    ------------------------------
    Mark Shnier (YQC)
    Quick Base Solution Provider
    Your Quick Base Coach
    http://QuickBaseCoach.com
    mark.shnier@gmail.com
    ------------------------------



  • 29.  RE: Formula URL button - save and redirect

    Posted 08-26-2020 22:40
    Edited by Rohit 08-26-2020 22:53
    Oh wow! That was a super simple solution and it worked. So that option enables 'the child' to be saved and go back to the parent in spite of the help text being misleading and saying --> "Select this option to automatically save a parent record in a parent table when a child record is created. You will not be prompted to save the parent record. It is automatically saved."

    But I just came across a limitation with it, what if the record is not being 'dirtied' by the user but a form rule is supposed to dirty it.

    Previously the user has two button choices that takes them to two separate forms (Approval Form and Rejection Form) for the same record and on these pages, they have the option to add notes if they're approving the request or rejecting the request. For the rejection of the request Notes are required; for approval Notes are not required. So it works when the user is rejecting it (because by adding notes, the user dirties the record) but it doesn't work when the user is approving the record and does not put in notes because they're optional. Both forms have a form rule trigger that changes the approval status on save. 


    Thanks a lot Mark!


  • 30.  RE: Formula URL button - save and redirect

    Posted 08-26-2020 23:37
    I was able to figure this limitation out. 
    I changed the Approval button from a simple redirect to an API_Edit record and then timestamp a hidden field. A form rule then wipes the hidden field when it is not empty on the Approval form thus causing the record to be dirty and the save to work even when the notes are optional on the Approval form.

    Thanks again Mark for your idea!

    ------------------------------
    Rohit Burani
    ------------------------------



  • 31.  RE: Formula URL button - save and redirect

    Posted 04-29-2021 10:07
    Extremely late to the convo, the fix on mobile for nexturl is to use nexturldashboard.

    ------------------------------
    GMacri
    ------------------------------



  • 32.  RE: Formula URL button - save and redirect

    Posted 04-29-2021 12:05
    Thx for that post.  Can you post a complete formula example?

    ------------------------------
    Mark Shnier (YQC)
    Quick Base Solution Provider
    Your Quick Base Coach
    http://QuickBaseCoach.com
    mark.shnier@gmail.com
    ------------------------------