0

Best practice to duplicate a record

Hello friends,

I am using the Ninox macOS client (now that it has been updated to 3.13.3)

I have created a "Duplicate Product Item" button to create a duplicate record of any product item in order to save the typing and time.

What is the best practice to create a new record (duplicate record) in a table? Currently I am doing "Do as server ... end". Is this a good practice?

I want to popup the new record in the end (after creation) to start editing the record. But I think 'cuz of Do as server, the popup does not happen although I am using the Ninox macOS app and not the browser.

 

do as server
    let currentItem := this;
    let newItem := (create 'Product Items');
    newItem.(
        Product := currentItem.Product.Id;
        Color := currentItem.Color.Id;
        'Item Name' := currentItem.'Item Name';
        'Item Code' := currentItem.'Item Code' + "_New";
        'Item Status' := currentItem.'Item Status';
        'Distributor Price' := currentItem.'Distributor Price';
        'Installer Price' := currentItem.'Installer Price';
        Description := currentItem.Description
    )
end

 

How can I make that popup happen ?

11 replies

null
    • Mel_Charles
    • 3 mths ago
    • Reported - view

    Your script looks fine to me.

    My sample script pops up the new record but I if too wrap in "do as server" then it don't open.

    But as I am only asking the script to do one task rather than loop through several many computations or records then this works fine for me - I am on web browser version.

    let before := max((select JobDockets).'Job Bag');
    let xJob := 'Job Bag';
    let xDate := 'Order Date';
    let newRecord := duplicate(this);
    openRecord(newRecord);
    newRecord.('Job Bag' := before + 1);
    newRecord.(Status := "New Job");
    newRecord.('Won?' := null);
    newRecord.('Order Ref' := null);
    newRecord.('Order Date' := today())

    • Fred
    • 3 mths ago
    • Reported - view
     said:
    What is the best practice to create a new record (duplicate record) in a table? Currently I am doing "Do as server ... end". Is this a good practice?

    For me the only time I use do as server is when I need to speed up the processing of code. But I am the only user, so others will have to chime in about the advantages/disadvantages in a large multi user environment.

     

     said:
    But I think 'cuz of Do as server, the popup does not happen although I am using the Ninox macOS app and not the browser.

     Where is the DB stored? The MacOS app is only the "server" if it is a local DB.

    If you are accessing a cloud DB, the MacOS app is a client.

    • John_Halls
    • 3 mths ago
    • Reported - view

     From what I understand all the variables are scoped so that those declared within the do as server will only be available there, and those outside the do as server similarly. All I can suggest is that a flag is set within the record that allows it to be found and then pop-up. The flag then has to be un-set before then next is set.

    Regards John

    • Database App Developer
    • vermau81
    • 3 mths ago
    • Reported - view

    I am using the Ninox cloud and the macOS app.

    1. When I scope newItem variable outside "Do as server", an empty record gets created and still no popup.

     

    var itm := null;
    var newItem := (create 'Product Items');
    do as server
        let currentItem := this;
        newItem.(
            Product := currentItem.Product.Id;
            Color := currentItem.Color.Id;
            'Item Name' := currentItem.'Item Name';
            'Item Code' := currentItem.'Item Code' + "_New";
            'Item Status' := currentItem.'Item Status';
            'Distributor Price' := currentItem.'Distributor Price';
            'Installer Price' := currentItem.'Installer Price';
            Description := currentItem.Description
        );
        itm := record('Product Items',newItem.Id)
    end;
    popupRecord(itm)
    

     

    2. When I scope newItem variable inside "Do as server", no record gets created at all and of course no popup.

     

    var itm := null;
    do as server
        let currentItem := this;
        var newItem := (create 'Product Items');
        newItem.(
            Product := currentItem.Product.Id;
            Color := currentItem.Color.Id;
            'Item Name' := currentItem.'Item Name';
            'Item Code' := currentItem.'Item Code' + "_New";
            'Item Status' := currentItem.'Item Status';
            'Distributor Price' := currentItem.'Distributor Price';
            'Installer Price' := currentItem.'Installer Price';
            Description := currentItem.Description
        );
        itm := record('Product Items',newItem.Id)
    end;
    popupRecord(itm)
    

     

    What I don't understand is the following:

    a) What data type should I give to the variable itm?

    b) According to both the code, a new record should get created ('cuz code is the same in both cases) and the record reference is passed on to the "itm" variable, then why in case of Point no 2 no record gets created and in case of Point no 1 a black record gets created.

    c) In case of my original code (at the top) where I don't use "itm" variable and no reference is passed for the newly created record, everything works fine.

     

    How to popup the newly created record ?

    Thanks,

    • Fred
    • 3 mths ago
    • Reported - view
     said:
    a) What data type should I give to the variable itm?

    That is the great thing about Ninox you don't need to pre-define the variable.

    It seems like a simple piece of code, do you really need the do as server? If you took it out do you notice a speed difference?

      • Database App Developer
      • vermau81
      • 3 mths ago
      • Reported - view

       I removed "Do as server" and

      1. Its pretty fast now

      2. I didn't know there was an inbuilt duplicate() function, so I don't have to write my custom code

      3. Since I am executing the query on the client, it pops up the record as well.

       

      Observations (I might be wrong):

      1. Even if we scope a variable outside the "Do as server", that variable does work in the code, however it actually is not present on the server, so does not work (in case of the itm variable above). newItem variable returns the record that is assigned to itm variable, but itm does not have anything inside it.

      2. Does "Do as server" have to be the first statement in the code?

    • Fred
    • 3 mths ago
    • Reported - view
     said:
    2. Does "Do as server" have to be the first statement in the code?

     It does not, you can mix in do as server on parts of your code that you need it done.

    • Mel_Charles
    • 3 mths ago
    • Reported - view

    what ninox docmentation says!

    do as server ... end

    Occasionally, it may be beneficial to fully execute a portion of your script on the server first, before sending it back to your computer or app.

    We don't recommend using do as server in triggers because triggers in a browser are always executed on the server as well as locally in the app. Rather use do as server in buttons.

    do as server is most often used in conjunction with the http() function to execute API calls server-side first. This bypasses the browser's CORS (cross-origin resource sharing) policy, which would otherwise block the http call. Then you receive the requested data from the server.

    More about API calls.

    Some functions cannot be executed on the server because they are related to your browser or app, e.g., alert().

    Example

    Let's get some data from a table in a database. First, you send a GET request to the respective database. Using the dot operator . , access the values of the response.

    let response := do as server
    http("GET", "https://api.ninoxdb.de/v1/teams/" + teamId() + "/databases/" + databaseId() + "/tables/" + tableId(this) + "/records", {
    Authorization: "Bearer 0xxx0000-000x-00xx-x0x0-0x0x0000000x"
    }, null)
    end;
    response.result

    Result: The result value of the response is returned, which in this case consists of only one record and the information contained in the individual fields.

    [{
        "id":1,
        "sequence":129,
        "createdAt":"2021-03-11T08:43:53",
        "createdBy":"xx0xxXX0XxxxXXxXx",
        "modifiedAt":"2022-01-21T14:20:36",
        "modifiedBy":"xx0xxXX0XxxxXXxXx",
        "fields": {
                    "Product name":"Ninox Cola",
                    "Product ID":"PID-123456789",
                    "Price":0.99
                  }
    }]

     

      • Database App Developer
      • vermau81
      • 3 mths ago
      • Reported - view

       so I used do as server in the button click.  It I didn’t know that there was a duplicate function already.

      But honestly this do as server thing is really confusing. Can’t really understand when to use it and when not to.

      i am using the Mac app. If I write custom logic to duplicate a record on a button click then as per the above text I should use do as server. Record gets duplicated, however can’t get the popup record.

      Actually at many places things are not very well explained in Ninox and there is not enough documentation on Google about Ninox except this forum and Nioxus. Not even any books online that we can read to learn about Ninox.

      • Mel_Charles
      • 3 mths ago
      • Reported - view

       I'm with you there - you can't beat a good book of some 800 pages of the likes as Ms Access/Excel Bibles etc. Even the java script books out there are good but then you have to discount all the stuff that does not apply to ninox.

      My take on "do as server" is frankly down to performance.

      To copy/duplicate a job via a script - the performance hit is almost negligible with or with out do as server. However when I bath update my main table that now has over 1 million records. the difference is night and day ( if i didn't have do "as a server" set - I could go on holiday and when I have come back the update would still be running 🤣

    • Fred
    • 3 mths ago
    • Reported - view
     said:
    Actually at many places things are not very well explained in Ninox and there is not enough documentation

    Yep that is the state of Ninox. In the years I've been using it, things have not really improved.

Content aside

  • 3 mths agoLast active
  • 11Replies
  • 85Views
  • 4 Following