0

add field by script

through trigger I can not add a field from another table

reading from the manual :

let p := create Person;
p.Name := "Sarah Conner"

 

my script :

let newpagamenti := create pagamenti;
newpagamenti.inizio := today()

7 replies

null
    • John_Halls
    • 2 yrs ago
    • Reported - view

    Hi

     

    It looks like this should work. Where and how are you running this script? Are you creating a child record from a parent? If so the child is being created but the link to the parent is missing and so it won't show up in the parent.

     

    Regards John

    • Fred
    • 2 yrs ago
    • Reported - view

    I don't see anything wrong with the code. Maybe the trigger is not kicking off like you think? Which table/field trigger did you put the code in?

     

    Can you put the code in a button then see if it works?

    • Peter_Romao
    • 2 yrs ago
    • Reported - view

    So @John or anyone, how does one create that link programmatically outside the parent table?

    Say we have a Table 'Parent' and a composite table 'Children' and a table called 'Remote Dashboard'.

    Now I create a button and a field called 'Chosen ID' in this remote dashboard where one can input the Id of the Parent field (that would be an existing  'Parent.Id' so as to make the example possible) 

    The button would have the following code:

    let i := 'Chosen ID';

    let p := select Parent where Id = i;

    let ch := create Children;

    Now the thing here is how do you tie this 'Children' record to the chosen 'Parent'?

    let ch := create p.Children

    won't do, same as:

    let ch := p.create Children

    won't do either. In fact it will not even pass syntax check!

    Maybe by using the record() function to get the handle to the parent?

    • Fred
    • 2 yrs ago
    • Reported - view

    Hi Peter -

     

    It could look something like this:

     

    let i := 'Chosen ID';

    let p := first(select Parent where Id = i);

    let ch := (create Children);

    ch.Parent := p

     

    assuming that Parent is the field name in the Children table. Change it to whatever it actually is. Ninox forced me to add first for the p variable because it says multiple values might get returned even though there should only be one record with that particular Id.

     

    good luck.

    • Alain_Fontaine
    • 2 yrs ago
    • Reported - view

    The "select" statement does indeed return an array of records in all circumstances. But in this case, the "select" statement is not needed at all. One can establish a relation by setting the N:1 reference field to the numeric value of the parent's id. Which gives:

    let i := 'Chosen Id';
    (create Children).(Parent := i)

    Now, if the value entered in "Chosen Id" does not point to an existing record in  "Parent", the script creates an unlinked child record. If you want to avoid this, you can first verify the existence of the parent record:

    let i := 'Chosen Id';
    if cnt((select Parent)[Id = i]) then
    (create Children).(Parent := i)
    else
    alert("Unknown parent!")
    end

    • Peter_Romao
    • 2 yrs ago
    • Reported - view

    That worked ok Fred. I was having the same problem with the multiple values thing so I tried item((select Parent where Id = i).Id,0) which also did the trick.

    Alain your explanation is great.

    Thank you guys!

    • Peter_Romao
    • 2 yrs ago
    • Reported - view

    ...and much simpler.

Content aside

  • 2 yrs agoLast active
  • 7Replies
  • 434Views