Announcement

Collapse
No announcement yet.

Using a button to duplicate a record and then display that record

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using a button to duplicate a record and then display that record

    I am trying to add a button to my master/detail form that will duplicate the current record and it's detail records and then edit that record. This is to be used as the starting point for a new record. To prevent problems with duplicate ID numbers and spurious entries, I think it would be best to redirect back to the same form but in Add mode and then prefill the form fields with the data from the copied record. How would I go about doing this?

    Any help would be greatly appreciated.
    Thanks,
    Chris
    Last edited by cnbrammer; 09-17-2014, 01:36 PM.

  • #2
    Originally posted by cnbrammer View Post
    I am trying to add a button to my master/detail form that will duplicate the current record and it's detail records and then edit that record. This is to be used as the starting point for a new record. To prevent problems with duplicate ID numbers and spurious entries, I think it would be best to redirect back to the same form but in Add mode and then prefill the form fields with the data from the copied record. How would I go about doing this?

    Any help would be greatly appreciated.
    Thanks,
    Chris
    More easy.

    Create a PHP button and do an INSERT SQL sentence against the table with the values of the fields. Then do a SELECT over detail with old id, and Insert again with results using newID for master field.
    /Giuseppe

    Professional Scriptcase Services
    Some Customers opinions

    Comment


    • #3
      cnbrammer - I was wondering if you were able to figure this out in scriptcase? I'm being asked to create the same situation. Any tips you could provide would be great. Although it's a good reference, I'm not quite understanding Giu's explanation. Thanks!

      Comment


      • #4
        With sql you can replicate the record. With exit(#) where # is a number, see helpfile, you can restart the same module. If you add a global to your application for the where clause in the sql section:


        where mykey=[glob_mykey] you can preselect the new inserted record if you have saved the last insert key value. Initially you have to set this global variable to '0 or 1=1' in this case to get a full selection.
        Albert Drent
        aducom software netherlands
        scriptcase partner, reseller, support and (turn-key) development
        www.scriptcase.eu / www.scriptcase.nl

        Comment


        • #5
          I finally got this written and working except for reloading the form so that it shows the newly created copy. I have tried sc_exit, sc_redir, and tried redirecting to a blank app and back to the form, with no luck. To complicate things, I have no WHERE clause in the form's SQL because I am using Application Link from a grid application to the form that has this copy button. How do I get the form to redirect back to the newly created copy?

          //Make a temporary table and copy the current order fields into it.
          $sqls = "CREATE TEMPORARY TABLE OrderTemp ENGINE=InnoDB " .
          "SELECT * FROM Orders " .
          "WHERE OrderID = " . {OrderID};
          sc_exec_sql($sqls);

          //Set the OrderID of the temporary record to NULL to avoid key field conflict.
          $sqls = "UPDATE OrderTemp SET OrderID = NULL";
          sc_exec_sql($sqls);

          sc_exec_sql("start transaction;");

          //Add the temporary order record to the orders database.
          $sqls = "INSERT INTO Orders " .
          "SELECT * FROM OrderTemp ";
          sc_exec_sql($sqls);

          //Save the new OrderID to a variable
          $NewOrderID = mysql_insert_id();

          sc_exec_sql("DROP TABLE OrderTemp");//Remove temporary table.

          //Make a temporary table and store the items of the copied order.
          $sqls = "CREATE TEMPORARY TABLE ItemTemp ENGINE=InnoDB " .
          "SELECT * FROM Items " .
          "WHERE OrderID = " . {OrderID};
          sc_exec_sql($sqls);

          //Set OrderID of the temporary item table records to the new OrderID.
          $sqls = "UPDATE ItemTemp SET OrderID = " . $NewOrderID ;
          sc_exec_sql($sqls);

          //Set the ItemID of the temporary record to NULL to avoid key field conflict.
          $sqls = "UPDATE ItemTemp SET ItemID = NULL";
          sc_exec_sql($sqls);

          //Add the temporary item records to the items database.
          $sqls = "INSERT INTO Items " .
          "SELECT * FROM ItemTemp ";
          sc_exec_sql($sqls);

          sc_exec_sql("DROP TABLE ItemTemp");//Remove temporary table.

          sc_exec_sql("commit;");

          //Redirect to the form for the new order
          sc_redir("B1-OrderForm");

          Comment


          • #6
            I figured out how to get around it, I had to manually define the link between the grid and form. So I added a new field to the grid and set it to HTML image, chose an edit icon, and added a link to the form there, passing the same variable I used in the redirect on the copy function.

            Comment

            Working...
            X