0

Populating a linked record by code

Hi all,

This is likely to be a really basic question, but I just don't seem to get this to work.

I have a patient database. Each patient has multiple prescriptions that are filled at a specified pharmacy. Each patient has a "default" pharmacy where they usually (but not always) fill the scripts.

When I create a new prescription record (in a different table), I can read the patient default pharmacy value (I use name of the pharmacy as linked field) and store it in a text field in the new prescription record.

The thing I cannot get to work is this: I also link each prescription to a pharmacy (by name). I want that selection to have a default value that is the same as the patient's default pharmacy, but NOTHING.WORKS. 

I've lost count of how many ways I've tried to set the value of the linked pharmacy record in the prescription when the patient field changes... Currently the 'trigger after update' code in the patient field is:

let xPharm := Client.Pharmacies.'Pharmacy name';
select Pharmacies where 'Pharmacy name' = xPharm;
'Default pharmacy' := xPharm;
Pharmacy := xPharm

The extra lines below are just to ensure I've fished out the correct value from patient record

"Default pharmacy" is a dynamic select field (tried in desperation) - doesn't work.

"Pharmacy" is a text field to store the name of the pharmacy. This works.

"Pharmacies" is a linked record from subtable...

Sure this should not be hard...

 

Thanks in advance

Markus

3 replies

null
    • Fred
    • 2 days ago
    • Reported - view

    I'm not sure why line 2 is there. You don't do anything with the select statement.

    If 'Default pharmacy' is a dynamic choice field, then to set it you have to use the record Id. If you add Id to the Dynamic value name then you will see the record Id.

    I'm guessing that 'Default pharmacy' pulls from the Pharmacy table that is the same table as the reference field Pharmacies in Client.

    What kind of reference field is Pharmacies in Clients? Is it 1:M (1 to many) or M:1 to Clients? or is it a M:M (many to many) between Pharmacy and Clients? You can post screenshots of the reference field edit page. Or you can post a sample DB with dummy information.

    • John_Halls
    • yesterday
    • Reported - view

    Hi 

    I would have a data model looking like this

    There is a relationship between Patients to Pharmacies so that there is one default Pharmacy for each Patient. There is also a Pharmacy for each Prescription. Each Prescription has to have a Patient, that's why I set Composition to Yes.

    When a new prescription is raised, the default Pharmacy is copied to the prescription with a script in the Trigger on new record

    Pharmacies := Patients.Pharmacies

    Regards John

      • Markus_Jarvinen
      • yesterday
      • Reported - view

       Oh good Lord... I can't believe I didn't even think to try the easiest and most logical possible way. Thank you John, that worked perfectly!!!!