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
-
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()) -
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.
-
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
-
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,
-
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?
-
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.
-
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 usedo as server
in buttons.do as server
is most often used in conjunction with thehttp()
function to execute API calls server-side first. This bypasses the browser's CORS (cross-origin resource sharing) policy, which would otherwise block thehttp
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 theresponse
.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 theresponse
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 } }]
-
said:
Actually at many places things are not very well explained in Ninox and there is not enough documentationYep that is the state of Ninox. In the years I've been using it, things have not really improved.
Content aside
- 1 mth agoLast active
- 11Replies
- 78Views
-
4
Following