Forum Discussion

ELANAMETH's avatar
ELANAMETH
Qrew Cadet
5 years ago

Confirm Popup and if ""OK"" execute API

Looking for URL/Java script code that will show a confirm popup and then execute an API (specifically an edit record) ONLY if OK was pressed. I have the popup and the API running, but I cannot get the API to run conditionally based on whether or not confirm is true or false. Any hints?

6 Replies

  • AustinK's avatar
    AustinK
    Qrew Commander
    I could try and explain it but I think an example from somewhere else is probably the best. There are several ways but they all basically do the same thing, its more like explicit vs implicit.

    var txt;
    var r = confirm("Press a button!");
    if (r == true) {
      txt = "You pressed OK!";
    } else {
      txt = "You pressed Cancel!";
    }

    That is one way.

    if (confirm("Press a button!")) {
      txt = "You pressed OK!";
    } else {
      txt = "You pressed Cancel!";
    }



    There are more in the links below.

    https://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm
    Also this one https://www.w3schools.com/jsref/met_win_confirm.asp
  • Thank you so much!, but I am trying to incorporate something like this: 

    if (confirm("Are you sure you want to mark complete?")) {
      //(if yes) execute the api (which is sitting in a variable)
    } else {
     // alert that record was not saved
    }

    Here is part of my code with the following variables  (it's all in a URL formula):

    $popup: "javascript:confirm('...')
    $url: the API that is editing the current record
    $Cancel: "javascript:alert('....')

    "javascript:" &
     "$.get('" & $popup & 
        "',function() {" &
        "if ($popup == true) {" &
         & $url & 
          "} else {"
     & $Cancel &
      "});" &
      "void(0);"
    • AustinK's avatar
      AustinK
      Qrew Commander
      First thing I noticed is you had 2 ampersands before $url. I will let you know if I see anything else.
    • AustinK's avatar
      AustinK
      Qrew Commander
      Does this help you more?

      var text popup = "javascript:confirm('testing this')";
      var text url = "javascript:alert('totally okay!')";
      var text cancel = "javascript:alert('cancelled!')";


      "javascript:" &
      "if(!confirm('testing this')){" &
      $cancel &
      "} else {" &
        $url &
      "};"

      At the bottom in the else you would do your $get for the url instead of the popup box. As you can see I am doing !confirm instead of just confirm, so that is saying if confirm is false. So if the user clicks okay it will be true and continue down to the $url part.

      I didn't test this but you can try.

      "javascript:" &
      "if(!confirm('testing this')){" &
      $cancel &
      "} else {" &
       "$.get('" & $url & "');" &
      "};"
    • ELANAMETH's avatar
      ELANAMETH
      Qrew Cadet
      Yay! That worked! I added in a location.reload after the $url and it saved it with the proper results. popup is working as needed. Thank you for all of your help!
  • Your examples are very helpful, I just can't seem to get it to work with another variable.